summaryrefslogtreecommitdiffstats
path: root/drivers/base/firmware_loader/sysfs_upload.c
blob: 829270067d1632f92656859fb9143e3fa9635670 (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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// SPDX-License-Identifier: GPL-2.0

#include <linux/firmware.h>
#include <linux/module.h>
#include <linux/slab.h>

#include "sysfs_upload.h"

/*
 * Support for user-space to initiate a firmware upload to a device.
 */

static const char * const fw_upload_prog_str[] = {
	[FW_UPLOAD_PROG_IDLE]	      = "idle",
	[FW_UPLOAD_PROG_RECEIVING]    = "receiving",
	[FW_UPLOAD_PROG_PREPARING]    = "preparing",
	[FW_UPLOAD_PROG_TRANSFERRING] = "transferring",
	[FW_UPLOAD_PROG_PROGRAMMING]  = "programming"
};

static const char * const fw_upload_err_str[] = {
	[FW_UPLOAD_ERR_NONE]	     = "none",
	[FW_UPLOAD_ERR_HW_ERROR]     = "hw-error",
	[FW_UPLOAD_ERR_TIMEOUT]	     = "timeout",
	[FW_UPLOAD_ERR_CANCELED]     = "user-abort",
	[FW_UPLOAD_ERR_BUSY]	     = "device-busy",
	[FW_UPLOAD_ERR_INVALID_SIZE] = "invalid-file-size",
	[FW_UPLOAD_ERR_RW_ERROR]     = "read-write-error",
	[FW_UPLOAD_ERR_WEAROUT]	     = "flash-wearout",
	[FW_UPLOAD_ERR_FW_INVALID]   = "firmware-invalid",
};

static const char *fw_upload_progress(struct device *dev,
				      enum fw_upload_prog prog)
{
	const char *status = "unknown-status";

	if (prog < FW_UPLOAD_PROG_MAX)
		status = fw_upload_prog_str[prog];
	else
		dev_err(dev, "Invalid status during secure update: %d\n", prog);

	return status;
}

static const char *fw_upload_error(struct device *dev,
				   enum fw_upload_err err_code)
{
	const char *error = "unknown-error";

	if (err_code < FW_UPLOAD_ERR_MAX)
		error = fw_upload_err_str[err_code];
	else
		dev_err(dev, "Invalid error code during secure update: %d\n",
			err_code);

	return error;
}

static ssize_t
status_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct fw_upload_priv *fwlp = to_fw_sysfs(dev)->fw_upload_priv;

	return sysfs_emit(buf, "%s\n", fw_upload_progress(dev, fwlp->progress));
}
DEVICE_ATTR_RO(status);

static ssize_t
error_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct fw_upload_priv *fwlp = to_fw_sysfs(dev)->fw_upload_priv;
	int ret;

	mutex_lock(&fwlp->lock);

	if (fwlp->progress != FW_UPLOAD_PROG_IDLE)
		ret = -EBUSY;
	else if (!fwlp->err_code)
		ret = 0;
	else
		ret = sysfs_emit(buf, "%s:%s\n",
				 fw_upload_progress(dev, fwlp->err_progress),
				 fw_upload_error(dev, fwlp->err_code));

	mutex_unlock(&fwlp->lock);

	return ret;
}
DEVICE_ATTR_RO(error);

static ssize_t cancel_store(struct device *dev, struct device_attribute *attr,
			    const char *buf, size_t count)
{
	struct fw_upload_priv *fwlp = to_fw_sysfs(dev)->fw_upload_priv;
	int ret = count;
	bool cancel;

	if (kstrtobool(buf, &cancel) || !cancel)
		return -EINVAL;

	mutex_lock(&fwlp->lock);
	if (fwlp->progress == FW_UPLOAD_PROG_IDLE)
		ret = -ENODEV;

	fwlp->ops->cancel(fwlp->fw_upload);
	mutex_unlock(&fwlp->lock);

	return ret;
}
DEVICE_ATTR_WO(cancel);

static ssize_t remaining_size_show(struct device *dev,
				   struct device_attribute *attr, char *buf)
{
	struct fw_upload_priv *fwlp = to_fw_sysfs(dev)->fw_upload_priv;

	return sysfs_emit(buf, "%u\n", fwlp->remaining_size);
}
DEVICE_ATTR_RO(remaining_size);

umode_t
fw_upload_is_visible(struct kobject *kobj, struct attribute *attr, int n)
{
	static struct fw_sysfs *fw_sysfs;

	fw_sysfs = to_fw_sysfs(kobj_to_dev(kobj));

	if (fw_sysfs->fw_upload_priv || attr == &dev_attr_loading.attr)
		return attr->mode;

	return 0;
}

