summaryrefstree
path: root/kernel/drivers/media/platform/exynos/camera/is-binary.c
blob: 9eff8f0a6e178cedb412e484922bfb7faa5d0e93 (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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 * Samsung EXYNOS FIMC-IS (Imaging Subsystem) driver
 *
 * Copyright (C) 2015 Samsung Electronics Co., Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/device.h>
#include <linux/vmalloc.h>
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/slab.h>
#include <linux/firmware.h>

#include "is-binary.h"
#include "exynos-is-sensor.h"

/* the version storage of each library binary */
typedef struct { char v[LIBRARY_VER_LEN + 1]; } lib_ver_t;
static lib_ver_t lib_ver_s[IS_BIN_LIB_HINT_RTA + 1];
/* the version stroage of each sensor setfile by position */
typedef struct { char v[SETFILE_VER_LEN + 1]; } set_ver_t;
static set_ver_t set_ver_s[SENSOR_POSITION_MAX];

static const char *bin_names[] = { "DDK", "RTA", "SETFILE" };

static char *library_get_buf(const struct is_bin_ver_info *info, unsigned int hint)
{
	lib_ver_t *s = (lib_ver_t *)info->s;

	return s[hint].v;
}

static unsigned int library_get_name_idx(unsigned int hint)
{

	if (hint == IS_BIN_LIB_HINT_RTA)
		return 1; /* RTA */

	return 0; /* DDK */
}

static char *setfile_get_buf(const struct is_bin_ver_info *info, unsigned int hint)
{
	set_ver_t *s = (set_ver_t *)info->s;

	return s[hint].v;
}

static unsigned int setfile_get_name_idx(unsigned int hint)
{
	return 2; /* SETFILE */
}

static const struct is_bin_ver_info bin_ver_info[] = {
	{
		.type		= IS_BIN_LIBRARY,
		.maxhint	= IS_BIN_LIB_HINT_RTA + 1,
		.offset		= LIBRARY_VER_OFS,
		.length		= LIBRARY_VER_LEN,
		.s		= &lib_ver_s,
		.get_buf	= library_get_buf,
		.get_name_idx	= library_get_name_idx,
	},
	{
		.type		= IS_BIN_SETFILE,
		.maxhint	= SENSOR_POSITION_MAX,
		.offset		= SETFILE_VER_OFS,
		.length		= SETFILE_VER_LEN,
		.s		= &set_ver_s,
		.get_buf	= setfile_get_buf,
		.get_name_idx	= setfile_get_name_idx,
	},
	{},
};

static noinline_for_stack long get_file_size(struct file *file)
{
	struct kstat st;
	u32 request_mask = (STATX_MODE | STATX_SIZE);

	if (vfs_getattr(&file->f_path, &st, request_mask, KSTAT_QUERY_FLAGS))
		return -1;
	if (!S_ISREG(st.mode))
		return -1;
	if (st.size != (long)st.size)
		return -1;

	return st.size;
}

static int read_file_contents(struct file *fp, struct is_binary *bin)
{
	long size;
	char *buf;
	int ret;
	loff_t pos = 0;

	size = get_file_size(fp);
	if (size <= 0)
		return -EBADF;

	/* if a buffer for binary is already allocated */
	if (bin->data) {
		if (!bin->size)
			return -EINVAL;

		buf = bin->data;

		/* shrink read size to fit the size of a given buffer */
		if (size > bin->size) {
			pr_crit("%s: will read only %ld bytes from a file (%ld)",
				__func__, bin->size, size);
			size = bin->size;
		}
	} else {
		buf = bin->alloc(size);
		if (!buf)
			return -ENOMEM;
	}

	ret = kernel_read(fp, buf, size, &pos);
	if (ret != size) {
		if (!bin->data)
			bin->free(buf);
		return ret;
	}

	bin->data = buf;
	bin->size = size;

	return 0;
}

static int write_file_contents(struct file *fp, struct is_binary *bin)
{
	ssize_t ret;
	ssize_t count;
	loff_t pos = 0;
	loff_t pos_temp = 0;
	char *buf = (char *)bin->data;

	do {
		count = min_t(ssize_t, PAGE_SIZE, bin->size - pos);
		ret = kernel_write(fp, buf, count, &pos_temp);
		if (ret > 0) {
			pos_temp += ret;
			pos += ret;
			buf += ret;
			ret = 0;
		} else {
			break;
		}
	} while (pos < bin->size);

	return ret;
}

/*
 * get_filesystem_binary: copy a binary in userland to a given buffer
 * @filename: filenam of binary file including full path
 * @bin: pointer to is_binary structure
 */
int get_filesystem_binary(const char *filename, struct is_binary *bin)
{
	struct file *fp;
	int ret = 0;

	fp = filp_open(filename, O_RDONLY, 0);
	if (!IS_ERR_OR_NULL(fp)) {
		ret = read_file_contents(fp, bin);
		fput(fp);
	} else {
		ret = PTR_ERR(fp);
	}

	return ret;
}

/*
 * put_filesystem_binary: copy a given buffer to a file in userland
 * @filename: filenam of binary file including full path
 * @bin: pointer to is_binary structure
 */
int put_filesystem_binary(const char *filename, struct is_binary *bin, u32 flags)
{
	struct file *fp;
	int ret = 0;

	fp = filp_open(filename, flags, 0666);
	if (!IS_ERR_OR_NULL(fp)) {
		ret = write_file_contents(fp, bin);
		fput(fp);
	} else {
		ret = PTR_ERR(fp);
	}

	return ret;
}

/*
 * setup_binary_loader: customize an instance of the binary loader
 * @bin: pointer to is_binary structure
 * @retry_cnt: retry count of request_firmware
 * @retry_err: specific error number for retry of request_firmware
 * @alloc: custom allocation function for file system loading
 * @free: custom free function for file system loading
 */
void setup_binary_loader(struct is_binary *bin,
				unsigned int retry_cnt, int retry_err,
				void *(*alloc)(unsigned long size),
				void (*free)(const void *buf))
{
	bin->retry_cnt = retry_cnt;
	bin->retry_err = retry_err;

	if (alloc && free) {
		bin->alloc = alloc;
		bin->free = free;
	} else {
		/* set vmalloc/vfree as a default */
		bin->alloc = &vmalloc;
		bin->free =  &vfree;
	}

	bin->customized = (unsigned long)bin;
}

 /*
  * request_binary: send loading request to the loader
  * @bin: pointer to is_binary structure
  * @path: path of binary file
  * @name: name of binary file
  * @device: device for which binary is being loaded
  */
int request_binary(struct is_binary *bin, const char *path,
				const char *name, struct device *device)
{
	char *filename;
	unsigned int retry_cnt = 0;
	int retry_err = 0;
	int ret;

	bin->data = NULL;
	bin->size = 0;
	bin->fw = NULL;

	/* whether the loader is customized or not */
	if (bin->customized != (unsigned long)bin) {
		bin->alloc = &vmalloc;
		bin->free =  &vfree;
	} else {
		retry_cnt = bin->retry_cnt;
		retry_err = bin->retry_err;
	}

	/* read the requested binary from file system directly */
	if (path) {
		filename = __getname();
		if (unlikely(!filename))
			return -ENOMEM;

		snprintf(filename, PATH_MAX, "%s%s", path, name);
		ret = get_filesystem_binary(filename, bin);
		__putname(filename);
		/* read successfully or don't want to go further more */
		if (!ret || !device)
			return ret;
	}

	/* ask to 'request_firmware' */
	do {
		ret = request_firmware(&bin->fw, name, device);

		if (!ret && bin->fw) {
			bin->data = (void *)bin->fw->data;
			bin->size = bin->fw->size;

			break;
		}

		/*
		 * if no specific error for retry is given;
		 * whatever any error is occurred, we should retry
		 */
		if (!bin->retry_err)
			retry_err = ret;
	} while (retry_cnt-- && (retry_err == ret));

	return ret;
}

/*
 * release_firmware: release the resource related to a binary
 * @bin: binary resource to release
 */
void release_binary(struct is_binary *bin)
{
	if (bin->fw)
		release_firmware(bin->fw);
	else if (bin->data)
		bin->free(bin->data);
}

/*
 * was_loaded_by: how was a binary loaded
 * @bin: pointer to is_binary structure
 */
int was_loaded_by(struct is_binary *bin)
{
	if (bin->fw)
		return 1;	/* by request_firmware */
	else if (bin->data)
		return 0;	/* from file system */
	else
		return -EINVAL;
}

/*
 * carve_binary_version: update the version of a binary to use in the future
 * @type: a binary type for IS
 * @hint: a hint to distinguish between same type binaries
 * @data: buffer of a loaded binary
 * @size: size of a loaded binary
 */
void carve_binary_version(enum is_bin_type type, unsigned int hint, struct is_binary *bin)
{
	int ofs, len;
	char *buf;

	if (type >= ARRAY_SIZE(bin_ver_info)) {
		pr_err("unknown type binary(%d)\n", type);
		return;
	}

	if (hint >= bin_ver_info[type].maxhint) {
		pr_warn("invalid hint(%d) to carved-out\n", hint);
		return;
	}

	ofs = bin->size - bin_ver_info[type].offset;
	if (ofs <= 0) {
		pr_warn("out of range offset(size: %zu <= offset: 0x%u)\n",
				bin->size, bin_ver_info[type].offset);
		return;
	}

	len = bin_ver_info[type].length;
	if ((ofs + len) > bin->size) {
		pr_warn("too long version length (binary: %zu < version: %d)\n",
						bin->size, (ofs + len));
		len -= ((ofs + len) - bin->size);
	}

	buf = bin_ver_info[type].get_buf(&bin_ver_info[type], hint);
	memcpy(buf, (char *)bin->data + ofs, len);
	buf[len] = '\0';

	info("%s version: %s\n",
		bin_names[bin_ver_info[type].get_name_idx(hint)], buf);
}

/*
 * get_binary_version: retrun the version string of a binary
 * @type: a binary type for IS
 * @hint: a hint to distinguish between same type binaries
 *	library: DDK(0), RTA(1)
 *	setfile: sensor position
 */
char *get_binary_version(enum is_bin_type type, unsigned int hint)
{
	if (type >= ARRAY_SIZE(bin_ver_info)) {
		pr_err("unknown type binary(%d)\n", type);
		return NULL;
	}

	if (hint >= bin_ver_info[type].maxhint) {
		pr_warn("invalid binary hint(%d)\n", hint);
		return NULL;
	}

	return bin_ver_info[type].get_buf(&bin_ver_info[type], hint);
}