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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
// SPDX-License-Identifier: GPL-2.0+
//
// fs-amp-lib.c --- Common library for FourSemi Audio Amplifiers
//
// Copyright (C) 2016-2025 Shanghai FourSemi Semiconductor Co.,Ltd.
#include <linux/crc16.h>
#include <linux/device.h>
#include <linux/firmware.h>
#include <linux/module.h>
#include <linux/slab.h>
#include "fs-amp-lib.h"
static int fs_get_scene_count(struct fs_amp_lib *amp_lib)
{
const struct fs_fwm_table *table;
int count;
if (!amp_lib || !amp_lib->dev)
return -EINVAL;
table = amp_lib->table[FS_INDEX_SCENE];
if (!table)
return -EFAULT;
count = table->size / sizeof(struct fs_scene_index);
if (count < 1 || count > FS_SCENE_COUNT_MAX) {
dev_err(amp_lib->dev, "Invalid scene count: %d\n", count);
return -ERANGE;
}
return count;
}
static void fs_get_fwm_string(struct fs_amp_lib *amp_lib,
int offset, const char **pstr)
{
const struct fs_fwm_table *table;
if (!amp_lib || !amp_lib->dev || !pstr)
return;
table = amp_lib->table[FS_INDEX_STRING];
if (table && offset > 0 && offset < table->size + sizeof(*table))
*pstr = (char *)table + offset;
else
*pstr = NULL;
}
static void fs_get_scene_reg(struct fs_amp_lib *amp_lib,
int offset, struct fs_amp_scene *scene)
{
const struct fs_fwm_table *table;
if (!amp_lib || !amp_lib->dev || !scene)
return;
table = amp_lib->table[FS_INDEX_REG];
if (table && offset > 0 && offset < table->size + sizeof(*table))
scene->reg = (struct fs_reg_table *)((char *)table + offset);
else
scene->reg = NULL;
}
static void fs_get_scene_model(struct fs_amp_lib *amp_lib,
int offset, struct fs_amp_scene *scene)
{
const struct fs_fwm_table *table;
const char *ptr;
if (!amp_lib || !amp_lib->dev || !scene)
return;
table = amp_lib->table[FS_INDEX_MODEL];
ptr = (char *)table;
if (table && offset > 0 && offset < table->size + sizeof(*table))
scene->model = (struct fs_file_table *)(ptr + offset);
else
scene->model = NULL;
}
static void fs_get_scene_effect(struct fs_amp_lib *amp_lib,
int offset, struct fs_amp_scene *scene)
{
const struct fs_fwm_table *table;
const char *ptr;
if (!amp_lib || !amp_lib->dev || !scene)
return;
table = amp_lib->table[FS_INDEX_EFFECT];
ptr = (char *)table;
if (table && offset > 0 && offset < table->size + sizeof(*table))
scene->effect = (struct fs_file_table *)(ptr + offset);
else
scene->effect = NULL;
}
static int fs_parse_scene_tables(struct fs_amp_lib *amp_lib)
{
const struct fs_scene_index *scene_index;
const struct fs_fwm_table *table;
struct fs_amp_scene *scene;
int idx, count;
if (!amp_lib || !amp_lib->dev)
return -EINVAL;
count = fs_get_scene_count(amp_lib);
if (count <= 0)
return -EFAULT;
scene = devm_kcalloc(amp_lib->dev, count, sizeof(*scene), GFP_KERNEL);
if (!scene)
return -ENOMEM;
amp_lib->scene_count = count;
amp_lib->scene = scene;
table = amp_lib->table[FS_INDEX_SCENE];
scene_index = (struct fs_scene_index *)table->buf;
for (idx = 0; idx < count; idx++) {
fs_get_fwm_string(amp_lib, scene_index->name, &scene->name);
if (!scene->name)
scene->name = devm_kasprintf(amp_lib->dev,
GFP_KERNEL, "S%d", idx);
dev_dbg(amp_lib->dev, "scene.%d name: %s\n", idx, scene->name);
fs_get_scene_reg(amp_lib, scene_index->reg, scene);
fs_get_scene_model(amp_lib, scene_index->model, scene);
fs_get_scene_effect(amp_lib, scene_index->effect, scene);
scene++;
scene_index++;
}
return 0;
}
static int fs_parse_all_tables(struct fs_amp_lib *amp_lib)
{
const struct fs_fwm_table *table;
const struct fs_fwm_index *index;
const char *ptr;
int idx, count;
int ret;
if (!amp_lib || !amp_lib->dev || !amp_lib->hdr)
return -EINVAL;
/* Parse all fwm tables */
table = (struct fs_fwm_table *)amp_lib->hdr->params;
index = (struct fs_fwm_index *)table->buf;
count = table->size / sizeof(*index);
for (idx = 0; idx < count; idx++, index++) {
if (index->type >= FS_INDEX_MAX)
return -ERANGE;
ptr = (char *)table + (int)index->offset;
amp_lib->table[index->type] = (struct fs_fwm_table *)ptr;
}
/* Parse all scene tables */
ret = fs_parse_scene_tables(amp_lib);
if (ret)
dev_err(amp_lib->dev, "Failed to parse scene: %d\n", ret);
return ret;
}
static int fs_verify_firmware(struct fs_amp_lib *amp_lib)
{
const struct fs_fwm_header *hdr;
int crcsum;
if (!amp_lib || !amp_lib->dev || !amp_lib->hdr)
return -EINVAL;
hdr = amp_lib->hdr;
/* Verify the crcsum code */
crcsum = crc16(0x0000, (const char *)&hdr->crc_size, hdr->crc_size);
if (crcsum != hdr->crc16) {
dev_err(amp_lib->dev, "Failed to checksum: %x-%x\n",
crcsum, hdr->crc16);
return -EFAULT;
}
/* Verify the devid(chip_type) */
if (amp_lib->devid != LO_U16(hdr->chip_type)) {
dev_err(amp_lib->dev, "DEVID dismatch: %04X#%04X\n",
amp_lib->devid, hdr->chip_type);
return -EINVAL;
}
return 0;
}
static void fs_print_firmware_info(struct fs_amp_lib *amp_lib)
{
const struct fs_fwm_header *hdr;
const char *pro_name = NULL;
const char *dev_name = NULL;
if (!amp_lib || !amp_lib->dev || !amp_lib->hdr)
return;
hdr = amp_lib->hdr;
fs_get_fwm_string(amp_lib, hdr->project, &pro_name);
fs_get_fwm_string(amp_lib, hdr->device, &dev_name);
dev_info(amp_lib->dev, "Project: %s Device: %s\n",
pro_name ? pro_name : "null",
dev_name ? dev_name : "null");
dev_info(amp_lib->dev, "Date: %04d%02d%02d-%02d%02d\n",
hdr->date.year, hdr->date.month, hdr->date.day,
hdr->date.hour, hdr->date.minute);
}
int fs_amp_load_firmware(struct fs_amp_lib *amp_lib, const char *name)
{
const struct firmware *cont;
struct fs_fwm_header *hdr;
int ret;
if (!amp_lib || !amp_lib->dev || !name)
return -EINVAL;
ret = request_firmware(&cont, name, amp_lib->dev);
if (ret) {
dev_err(amp_lib->dev, "Failed to request %s: %d\n", name, ret);
return ret;
}
dev_info(amp_lib->dev, "Loading %s - size: %zu\n", name, cont->size);
hdr = devm_kmemdup(amp_lib->dev, cont->data, cont->size, GFP_KERNEL);
release_firmware(cont);
if (!hdr)
return -ENOMEM;
amp_lib->hdr = hdr;
ret = fs_verify_firmware(amp_lib);
if (ret) {
amp_lib->hdr = NULL;
return ret;
}
ret = fs_parse_all_tables(amp_lib);
if (ret) {
amp_lib->hdr = NULL;
return ret;
}
fs_print_firmware_info(amp_lib);
return 0;
}
EXPORT_SYMBOL_GPL(fs_amp_load_firmware);
MODULE_AUTHOR("Nick Li <nick.li@foursemi.com>");
MODULE_DESCRIPTION("FourSemi audio amplifier library");
MODULE_LICENSE("GPL");
|