static void fw_upload_update_progress(struct fw_upload_priv *fwlp,
				      enum fw_upload_prog new_progress)
{
	mutex_lock(&fwlp->lock);
	fwlp->progress = new_progress;
	mutex_unlock(&fwlp->lock);
}

static void fw_upload_set_error(struct fw_upload_priv *fwlp,
				enum fw_upload_err err_code)
{
	mutex_lock(&fwlp->lock);
	fwlp->err_progress = fwlp->progress;
	fwlp->err_code = err_code;
	mutex_unlock(&fwlp->lock);
}

static void fw_upload_prog_complete(struct fw_upload_priv *fwlp)
{
	mutex_lock(&fwlp->lock);
	fwlp->progress = FW_UPLOAD_PROG_IDLE;
	mutex_unlock(&fwlp->lock);
}

static void fw_upload_main(struct work_struct *work)
{
	struct fw_upload_priv *fwlp;
	struct fw_sysfs *fw_sysfs;
	u32 written = 0, offset = 0;
	enum fw_upload_err ret;
	struct device *fw_dev;
	struct fw_upload *fwl;

	fwlp = container_of(work, struct fw_upload_priv, work);
	fwl = fwlp->fw_upload;
	fw_sysfs = (struct fw_sysfs *)fwl->priv;
	fw_dev = &fw_sysfs->dev;

	fw_upload_update_progress(fwlp, FW_UPLOAD_PROG_PREPARING);
	ret = fwlp->ops->prepare(fwl, fwlp->data, fwlp->remaining_size);
	if (ret != FW_UPLOAD_ERR_NONE) {
		fw_upload_set_error(fwlp, ret);
		goto putdev_exit;
	}

	fw_upload_update_progress(fwlp, FW_UPLOAD_PROG_TRANSFERRING);
	while (fwlp->remaining_size) {
		ret = fwlp->ops->write(fwl, fwlp->data, offset,
					fwlp->remaining_size, &written);
		if (ret != FW_UPLOAD_ERR_NONE || !written) {
			if (ret == FW_UPLOAD_ERR_NONE) {
				dev_warn(fw_dev, "write-op wrote zero data\n");
				ret = FW_UPLOAD_ERR_RW_ERROR;
			}
			fw_upload_set_error(fwlp, ret);
			goto done;
		}

		fwlp->remaining_size -= written;
		offset += written;
	}

	fw_upload_update_progress(fwlp, FW_UPLOAD_PROG_PROGRAMMING);
	ret = fwlp->ops->poll_complete(fwl);
	if (ret != FW_UPLOAD_ERR_NONE)
		fw_upload_set_error(fwlp, ret);

done:
	if (fwlp->ops->cleanup)
		fwlp->ops->cleanup(fwl);

putdev_exit:
	put_device(fw_dev->parent);

	/*
	 * Note: fwlp->remaining_size is left unmodified here to provide
	 * additional information on errors. It will be reinitialized when
	 * the next firmeware upload begins.
	 */
	mutex_lock(&fw_lock);
	fw_free_paged_buf(fw_sysfs->fw_priv);
	fw_state_init(fw_sysfs->fw_priv);
	mutex_unlock(&fw_lock);
	fwlp->data = NULL;
	fw_upload_prog_complete(fwlp);
}

/*
 * Start a worker thread to upload data to the parent driver.
 * Must be called with fw_lock held.
 */
int fw_upload_start(struct fw_sysfs *fw_sysfs)
{
	struct fw_priv *fw_priv = fw_sysfs->fw_priv;
	struct device *fw_dev = &fw_sysfs->dev;
	struct fw_upload_priv *fwlp;

	if (!fw_sysfs->fw_upload_priv)
		return 0;

	if (!fw_priv->size) {
		fw_free_paged_buf(fw_priv);
		fw_state_init(fw_sysfs->fw_priv);
		return 0;
	}

	fwlp = fw_sysfs->fw_upload_priv;
	mutex_lock(&fwlp->lock);

	/* Do not interfere with an on-going fw_upload */
	if (fwlp->progress != FW_UPLOAD_PROG_IDLE) {
		mutex_unlock(&fwlp->lock);
		return -EBUSY;
	}

	get_device(fw_dev->parent); /* released in fw_upload_main */

	fwlp->progress = FW_UPLOAD_PROG_RECEIVING;
	fwlp->err_code = 0;
	fwlp->remaining_size = fw_priv->size;
	fwlp->data = fw_priv->data;

	pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
		 __func__, fw_priv->fw_name,
		 fw_priv, fw_priv->data,
		 (unsigned int)fw_priv->size);

	queue_work(system_long_wq, &fwlp->work);
	mutex_unlock(&fwlp->lock);

	return 0;
}

void fw_upload_free(struct fw_sysfs *fw_sysfs)
{
	struct fw_upload_priv *fw_upload_priv = fw_sysfs->fw_upload_priv;

	free_fw_priv(fw_sysfs->fw_priv);
	kfree(fw_upload_priv->fw_upload);
	kfree(fw_upload_priv);
}

