aboutsummaryrefslogtreecommitdiffstats
path: root/alsa-pcm.c
blob: 17c08fd6bb07b60d172ef62b40a6957cca70f5aa (plain)
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
# Low level CAN signaling binder

Low level CAN bus binder. Based upon OpenXC vi-firmware project. Purpose of this project is to offer a low level binding to an AGL platform, idea remains the same than the vi-firmware project. It's meant to generate from a JSON file describing CAN messages and diagnostic message (OBD2 for now) present in a car, a cpp file to integrate with the project and compile all together. Result will be a widget file to install on an AGL target system.

# Prerequirements

- Make sure you already have set up the AGL SDK before using the following [guide][SDK_instructions].

- This repo make use of git submodule, make sure to execute the following commands from the repository once cloned :

```bash
$ git submodule init
$ git submodule update
```

- Make sure you have installed the AGL generator else you aren't able to generate custom low-level CAN binding. Generator can be found [here][generator] with the attached instruction to install and run it. It will produce a *configuration-generated.cpp* file to paste in the source, *src/*, directory.

# Getting started

## Compile and install the binding

AGL SDK environment correctly set, if you have an AGL target already running in your network, I encourage you to set the TARGET variable in the root CMakeLists.txt file. Then you can directly install the binding and source directory on your target system.

Execute commands to get your binding compile :

```bash
$ mkdir build
$ cd build
$ cmake ..
$ make
```

And if you have set TARGET variable, you can install it on your AGL system :

```bash
$ make install
```

If not, you will have to install it manually copying the *low-can-binding.wgt* file on your target, then from it execute the following commands :

```bash
~# afm-util install low-can-binding.wgt
{ "added": "low-can-binding@0.1" } 
```

## Confiure system and binding

Configure the binding specifying in the JSON configuration file the CAN device that the binding will to connect to. Edit file */var/lib/afm/applications/low-can-binding/0.1/can_buses.json* and change the CAN device name to the one you have :

```json
{
	"canbus":  "vcan0"
}
```

If you have several CAN bus devices then use an array:

