2017-11-03 18:28:30 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2009-06-03 21:17:58 +08:00
|
|
|
/*
|
2012-02-03 00:29:53 +08:00
|
|
|
* u_uac1.c -- ALSA audio utilities for Gadget stack
|
2009-06-03 21:17:58 +08:00
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
|
|
|
|
* Copyright (C) 2008 Analog Devices, Inc
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
2014-07-23 01:58:38 +08:00
|
|
|
#include <linux/module.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2009-06-03 21:17:58 +08:00
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/ctype.h>
|
|
|
|
#include <linux/random.h>
|
|
|
|
#include <linux/syscalls.h>
|
|
|
|
|
2017-06-18 21:23:53 +08:00
|
|
|
#include "u_uac1_legacy.h"
|
2009-06-03 21:17:58 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This component encapsulates the ALSA devices for USB audio gadget
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Some ALSA internal helper functions
|
|
|
|
*/
|
|
|
|
static int snd_interval_refine_set(struct snd_interval *i, unsigned int val)
|
|
|
|
{
|
|
|
|
struct snd_interval t;
|
|
|
|
t.empty = 0;
|
|
|
|
t.min = t.max = val;
|
|
|
|
t.openmin = t.openmax = 0;
|
|
|
|
t.integer = 1;
|
|
|
|
return snd_interval_refine(i, &t);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _snd_pcm_hw_param_set(struct snd_pcm_hw_params *params,
|
|
|
|
snd_pcm_hw_param_t var, unsigned int val,
|
|
|
|
int dir)
|
|
|
|
{
|
|
|
|
int changed;
|
|
|
|
if (hw_is_mask(var)) {
|
|
|
|
struct snd_mask *m = hw_param_mask(params, var);
|
|
|
|
if (val == 0 && dir < 0) {
|
|
|
|
changed = -EINVAL;
|
|
|
|
snd_mask_none(m);
|
|
|
|
} else {
|
|
|
|
if (dir > 0)
|
|
|
|
val++;
|
|
|
|
else if (dir < 0)
|
|
|
|
val--;
|
|
|
|
changed = snd_mask_refine_set(
|
|
|
|
hw_param_mask(params, var), val);
|
|
|
|
}
|
|
|
|
} else if (hw_is_interval(var)) {
|
|
|
|
struct snd_interval *i = hw_param_interval(params, var);
|
|
|
|
if (val == 0 && dir < 0) {
|
|
|
|
changed = -EINVAL;
|
|
|
|
snd_interval_none(i);
|
|
|
|
} else if (dir == 0)
|
|
|
|
changed = snd_interval_refine_set(i, val);
|
|
|
|
else {
|
|
|
|
struct snd_interval t;
|
|
|
|
t.openmin = 1;
|
|
|
|
t.openmax = 1;
|
|
|
|
t.empty = 0;
|
|
|
|
t.integer = 0;
|
|
|
|
if (dir < 0) {
|
|
|
|
t.min = val - 1;
|
|
|
|
t.max = val;
|
|
|
|
} else {
|
|
|
|
t.min = val;
|
|
|
|
t.max = val+1;
|
|
|
|
}
|
|
|
|
changed = snd_interval_refine(i, &t);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
return -EINVAL;
|
|
|
|
if (changed) {
|
|
|
|
params->cmask |= 1 << var;
|
|
|
|
params->rmask |= 1 << var;
|
|
|
|
}
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set default hardware params
|
|
|
|
*/
|
|
|
|
static int playback_default_hw_params(struct gaudio_snd_dev *snd)
|
|
|
|
{
|
|
|
|
struct snd_pcm_substream *substream = snd->substream;
|
|
|
|
struct snd_pcm_hw_params *params;
|
|
|
|
snd_pcm_sframes_t result;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SNDRV_PCM_ACCESS_RW_INTERLEAVED,
|
|
|
|
* SNDRV_PCM_FORMAT_S16_LE
|
|
|
|
* CHANNELS: 2
|
|
|
|
* RATE: 48000
|
|
|
|
*/
|
|
|
|
snd->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
|
|
|
|
snd->format = SNDRV_PCM_FORMAT_S16_LE;
|
|
|
|
snd->channels = 2;
|
|
|
|
snd->rate = 48000;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
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_PREPARE, NULL);
|
|
|
|
if (result < 0) {
|
|
|
|
ERROR(snd->card,
|
|
|
|
"Preparing sound card failed: %d\n", (int)result);
|
|
|
|
kfree(params);
|
|
|
|
return 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);
|
|
|
|
|
|
|
|
kfree(params);
|
|
|
|
|
|
|
|
INFO(snd->card,
|
|
|
|
"Hardware params: access %x, format %x, channels %d, rate %d\n",
|
|
|
|
snd->access, snd->format, snd->channels, snd->rate);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Playback audio buffer data by ALSA PCM device
|
|
|
|
*/
|
2014-07-23 01:58:38 +08:00
|
|
|
size_t u_audio_playback(struct gaudio *card, void *buf, size_t count)
|
2009-06-03 21:17:58 +08:00
|
|
|
{
|
|
|
|
struct gaudio_snd_dev *snd = &card->playback;
|
|
|
|
struct snd_pcm_substream *substream = snd->substream;
|
|
|
|
struct snd_pcm_runtime *runtime = substream->runtime;
|
|
|
|
ssize_t result;
|
|
|
|
snd_pcm_sframes_t frames;
|
|
|
|
|
|
|
|
try_again:
|
|
|
|
if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
|
|
|
|
runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
|
|
|
|
result = snd_pcm_kernel_ioctl(substream,
|
|
|
|
SNDRV_PCM_IOCTL_PREPARE, NULL);
|
|
|
|
if (result < 0) {
|
|
|
|
ERROR(card, "Preparing sound card failed: %d\n",
|
|
|
|
(int)result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
frames = bytes_to_frames(runtime, count);
|
2017-05-21 16:19:16 +08:00
|
|
|
result = snd_pcm_kernel_write(snd->substream, buf, frames);
|
2009-06-03 21:17:58 +08:00
|
|
|
if (result != frames) {
|
|
|
|
ERROR(card, "Playback error: %d\n", (int)result);
|
|
|
|
goto try_again;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-23 01:58:38 +08:00
|
|
|
int u_audio_get_playback_channels(struct gaudio *card)
|
2009-06-03 21:17:58 +08:00
|
|
|
{
|
|
|
|
return card->playback.channels;
|
|
|
|
}
|
|
|
|
|
2014-07-23 01:58:38 +08:00
|
|
|
int u_audio_get_playback_rate(struct gaudio *card)
|
2009-06-03 21:17:58 +08:00
|
|
|
{
|
|
|
|
return card->playback.rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open ALSA PCM and control device files
|
|
|
|
* Initial the PCM or control device
|
|
|
|
*/
|
|
|
|
static int gaudio_open_snd_dev(struct gaudio *card)
|
|
|
|
{
|
|
|
|
struct snd_pcm_file *pcm_file;
|
|
|
|
struct gaudio_snd_dev *snd;
|
2017-06-18 21:23:53 +08:00
|
|
|
struct f_uac1_legacy_opts *opts;
|
2014-07-23 01:58:38 +08:00
|
|
|
char *fn_play, *fn_cap, *fn_cntl;
|
|
|
|
|
2017-06-18 21:23:53 +08:00
|
|
|
opts = container_of(card->func.fi, struct f_uac1_legacy_opts,
|
|
|
|
func_inst);
|
2014-07-23 01:58:38 +08:00
|
|
|
fn_play = opts->fn_play;
|
|
|
|
fn_cap = opts->fn_cap;
|
|
|
|
fn_cntl = opts->fn_cntl;
|
2009-06-03 21:17:58 +08:00
|
|
|
|
|
|
|
/* Open control device */
|
|
|
|
snd = &card->control;
|
|
|
|
snd->filp = filp_open(fn_cntl, O_RDWR, 0);
|
|
|
|
if (IS_ERR(snd->filp)) {
|
|
|
|
int ret = PTR_ERR(snd->filp);
|
|
|
|
ERROR(card, "unable to open sound control device file: %s\n",
|
|
|
|
fn_cntl);
|
|
|
|
snd->filp = NULL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
snd->card = card;
|
|
|
|
|
|
|
|
/* Open PCM playback device and setup substream */
|
|
|
|
snd = &card->playback;
|
|
|
|
snd->filp = filp_open(fn_play, O_WRONLY, 0);
|
|
|
|
if (IS_ERR(snd->filp)) {
|
2013-02-28 12:44:38 +08:00
|
|
|
int ret = PTR_ERR(snd->filp);
|
|
|
|
|
2009-06-03 21:17:58 +08:00
|
|
|
ERROR(card, "No such PCM playback device: %s\n", fn_play);
|
|
|
|
snd->filp = NULL;
|
2013-02-28 12:44:38 +08:00
|
|
|
return ret;
|
2009-06-03 21:17:58 +08:00
|
|
|
}
|
|
|
|
pcm_file = snd->filp->private_data;
|
|
|
|
snd->substream = pcm_file->substream;
|
|
|
|
snd->card = card;
|
|
|
|
playback_default_hw_params(snd);
|
|
|
|
|
|
|
|
/* Open PCM capture device and setup substream */
|
|
|
|
snd = &card->capture;
|
|
|
|
snd->filp = filp_open(fn_cap, O_RDONLY, 0);
|
|
|
|
if (IS_ERR(snd->filp)) {
|
|
|
|
ERROR(card, "No such PCM capture device: %s\n", fn_cap);
|
2009-08-03 02:38:58 +08:00
|
|
|
snd->substream = NULL;
|
|
|
|
snd->card = NULL;
|
2010-12-01 18:53:00 +08:00
|
|
|
snd->filp = NULL;
|
2009-08-03 02:38:58 +08:00
|
|
|
} else {
|
|
|
|
pcm_file = snd->filp->private_data;
|
|
|
|
snd->substream = pcm_file->substream;
|
|
|
|
snd->card = card;
|
2009-06-03 21:17:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close ALSA PCM and control device files
|
|
|
|
*/
|
|
|
|
static int gaudio_close_snd_dev(struct gaudio *gau)
|
|
|
|
{
|
|
|
|
struct gaudio_snd_dev *snd;
|
|
|
|
|
|
|
|
/* Close control device */
|
|
|
|
snd = &gau->control;
|
2010-12-01 18:53:00 +08:00
|
|
|
if (snd->filp)
|
2012-07-23 01:23:33 +08:00
|
|
|
filp_close(snd->filp, NULL);
|
2009-06-03 21:17:58 +08:00
|
|
|
|
|
|
|
/* Close PCM playback device and setup substream */
|
|
|
|
snd = &gau->playback;
|
2010-12-01 18:53:00 +08:00
|
|
|
if (snd->filp)
|
2012-07-23 01:23:33 +08:00
|
|
|
filp_close(snd->filp, NULL);
|
2009-06-03 21:17:58 +08:00
|
|
|
|
|
|
|
/* Close PCM capture device and setup substream */
|
|
|
|
snd = &gau->capture;
|
2010-12-01 18:53:00 +08:00
|
|
|
if (snd->filp)
|
2012-07-23 01:23:33 +08:00
|
|
|
filp_close(snd->filp, NULL);
|
2009-06-03 21:17:58 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gaudio_setup - setup ALSA interface and preparing for USB transfer
|
|
|
|
*
|
|
|
|
* This sets up PCM, mixer or MIDI ALSA devices fore USB gadget using.
|
|
|
|
*
|
|
|
|
* Returns negative errno, or zero on success
|
|
|
|
*/
|
2014-07-23 01:58:38 +08:00
|
|
|
int gaudio_setup(struct gaudio *card)
|
2009-06-03 21:17:58 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = gaudio_open_snd_dev(card);
|
|
|
|
if (ret)
|
|
|
|
ERROR(card, "we need at least one control device\n");
|
2009-12-10 11:28:39 +08:00
|
|
|
|
2009-06-03 21:17:58 +08:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gaudio_cleanup - remove ALSA device interface
|
|
|
|
*
|
|
|
|
* This is called to free all resources allocated by @gaudio_setup().
|
|
|
|
*/
|
2014-07-23 01:58:38 +08:00
|
|
|
void gaudio_cleanup(struct gaudio *the_card)
|
2009-06-03 21:17:58 +08:00
|
|
|
{
|
2014-12-01 16:09:29 +08:00
|
|
|
if (the_card)
|
2009-12-10 11:28:39 +08:00
|
|
|
gaudio_close_snd_dev(the_card);
|
2009-06-03 21:17:58 +08:00
|
|
|
}
|
|
|
|
|