/**
 * firmware_upload_register() - register for the firmware upload sysfs API
 * @module: kernel module of this device
 * @parent: parent device instantiating firmware upload
 * @name: firmware name to be associated with this device
 * @ops: pointer to structure of firmware upload ops
 * @dd_handle: pointer to parent driver private data
 *
 *	@name must be unique among all users of firmware upload. The firmware
 *	sysfs files for this device will be found at /sys/class/firmware/@name.
 *
 *	Return: struct fw_upload pointer or ERR_PTR()
 *
 **/
struct fw_upload *
firmware_upload_register(struct module *module, struct device *parent,
			 const char *name, const struct fw_upload_ops *ops,
			 void *dd_handle)
{
	u32 opt_flags = FW_OPT_NOCACHE;
	struct fw_upload *fw_upload;
	struct fw_upload_priv *fw_upload_priv;
	struct fw_sysfs *fw_sysfs;
	struct fw_priv *fw_priv;
	struct device *fw_dev;
	int ret;

	if (!name || name[0] == '\0')
		return ERR_PTR(-EINVAL);

	if (!ops || !ops->cancel || !ops->prepare ||
	    !ops->write || !ops->poll_complete) {
		dev_err(parent, "Attempt to register without all required ops\n");
		return ERR_PTR(-EINVAL);
	}

	if (!try_module_get(module))
		return ERR_PTR(-EFAULT);

	fw_upload = kzalloc(sizeof(*fw_upload), GFP_KERNEL);
	if (!fw_upload) {
		ret = -ENOMEM;
		goto exit_module_put;
	}

	fw_upload_priv = kzalloc(sizeof(*fw_upload_priv), GFP_KERNEL);
	if (!fw_upload_priv) {
		ret = -ENOMEM;
		goto free_fw_upload;
	}

	fw_upload_priv->fw_upload = fw_upload;
	fw_upload_priv->ops = ops;
	mutex_init(&fw_upload_priv->lock);
	fw_upload_priv->module = module;
	fw_upload_priv->name = name;
	fw_upload_priv->err_code = 0;
	fw_upload_priv->progress = FW_UPLOAD_PROG_IDLE;
	INIT_WORK(&fw_upload_priv->work, fw_upload_main);
	fw_upload->dd_handle = dd_handle;

	fw_sysfs = fw_create_instance(NULL, name, parent, opt_flags);
	if (IS_ERR(fw_sysfs)) {
		ret = PTR_ERR(fw_sysfs);
		goto free_fw_upload_priv;
	}
	fw_upload->priv = fw_sysfs;
	fw_sysfs->fw_upload_priv = fw_upload_priv;
	fw_dev = &fw_sysfs->dev;

	ret = alloc_lookup_fw_priv(name, &fw_cache, &fw_priv,  NULL, 0, 0,
				   FW_OPT_NOCACHE);
	if (ret != 0) {
		if (ret > 0)
			ret = -EINVAL;
		goto free_fw_sysfs;
	}
	fw_priv->is_paged_buf = true;
	fw_sysfs->fw_priv = fw_priv;

	ret = device_add(fw_dev);
	if (ret) {
		dev_err(fw_dev, "%s: device_register failed\n", __func__);
		put_device(fw_dev);
		goto exit_module_put;
	}

	return fw_upload;

free_fw_sysfs:
	kfree(fw_sysfs);

free_fw_upload_priv:
	kfree(fw_upload_priv);

free_fw_upload:
	kfree(fw_upload);

exit_module_put:
	module_put(module);

	return ERR_PTR(ret);
}
EXPORT_SYMBOL_GPL(firmware_upload_register);

/**
 * firmware_upload_unregister() - Unregister firmware upload interface
 * @fw_upload: pointer to struct fw_upload
 **/
void firmware_upload_unregister(struct fw_upload *fw_upload)
{
	struct fw_sysfs *fw_sysfs = fw_upload->priv;
	struct fw_upload_priv *fw_upload_priv = fw_sysfs->fw_upload_priv;
	struct module *module = fw_upload_priv->module;

	mutex_lock(&fw_upload_priv->lock);
	if (fw_upload_priv->progress == FW_UPLOAD_PROG_IDLE) {
		mutex_unlock(&fw_upload_priv->lock);
		goto unregister;
	}

	fw_upload_priv->ops->cancel(fw_upload);
	mutex_unlock(&fw_upload_priv->lock);

	/* Ensure lower-level device-driver is finished */
	flush_work(&fw_upload_priv->work);

unregister:
	device_unregister(&fw_sysfs->dev);
	module_put(module);
}
EXPORT_SYMBOL_GPL(firmware_upload_unregister);