```json
{
	"canbus": [ "vcan0".highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
// SPDX-License-Identifier: GPL-2.0
/*
 * ALSA Virtual Soundcard
 *
 * alsa-pcm.c - AVIRT ALSA PCM interface
 *
 * Copyright (C) 2010-2018 Fiberdyne Systems Pty Ltd
 */

#include "core_internal.h"

#define D_LOGNAME "pcm"

#define D_INFOK(fmt, args...) DINFO(D_LOGNAME, fmt, ##args)
#define D_PRINTK(fmt, args...) DDEBUG(D_LOGNAME, fmt, ##args)
#define D_ERRORK(fmt, args...) DERROR(D_LOGNAME, fmt, ##args)

#define DO_AUDIOPATH_CB(ap, callback, substream, ...)                          \
	(((ap)->pcm_ops->callback) ?                                           \
		 (ap)->pcm_ops->callback((substream), ##__VA_ARGS__) :         \
		 0)

/**
 * avirt_pcm_period_elapsed - PCM buffer complete callback
 * @substreamid: pointer to ALSA PCM substream
 *
 * This should be called from a child Audio Path once it has finished processing
 * the pcm buffer
 */
void avirt_pcm_period_elapsed(struct snd_pcm_substream *substream)
{
	// Notify ALSA middle layer of the elapsed period boundary
	snd_pcm_period_elapsed(substream);
}
EXPORT_SYMBOL_GPL(avirt_pcm_period_elapsed);

/*******************************************************************************
 * ALSA PCM Callbacks
 ******************************************************************************/
/**
 * pcm_open - Implements 'open' callback for PCM middle layer
 * @substream: pointer to ALSA PCM substream
 *
 * This is called when an ALSA PCM substream is opened. The substream device is
 * configured here.
 *
 * Returns 0 on success or error code otherwise.
 */
static int pcm_open(struct snd_pcm_substream *substream)
{
	struct avirt_audiopath *audiopath;
	struct avirt_stream *stream;
	struct snd_pcm_hardware *hw;
	unsigned int chans = 0;

	stream = __avirt_stream_find_by_device(substream->pcm->device);
	audiopath = avirt_audiopath_get(stream->map);
	CHK_NULL_V(audiopath, -EFAULT, "Cannot find Audio Path uid: '%s'!",
		   stream->map);
	substream->private_data = audiopath;

	// Copy the hw params from the audiopath to the pcm
	hw = &substream->runtime->hw;
	memcpy(hw, audiopath->hw, sizeof(struct snd_pcm_hardware));

	stream = __avirt_stream_find_by_device(substream->pcm->device);
	if (IS_ERR_VALUE(stream) || !stream)
		return PTR_ERR(stream);

	// Setup remaining hw properties
	chans = stream->channels;
	hw->channels_min = chans;
	hw->channels_max = chans;

	// Do additional Audio Path 'open' callback
	return DO_AUDIOPATH_CB(audiopath, open, substream);
}

/**
 * pcm_close - Implements 'close' callback for PCM middle layer
 * @substream: pointer to ALSA PCM substream
 *
 * This is called when a PCM substream is closed.
 *
 * Returns 0 on success or error code otherwise.
 */
static int pcm_close(struct snd_pcm_substream *substream)
{
	// Do additional Audio Path 'close' callback
	return DO_AUDIOPATH_CB(
		((struct avirt_audiopath *)substream->private_data), close,
		substream);
}

/**
 * pcm_hw_params - Implements 'hw_params' callback for PCM middle layer
 * @substream: pointer to ALSA PCM substream
 * @hw_params: contains the hardware parameters for the PCM
 *
 * This is called when the hardware parameters are set, including buffer size,
 * the period size, the format, etc. The buffer allocation should be done here.
 *
 * Returns 0 on success or error code otherwise.
 */
static int pcm_hw_params(struct snd_pcm_substream *substream,
			 struct snd_pcm_hw_params *hw_params)
{
	int retval;
	size_t bufsz;
	struct avirt_audiopath *audiopath;
	struct avirt_stream *stream;

	stream = __avirt_stream_find_by_device(substream->pcm->device);
	if (IS_ERR_VALUE(stream) || !stream)
		return PTR_ERR(stream);

	if ((params_channels(hw_params) > stream->channels) ||
	    (params_channels(hw_params) < stream->channels)) {
		D_ERRORK("Requested number of channels: %d not supported",
			 params_channels(hw_params));
		return -EINVAL;
	}

	audiopath = ((struct avirt_audiopath *)substream->private_data);
	bufsz = params_buffer_bytes(hw_params) * audiopath->hw->periods_max;

	retval = snd_pcm_lib_alloc_vmalloc_buffer(substream, bufsz);
	if (retval < 0)
		D_ERRORK("pcm: buffer allocation failed: %d", retval);

	return retval;
}

/**
 * pcm_hw_free - Implements 'hw_free' callback for PCM middle layer
 * @substream: pointer to ALSA PCM substream
 *
 * Release the resources allocated via 'hw_params'.
 * This function is always called before the 'close' callback .
 *
 * Returns 0 on success or error code otherwise.
 */
static int pcm_hw_free(struct snd_pcm_substream *substream)
{
	int err;

	// Do additional Audio Path 'hw_free' callback
	err = DO_AUDIOPATH_CB(
		((struct avirt_audiopath *)substream->private_data), hw_free,
		substream);

	return snd_pcm_lib_free_vmalloc_buffer(substream);
}

/**
 * pcm_prepare - Implements 'prepare' callback for PCM middle layer
 * @substream: pointer to ALSA PCM substream
 *
 * The format rate, sample rate, etc., can be set here. This callback can be
 * called many times at each setup. This function is also used to handle overrun
 * and underrun conditions when we try and resync the DMA (if we're using DMA).
 *
 * Returns 0 on success or error code otherwise.
 */
static int pcm_prepare(struct snd_pcm_substream *substream)
{
	// Do additional Audio Path 'prepare' callback
	return DO_AUDIOPATH_CB(
		((struct avirt_audiopath *)substream->private_data), prepare,
		substream);
}

/**
 * pcm_trigger - Implements 'trigger' callback for PCM middle layer
 * @substream: pointer to ALSA PCM substream
 * @cmd: action to be performed (start or stop)
 *
 * This is called when the PCM is started, stopped or paused. The action
 * indicated action is 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)
{
	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
	case SNDRV_PCM_TRIGGER_RESUME:
	case SNDRV_PCM_TRIGGER_STOP:
	case SNDRV_PCM_TRIGGER_SUSPEND:
		break;
	default:
		D_ERRORK("Invalid trigger cmd: %d", cmd);
		return -EINVAL;
	}

	// Do additional Audio Path 'trigger' callback
	return DO_AUDIOPATH_CB(
		((struct avirt_audiopath *)substream->private_data), trigger,
		substream, cmd);
}

/**
 * pcm_pointer - Implements 'pointer' callback for PCM middle layer
 * @substream: pointer to ALSA PCM substream
 *
 * This gets called when the user space needs a DMA buffer index. IO errors will
 * be generated if the index does not increment, or drives beyond the frame
 * threshold of the buffer itself.
 *
 * Returns the current hardware buffer frame index.
 */
static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream)
{
	// Do additional Audio Path 'pointer' callback
	return DO_AUDIOPATH_CB(
		((struct avirt_audiopath *)substream->private_data), pointer,
		substream);
}

/**
 * pcm_pointer - Implements 'get_time_info' callback for PCM middle layer
 * @substream: pointer to ALSA PCM substream
 * @system_ts
 * @audio_ts
 * @audio_tstamp_config
 * @audio_tstamp_report
 *
 * Generic way to get system timestamp and audio timestamp info
 *
 * Returns 0 on success or error code otherwise
 */
static int pcm_get_time_info(
	struct snd_pcm_substream *substream, struct timespec *system_ts,
	struct timespec *audio_ts,
	struct snd_pcm_audio_tstamp_config *audio_tstamp_config,
	struct snd_pcm_audio_tstamp_report *audio_tstamp_report)
{
	return DO_AUDIOPATH_CB(
		((struct avirt_audiopath *)substream->private_data),
		get_time_info, substream, system_ts, audio_ts,
		audio_tstamp_config, audio_tstamp_report);
}

/**
 * pcm_copy_user - Implements 'copy_user' callback for PCM middle layer
 * @substream: pointer to ALSA PCM substream
 * @channel:
 * @pos: The offset in the DMA
 * @src: Audio PCM data from the user space
 * @count:
 *
 * This is where we need to copy user audio PCM data into the sound driver
 *
 * Returns 0 on success or error code otherwise.
 *
 */
static int pcm_copy_user(struct snd_pcm_substream *substream, int channel,
			 snd_pcm_uframes_t pos, void __user *src,
			 snd_pcm_uframes_t count)
{
	// struct snd_pcm_runtime *runtime;
	// int offset;

	// runtime = substream->runtime;
	// offset = frames_to_bytes(runtime, pos);

	// Do additional Audio Path 'copy_user' callback
	return DO_AUDIOPATH_CB(
		((struct avirt_audiopath *)substream->private_data), copy_user,
		substream, channel, pos, src, count);
}

/**
 * pcm_copy_kernel - Implements 'copy_kernel' callback for PCM middle layer
 * @substream: pointer to ALSA PCM substream
 * @channel:
 * @pos: The offset in the DMA
 * @src: Audio PCM data from the user space
 * @count:
 *
 * This is where we need to copy kernel audio PCM data into the sound driver
 *
 * Returns 0 on success or error code otherwise.
 *
 */
static int pcm_copy_kernel(struct snd_pcm_substream *substream, int channel,
			   unsigned long pos, void *buf, unsigned long count)
{
	return DO_AUDIOPATH_CB(
		((struct avirt_audiopath *)substream->private_data),
		copy_kernel, substream, channel, pos, buf, count);
}

/**
 * pcm_ack - Implements 'ack' callback for PCM middle layer
 * @substream: pointer to ALSA PCM substream
 *
 * This is where we need to ack
 *
 * Returns 0 on success or error code otherwise.
 *
 */
static int pcm_ack(struct snd_pcm_substream *substream)
{
	return DO_AUDIOPATH_CB(
		((struct avirt_audiopath *)substream->private_data), ack,
		substream);
}

static int pcm_silence(struct snd_pcm_substream *substream, int channel,
		       snd_pcm_uframes_t pos, snd_pcm_uframes_t count)
{
	return DO_AUDIOPATH_CB(
		((struct avirt_audiopath *)substream->private_data),
		fill_silence, substream, channel, pos, count);
}

struct snd_pcm_ops pcm_ops = {
	.open = pcm_open,
	.close = pcm_close,
	.ioctl = snd_pcm_lib_ioctl,
	.hw_params = pcm_hw_params,
	.hw_free = pcm_hw_free,
	.prepare = pcm_prepare,
	.trigger = pcm_trigger,
	.pointer = pcm_pointer,
	//.get_time_info = pcm_get_time_info,
	//.fill_silence = pcm_silence,
	.copy_user = pcm_copy_user,
	//.copy_kernel = pcm_copy_kernel,
	.page = snd_pcm_lib_get_vmalloc_page,
	//.ack = pcm_ack,
};