aboutsummaryrefslogtreecommitdiff
path: root/techpack/audio/dsp/q6common.c
blob: 6e50196b6b6b1212eae5e60346fb82271142b458 (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
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
/* Copyright (c) 2017-2019, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <dsp/q6common.h>

struct q6common_ctl {
	bool instance_id_supported;
};

static struct q6common_ctl common;

/**
 * q6common_update_instance_id_support
 *
 * Update instance ID support flag to true/false
 *
 * @supported: enable/disable for instance ID support
 */
void q6common_update_instance_id_support(bool supported)
{
	common.instance_id_supported = supported;
}
EXPORT_SYMBOL(q6common_update_instance_id_support);

/**
 * q6common_is_instance_id_supported
 *
 * Returns true/false for instance ID support
 */
bool q6common_is_instance_id_supported(void)
{
	return common.instance_id_supported;
}
EXPORT_SYMBOL(q6common_is_instance_id_supported);

/**
 * q6common_pack_pp_params
 *
 * Populate params header based on instance ID support and pack
 * it with payload.
 * Instance ID support -
 *     yes - param_hdr_v3 + payload
 *     no  - param_hdr_v1 + payload
 *
 * @dest: destination data pointer to be packed into
 * @v3_hdr: param header v3
 * @param_data: param payload
 * @total_size: total size of packed data (hdr + payload)
 *
 * Returns 0 on success or error on failure
 */
int q6common_pack_pp_params(u8 *dest, struct param_hdr_v3 *v3_hdr,
			    u8 *param_data, u32 *total_size)
{
	struct param_hdr_v1 *v1_hdr = NULL;
	u32 packed_size = 0;
	u32 param_size = 0;
	bool iid_supported = q6common_is_instance_id_supported();

	if (dest == NULL) {
		pr_err("%s: Received NULL pointer for destination\n", __func__);
		return -EINVAL;
	} else if (v3_hdr == NULL) {
		pr_err("%s: Received NULL pointer for header\n", __func__);
		return -EINVAL;
	} else if (total_size == NULL) {
		pr_err("%s: Received NULL pointer for total size\n", __func__);
		return -EINVAL;
	}

	param_size = v3_hdr->param_size;
	packed_size = iid_supported ? sizeof(struct param_hdr_v3) :
				      sizeof(struct param_hdr_v1);

	if (iid_supported) {
		memcpy(dest, v3_hdr, packed_size);
	} else {
		v1_hdr = (struct param_hdr_v1 *) dest;
		v1_hdr->module_id = v3_hdr->module_id;
		v1_hdr->param_id = v3_hdr->param_id;

		if (param_size > U16_MAX) {
			pr_err("%s: Invalid param size for V1 %d\n", __func__,
			       param_size);
			return -EINVAL;
		}
		v1_hdr->param_size = param_size;
		v1_hdr->reserved = 0;
	}

	/*
	 * Make param_data optional for cases when there is no data
	 * present as in some set cases and all get cases.
	 */
	if (param_data != NULL) {
		memcpy(dest + packed_size, param_data, param_size);
		packed_size += param_size;
	}

	*total_size = packed_size;

	return 0;
}
EXPORT_SYMBOL(q6common_pack_pp_params);

/**
 * q6common_pack_pp_params_v2
 *
 * Populate params header based on instance ID support and pack
 * it with payload.
 * Instance ID support -
 *     yes - param_hdr_v3 + payload
 *     no  - param_hdr_v1 + payload
 *
 * @dest: destination data pointer to be packed into
 * @v3_hdr: param header v3
 * @param_data: param payload
 * @total_size: total size of packed data (hdr + payload)
 * @iid_supported: Instance ID supported or not
 *
 * Returns 0 on success or error on failure
 */
int q6common_pack_pp_params_v2(u8 *dest, struct param_hdr_v3 *v3_hdr,
			    u8 *param_data, u32 *total_size,
			    bool iid_supported)
{
	struct param_hdr_v1 *v1_hdr = NULL;
	u32 packed_size = 0;
	u32 param_size = 0;

	if (dest == NULL) {
		pr_err("%s: Received NULL pointer for destination\n", __func__);
		return -EINVAL;
	} else if (v3_hdr == NULL) {
		pr_err("%s: Received NULL pointer for header\n", __func__);
		return -EINVAL;
	} else if (total_size == NULL) {
		pr_err("%s: Received NULL pointer for total size\n", __func__);
		return -EINVAL;
	}

	param_size = v3_hdr->param_size;
	packed_size = iid_supported ? sizeof(struct param_hdr_v3) :
				      sizeof(struct param_hdr_v1);

	if (iid_supported) {
		memcpy(dest, v3_hdr, packed_size);
	} else {
		v1_hdr = (struct param_hdr_v1 *) dest;
		v1_hdr->module_id = v3_hdr->module_id;
		v1_hdr->param_id = v3_hdr->param_id;

		if (param_size > U16_MAX) {
			pr_err("%s: Invalid param size for V1 %d\n", __func__,
			       param_size);
			return -EINVAL;
		}
		v1_hdr->param_size = param_size;
		v1_hdr->reserved = 0;
	}

	/*
	 * Make param_data optional for cases when there is no data
	 * present as in some set cases and all get cases.
	 */
	if (param_data != NULL) {
		memcpy(dest + packed_size, param_data, param_size);
		packed_size += param_size;
	}

	*total_size = packed_size;

	return 0;
}
EXPORT_SYMBOL(q6common_pack_pp_params_v2);