Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 503886
Collapse All | Expand All

(-)linux-3.4.83/include/linux/firmware.h (+15 lines)
Lines 12-17 Link Here
12
	size_t size;
12
	size_t size;
13
	const u8 *data;
13
	const u8 *data;
14
	struct page **pages;
14
	struct page **pages;
15
16
	/* firmware loader private fields */
17
	void *priv;
15
};
18
};
16
19
17
struct module;
20
struct module;
Lines 44-49 Link Here
44
	void (*cont)(const struct firmware *fw, void *context));
47
	void (*cont)(const struct firmware *fw, void *context));
45
48
46
void release_firmware(const struct firmware *fw);
49
void release_firmware(const struct firmware *fw);
50
int cache_firmware(const char *name);
51
int uncache_firmware(const char *name);
47
#else
52
#else
48
static inline int request_firmware(const struct firmware **fw,
53
static inline int request_firmware(const struct firmware **fw,
49
				   const char *name,
54
				   const char *name,
Lines 62-67 Link Here
62
static inline void release_firmware(const struct firmware *fw)
67
static inline void release_firmware(const struct firmware *fw)
63
{
68
{
64
}
69
}
70
71
static inline int cache_firmware(const char *name)
72
{
73
	return -ENOENT;
74
}
75
76
static inline int uncache_firmware(const char *name)
77
{
78
	return -EINVAL;
79
}
65
#endif
80
#endif
66
81
67
#endif
82
#endif
(-)linux-3.4.83/drivers/base/firmware_class.c (-189 / +1013 lines)
Lines 21-28 Link Here
21
#include <linux/firmware.h>
21
#include <linux/firmware.h>
22
#include <linux/slab.h>
22
#include <linux/slab.h>
23
#include <linux/sched.h>
23
#include <linux/sched.h>
24
#include <linux/file.h>
25
#include <linux/list.h>
26
#include <linux/async.h>
27
#include <linux/pm.h>
28
#include <linux/suspend.h>
29
#include <linux/syscore_ops.h>
24
30
25
#define to_dev(obj) container_of(obj, struct device, kobj)
31
#define to_dev(obj) container_of(obj, struct device, kobj)
32
#include <generated/utsrelease.h>
33
34
#include "base.h"
26
35
27
MODULE_AUTHOR("Manuel Estrada Sainz");
36
MODULE_AUTHOR("Manuel Estrada Sainz");
28
MODULE_DESCRIPTION("Multi purpose firmware loading support");
37
MODULE_DESCRIPTION("Multi purpose firmware loading support");
Lines 87-109 Link Here
87
	return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
96
	return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
88
}
97
}
89
98
90
/* fw_lock could be moved to 'struct firmware_priv' but since it is just
99
struct firmware_cache {
91
 * guarding for corner cases a global lock should be OK */
100
	/* firmware_buf instance will be added into the below list */
92
static DEFINE_MUTEX(fw_lock);
101
	spinlock_t lock;
102
	struct list_head head;
103
	int state;
104
105
#ifdef CONFIG_PM_SLEEP
106
	/*
107
	 * Names of firmware images which have been cached successfully
108
	 * will be added into the below list so that device uncache
109
	 * helper can trace which firmware images have been cached
110
	 * before.
111
	 */
112
	spinlock_t name_lock;
113
	struct list_head fw_names;
93
114
94
struct firmware_priv {
115
	struct delayed_work work;
116
117
	struct notifier_block   pm_notify;
118
#endif
119
};
120
121
struct firmware_buf {
122
	struct kref ref;
123
	struct list_head list;
95
	struct completion completion;
124
	struct completion completion;
96
	struct firmware *fw;
125
	struct firmware_cache *fwc;
97
	unsigned long status;
126
	unsigned long status;
127
	void *data;
128
	size_t size;
129
#ifdef CONFIG_FW_LOADER_USER_HELPER
130
	bool is_paged_buf;
98
	struct page **pages;
131
	struct page **pages;
99
	int nr_pages;
132
	int nr_pages;
100
	int page_array_size;
133
	int page_array_size;
101
	struct timer_list timeout;
134
#endif
102
	struct device dev;
103
	bool nowait;
104
	char fw_id[];
135
	char fw_id[];
105
};
136
};
106
137
138
struct fw_cache_entry {
139
	struct list_head list;
140
	char name[];
141
};
142
143
struct fw_name_devm {
144
	unsigned long magic;
145
	char name[];
146
};
147
148
#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
149
150
#define	FW_LOADER_NO_CACHE	0
151
#define	FW_LOADER_START_CACHE	1
152
153
static int fw_cache_piggyback_on_request(const char *name);
154
155
/* fw_lock could be moved to 'struct firmware_priv' but since it is just
156
 * guarding for corner cases a global lock should be OK */
157
static DEFINE_MUTEX(fw_lock);
158
159
static struct firmware_cache fw_cache;
160
161
static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
162
					      struct firmware_cache *fwc)
163
{
164
	struct firmware_buf *buf;
165
166
	buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
167
168
	if (!buf)
169
		return buf;
170
171
	kref_init(&buf->ref);
172
	strcpy(buf->fw_id, fw_name);
173
	buf->fwc = fwc;
174
	init_completion(&buf->completion);
175
176
	pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
177
178
	return buf;
179
}
180
181
static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
182
{
183
	struct firmware_buf *tmp;
184
	struct firmware_cache *fwc = &fw_cache;
185
186
	list_for_each_entry(tmp, &fwc->head, list)
187
		if (!strcmp(tmp->fw_id, fw_name))
188
			return tmp;
189
	return NULL;
190
}
191
192
static int fw_lookup_and_allocate_buf(const char *fw_name,
193
				      struct firmware_cache *fwc,
194
				      struct firmware_buf **buf)
195
{
196
	struct firmware_buf *tmp;
197
198
	spin_lock(&fwc->lock);
199
	tmp = __fw_lookup_buf(fw_name);
200
	if (tmp) {
201
		kref_get(&tmp->ref);
202
		spin_unlock(&fwc->lock);
203
		*buf = tmp;
204
		return 1;
205
	}
206
	tmp = __allocate_fw_buf(fw_name, fwc);
207
	if (tmp)
208
		list_add(&tmp->list, &fwc->head);
209
	spin_unlock(&fwc->lock);
210
211
	*buf = tmp;
212
213
	return tmp ? 0 : -ENOMEM;
214
}
215
216
static struct firmware_buf *fw_lookup_buf(const char *fw_name)
217
{
218
	struct firmware_buf *tmp;
219
	struct firmware_cache *fwc = &fw_cache;
220
221
	spin_lock(&fwc->lock);
222
	tmp = __fw_lookup_buf(fw_name);
223
	spin_unlock(&fwc->lock);
224
225
	return tmp;
226
}
227
228
static void __fw_free_buf(struct kref *ref)
229
{
230
	struct firmware_buf *buf = to_fwbuf(ref);
231
	struct firmware_cache *fwc = buf->fwc;
232
233
	pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
234
		 __func__, buf->fw_id, buf, buf->data,
235
		 (unsigned int)buf->size);
236
237
	list_del(&buf->list);
238
	spin_unlock(&fwc->lock);
239
240
#ifdef CONFIG_FW_LOADER_USER_HELPER
241
	if (buf->is_paged_buf) {
242
		int i;
243
		vunmap(buf->data);
244
		for (i = 0; i < buf->nr_pages; i++)
245
			__free_page(buf->pages[i]);
246
		kfree(buf->pages);
247
	} else
248
#endif
249
		vfree(buf->data);
250
	kfree(buf);
251
}
252
253
static void fw_free_buf(struct firmware_buf *buf)
254
{
255
	struct firmware_cache *fwc = buf->fwc;
256
	spin_lock(&fwc->lock);
257
	if (!kref_put(&buf->ref, __fw_free_buf))
258
		spin_unlock(&fwc->lock);
259
}
260
261
/* direct firmware loading support */
262
static char fw_path_para[256];
263
static const char * const fw_path[] = {
264
	fw_path_para,
265
	"/lib/firmware/updates/" UTS_RELEASE,
266
	"/lib/firmware/updates",
267
	"/lib/firmware/" UTS_RELEASE,
268
	"/lib/firmware"
269
};
270
271
/*
272
 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
273
 * from kernel command line because firmware_class is generally built in
274
 * kernel instead of module.
275
 */
276
module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
277
MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
278
279
/* Don't inline this: 'struct kstat' is biggish */
280
static noinline_for_stack long fw_file_size(struct file *file)
281
{
282
	struct kstat st;
283
	if (vfs_getattr(file->f_path.mnt, file->f_path.dentry, &st))
284
		return -1;
285
	if (!S_ISREG(st.mode))
286
		return -1;
287
	if (st.size != (long)st.size)
288
		return -1;
289
	return st.size;
290
}
291
292
static bool fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf)
293
{
294
	long size;
295
	char *buf;
296
297
	size = fw_file_size(file);
298
	if (size <= 0)
299
		return false;
300
	buf = vmalloc(size);
301
	if (!buf)
302
		return false;
303
	if (kernel_read(file, 0, buf, size) != size) {
304
		vfree(buf);
305
		return false;
306
	}
307
	fw_buf->data = buf;
308
	fw_buf->size = size;
309
	return true;
310
}
311
312
static bool fw_get_filesystem_firmware(struct device *device,
313
				       struct firmware_buf *buf)
314
{
315
	int i;
316
	bool success = false;
317
	char *path = __getname();
318
319
	for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
320
		struct file *file;
321
322
		/* skip the unset customized path */
323
		if (!fw_path[i][0])
324
			continue;
325
326
		snprintf(path, PATH_MAX, "%s/%s", fw_path[i], buf->fw_id);
327
328
		file = filp_open(path, O_RDONLY, 0);
329
		if (IS_ERR(file))
330
			continue;
331
		success = fw_read_file_contents(file, buf);
332
		fput(file);
333
		if (success)
334
			break;
335
	}
336
	__putname(path);
337
338
	if (success) {
339
		dev_dbg(device, "firmware: direct-loading firmware %s\n",
340
			buf->fw_id);
341
		mutex_lock(&fw_lock);
342
		set_bit(FW_STATUS_DONE, &buf->status);
343
		complete_all(&buf->completion);
344
		mutex_unlock(&fw_lock);
345
	}
346
347
	return success;
348
}
349
350
/* firmware holds the ownership of pages */
351
static void firmware_free_data(const struct firmware *fw)
352
{
353
	/* Loaded directly? */
354
	if (!fw->priv) {
355
		vfree(fw->data);
356
		return;
357
	}
358
	fw_free_buf(fw->priv);
359
}
360
361
/* store the pages buffer info firmware from buf */
362
static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
363
{
364
	fw->priv = buf;
365
#ifdef CONFIG_FW_LOADER_USER_HELPER
366
	fw->pages = buf->pages;
367
#endif
368
	fw->size = buf->size;
369
	fw->data = buf->data;
370
371
	pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
372
		 __func__, buf->fw_id, buf, buf->data,
373
		 (unsigned int)buf->size);
374
}
375
376
#ifdef CONFIG_PM_SLEEP
377
static void fw_name_devm_release(struct device *dev, void *res)
378
{
379
	struct fw_name_devm *fwn = res;
380
381
	if (fwn->magic == (unsigned long)&fw_cache)
382
		pr_debug("%s: fw_name-%s devm-%p released\n",
383
				__func__, fwn->name, res);
384
}
385
386
static int fw_devm_match(struct device *dev, void *res,
387
		void *match_data)
388
{
389
	struct fw_name_devm *fwn = res;
390
391
	return (fwn->magic == (unsigned long)&fw_cache) &&
392
		!strcmp(fwn->name, match_data);
393
}
394
395
static struct fw_name_devm *fw_find_devm_name(struct device *dev,
396
		const char *name)
397
{
398
	struct fw_name_devm *fwn;
399
400
	fwn = devres_find(dev, fw_name_devm_release,
401
			  fw_devm_match, (void *)name);
402
	return fwn;
403
}
404
405
/* add firmware name into devres list */
406
static int fw_add_devm_name(struct device *dev, const char *name)
407
{
408
	struct fw_name_devm *fwn;
409
410
	fwn = fw_find_devm_name(dev, name);
411
	if (fwn)
412
		return 1;
413
414
	fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
415
			   strlen(name) + 1, GFP_KERNEL);
416
	if (!fwn)
417
		return -ENOMEM;
418
419
	fwn->magic = (unsigned long)&fw_cache;
420
	strcpy(fwn->name, name);
421
	devres_add(dev, fwn);
422
423
	return 0;
424
}
425
#else
426
static int fw_add_devm_name(struct device *dev, const char *name)
427
{
428
	return 0;
429
}
430
#endif
431
432
433
/*
434
 * user-mode helper code
435
 */
436
#ifdef CONFIG_FW_LOADER_USER_HELPER
437
struct firmware_priv {
438
	struct delayed_work timeout_work;
439
	bool nowait;
440
	struct device dev;
441
	struct firmware_buf *buf;
442
	struct firmware *fw;
443
};
444
107
static struct firmware_priv *to_firmware_priv(struct device *dev)
445
static struct firmware_priv *to_firmware_priv(struct device *dev)
108
{
446
{
109
	return container_of(dev, struct firmware_priv, dev);
447
	return container_of(dev, struct firmware_priv, dev);
Lines 111-121 Link Here
111
449
112
static void fw_load_abort(struct firmware_priv *fw_priv)
450
static void fw_load_abort(struct firmware_priv *fw_priv)
113
{
451
{
114
	set_bit(FW_STATUS_ABORT, &fw_priv->status);
452
	struct firmware_buf *buf = fw_priv->buf;
115
	wmb();
453
116
	complete(&fw_priv->completion);
454
	/*
455
	 * There is a small window in which user can write to 'loading'
456
	 * between loading done and disappearance of 'loading'
457
	 */
458
	if (test_bit(FW_STATUS_DONE, &buf->status))
459
		return;
460
461
	set_bit(FW_STATUS_ABORT, &buf->status);
462
	complete_all(&buf->completion);
463
464
	/* avoid user action after loading abort */
465
	fw_priv->buf = NULL;
117
}
466
}
118
467
468
#define is_fw_load_aborted(buf)	\
469
	test_bit(FW_STATUS_ABORT, &(buf)->status)
470
119
static ssize_t firmware_timeout_show(struct class *class,
471
static ssize_t firmware_timeout_show(struct class *class,
120
				     struct class_attribute *attr,
472
				     struct class_attribute *attr,
121
				     char *buf)
473
				     char *buf)
Lines 156-166 Link Here
156
static void fw_dev_release(struct device *dev)
508
static void fw_dev_release(struct device *dev)
157
{
509
{
158
	struct firmware_priv *fw_priv = to_firmware_priv(dev);
510
	struct firmware_priv *fw_priv = to_firmware_priv(dev);
159
	int i;
160
511
161
	for (i = 0; i < fw_priv->nr_pages; i++)
162
		__free_page(fw_priv->pages[i]);
163
	kfree(fw_priv->pages);
164
	kfree(fw_priv);
512
	kfree(fw_priv);
165
513
166
	module_put(THIS_MODULE);
514
	module_put(THIS_MODULE);
Lines 170-176 Link Here
170
{
518
{
171
	struct firmware_priv *fw_priv = to_firmware_priv(dev);
519
	struct firmware_priv *fw_priv = to_firmware_priv(dev);
172
520
173
	if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id))
521
	if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
174
		return -ENOMEM;
522
		return -ENOMEM;
175
	if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
523
	if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
176
		return -ENOMEM;
524
		return -ENOMEM;
Lines 191-216 Link Here
191
				     struct device_attribute *attr, char *buf)
539
				     struct device_attribute *attr, char *buf)
192
{
540
{
193
	struct firmware_priv *fw_priv = to_firmware_priv(dev);
541
	struct firmware_priv *fw_priv = to_firmware_priv(dev);
194
	int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
542
	int loading = 0;
195
543
196
	return sprintf(buf, "%d\n", loading);
544
	mutex_lock(&fw_lock);
197
}
545
	if (fw_priv->buf)
546
		loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
547
	mutex_unlock(&fw_lock);
198
548
199
static void firmware_free_data(const struct firmware *fw)
549
	return sprintf(buf, "%d\n", loading);
200
{
201
	int i;
202
	vunmap(fw->data);
203
	if (fw->pages) {
204
		for (i = 0; i < PFN_UP(fw->size); i++)
205
			__free_page(fw->pages[i]);
206
		kfree(fw->pages);
207
	}
208
}
550
}
209
551
210
/* Some architectures don't have PAGE_KERNEL_RO */
552
/* Some architectures don't have PAGE_KERNEL_RO */
211
#ifndef PAGE_KERNEL_RO
553
#ifndef PAGE_KERNEL_RO
212
#define PAGE_KERNEL_RO PAGE_KERNEL
554
#define PAGE_KERNEL_RO PAGE_KERNEL
213
#endif
555
#endif
556
557
/* one pages buffer should be mapped/unmapped only once */
558
static int fw_map_pages_buf(struct firmware_buf *buf)
559
{
560
	if (!buf->is_paged_buf)
561
		return 0;
562
563
	if (buf->data)
564
		vunmap(buf->data);
565
	buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
566
	if (!buf->data)
567
		return -ENOMEM;
568
	return 0;
569
}
570
214
/**
571
/**
215
 * firmware_loading_store - set value in the 'loading' control file
572
 * firmware_loading_store - set value in the 'loading' control file
216
 * @dev: device pointer
573
 * @dev: device pointer
Lines 229-273 Link Here
229
				      const char *buf, size_t count)
586
				      const char *buf, size_t count)
230
{
587
{
231
	struct firmware_priv *fw_priv = to_firmware_priv(dev);
588
	struct firmware_priv *fw_priv = to_firmware_priv(dev);
589
	struct firmware_buf *fw_buf;
232
	int loading = simple_strtol(buf, NULL, 10);
590
	int loading = simple_strtol(buf, NULL, 10);
233
	int i;
591
	int i;
234
592
235
	mutex_lock(&fw_lock);
593
	mutex_lock(&fw_lock);
236
594
	fw_buf = fw_priv->buf;
237
	if (!fw_priv->fw)
595
	if (!fw_buf)
238
		goto out;
596
		goto out;
239
597
240
	switch (loading) {
598
	switch (loading) {
241
	case 1:
599
	case 1:
242
		firmware_free_data(fw_priv->fw);
600
		/* discarding any previous partial load */
243
		memset(fw_priv->fw, 0, sizeof(struct firmware));
601
		if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
244
		/* If the pages are not owned by 'struct firmware' */
602
			for (i = 0; i < fw_buf->nr_pages; i++)
245
		for (i = 0; i < fw_priv->nr_pages; i++)
603
				__free_page(fw_buf->pages[i]);
246
			__free_page(fw_priv->pages[i]);
604
			kfree(fw_buf->pages);
247
		kfree(fw_priv->pages);
605
			fw_buf->pages = NULL;
248
		fw_priv->pages = NULL;
606
			fw_buf->page_array_size = 0;
249
		fw_priv->page_array_size = 0;
607
			fw_buf->nr_pages = 0;
250
		fw_priv->nr_pages = 0;
608
			set_bit(FW_STATUS_LOADING, &fw_buf->status);
251
		set_bit(FW_STATUS_LOADING, &fw_priv->status);
609
		}
252
		break;
610
		break;
253
	case 0:
611
	case 0:
254
		if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
612
		if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
255
			vunmap(fw_priv->fw->data);
613
			set_bit(FW_STATUS_DONE, &fw_buf->status);
256
			fw_priv->fw->data = vmap(fw_priv->pages,
614
			clear_bit(FW_STATUS_LOADING, &fw_buf->status);
257
						 fw_priv->nr_pages,
615
258
						 0, PAGE_KERNEL_RO);
616
			/*
259
			if (!fw_priv->fw->data) {
617
			 * Several loading requests may be pending on
260
				dev_err(dev, "%s: vmap() failed\n", __func__);
618
			 * one same firmware buf, so let all requests
261
				goto err;
619
			 * see the mapped 'buf->data' once the loading
262
			}
620
			 * is completed.
263
			/* Pages are now owned by 'struct firmware' */
621
			 * */
264
			fw_priv->fw->pages = fw_priv->pages;
622
			fw_map_pages_buf(fw_buf);
265
			fw_priv->pages = NULL;
623
			complete_all(&fw_buf->completion);
266
267
			fw_priv->page_array_size = 0;
268
			fw_priv->nr_pages = 0;
269
			complete(&fw_priv->completion);
270
			clear_bit(FW_STATUS_LOADING, &fw_priv->status);
271
			break;
624
			break;
272
		}
625
		}
273
		/* fallthrough */
626
		/* fallthrough */
Lines 275-281 Link Here
275
		dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
628
		dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
276
		/* fallthrough */
629
		/* fallthrough */
277
	case -1:
630
	case -1:
278
	err:
279
		fw_load_abort(fw_priv);
631
		fw_load_abort(fw_priv);
280
		break;
632
		break;
281
	}
633
	}
Lines 290-312 Link Here
290
				  struct bin_attribute *bin_attr,
642
				  struct bin_attribute *bin_attr,
291
				  char *buffer, loff_t offset, size_t count)
643
				  char *buffer, loff_t offset, size_t count)
292
{
644
{
293
	struct device *dev = to_dev(kobj);
645
	struct device *dev = to_dev(kobj);
294
	struct firmware_priv *fw_priv = to_firmware_priv(dev);
646
	struct firmware_priv *fw_priv = to_firmware_priv(dev);
295
	struct firmware *fw;
647
	struct firmware_buf *buf;
296
	ssize_t ret_count;
648
	ssize_t ret_count;
297
649
298
	mutex_lock(&fw_lock);
650
	mutex_lock(&fw_lock);
299
	fw = fw_priv->fw;
651
	buf = fw_priv->buf;
300
	if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
652
	if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
301
		ret_count = -ENODEV;
653
		ret_count = -ENODEV;
302
		goto out;
654
		goto out;
303
	}
655
	}
304
	if (offset > fw->size) {
656
	if (offset > buf->size) {
305
		ret_count = 0;
657
		ret_count = 0;
306
		goto out;
658
		goto out;
307
	}
659
	}
308
	if (count > fw->size - offset)
660
	if (count > buf->size - offset)
309
		count = fw->size - offset;
661
		count = buf->size - offset;
310
662
311
	ret_count = count;
663
	ret_count = count;
312
664
Lines 316-326 Link Here
316
		int page_ofs = offset & (PAGE_SIZE-1);
668
		int page_ofs = offset & (PAGE_SIZE-1);
317
		int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
669
		int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
318
670
319
		page_data = kmap(fw_priv->pages[page_nr]);
671
		page_data = kmap(buf->pages[page_nr]);
320
672
321
		memcpy(buffer, page_data + page_ofs, page_cnt);
673
		memcpy(buffer, page_data + page_ofs, page_cnt);
322
674
323
		kunmap(fw_priv->pages[page_nr]);
675
		kunmap(buf->pages[page_nr]);
324
		buffer += page_cnt;
676
		buffer += page_cnt;
325
		offset += page_cnt;
677
		offset += page_cnt;
326
		count -= page_cnt;
678
		count -= page_cnt;
Lines 332-343 Link Here
332
684
333
static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
685
static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
334
{
686
{
687
	struct firmware_buf *buf = fw_priv->buf;
335
	int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
688
	int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
336
689
337
	/* If the array of pages is too small, grow it... */
690
	/* If the array of pages is too small, grow it... */
338
	if (fw_priv->page_array_size < pages_needed) {
691
	if (buf->page_array_size < pages_needed) {
339
		int new_array_size = max(pages_needed,
692
		int new_array_size = max(pages_needed,
340
					 fw_priv->page_array_size * 2);
693
					 buf->page_array_size * 2);
341
		struct page **new_pages;
694
		struct page **new_pages;
342
695
343
		new_pages = kmalloc(new_array_size * sizeof(void *),
696
		new_pages = kmalloc(new_array_size * sizeof(void *),
Lines 346-369 Link Here
346
			fw_load_abort(fw_priv);
699
			fw_load_abort(fw_priv);
347
			return -ENOMEM;
700
			return -ENOMEM;
348
		}
701
		}
349
		memcpy(new_pages, fw_priv->pages,
702
		memcpy(new_pages, buf->pages,
350
		       fw_priv->page_array_size * sizeof(void *));
703
		       buf->page_array_size * sizeof(void *));
351
		memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
704
		memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
352
		       (new_array_size - fw_priv->page_array_size));
705
		       (new_array_size - buf->page_array_size));
353
		kfree(fw_priv->pages);
706
		kfree(buf->pages);
354
		fw_priv->pages = new_pages;
707
		buf->pages = new_pages;
355
		fw_priv->page_array_size = new_array_size;
708
		buf->page_array_size = new_array_size;
356
	}
709
	}
357
710
358
	while (fw_priv->nr_pages < pages_needed) {
711
	while (buf->nr_pages < pages_needed) {
359
		fw_priv->pages[fw_priv->nr_pages] =
712
		buf->pages[buf->nr_pages] =
360
			alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
713
			alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
361
714
362
		if (!fw_priv->pages[fw_priv->nr_pages]) {
715
		if (!buf->pages[buf->nr_pages]) {
363
			fw_load_abort(fw_priv);
716
			fw_load_abort(fw_priv);
364
			return -ENOMEM;
717
			return -ENOMEM;
365
		}
718
		}
366
		fw_priv->nr_pages++;
719
		buf->nr_pages++;
367
	}
720
	}
368
	return 0;
721
	return 0;
369
}
722
}
Lines 384-403 Link Here
384
				   struct bin_attribute *bin_attr,
737
				   struct bin_attribute *bin_attr,
385
				   char *buffer, loff_t offset, size_t count)
738
				   char *buffer, loff_t offset, size_t count)
386
{
739
{
387
	struct device *dev = to_dev(kobj);
740
	struct device *dev = to_dev(kobj);
388
	struct firmware_priv *fw_priv = to_firmware_priv(dev);
741
	struct firmware_priv *fw_priv = to_firmware_priv(dev);
389
	struct firmware *fw;
742
	struct firmware_buf *buf;
390
	ssize_t retval;
743
	ssize_t retval;
391
744
392
	if (!capable(CAP_SYS_RAWIO))
745
	if (!capable(CAP_SYS_RAWIO))
393
		return -EPERM;
746
		return -EPERM;
394
747
395
	mutex_lock(&fw_lock);
748
	mutex_lock(&fw_lock);
396
	fw = fw_priv->fw;
749
	buf = fw_priv->buf;
397
	if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
750
	if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
398
		retval = -ENODEV;
751
		retval = -ENODEV;
399
		goto out;
752
		goto out;
400
	}
753
	}
754
401
	retval = fw_realloc_buffer(fw_priv, offset + count);
755
	retval = fw_realloc_buffer(fw_priv, offset + count);
402
	if (retval)
756
	if (retval)
403
		goto out;
757
		goto out;
Lines 410-426 Link Here
410
		int page_ofs = offset & (PAGE_SIZE - 1);
764
		int page_ofs = offset & (PAGE_SIZE - 1);
411
		int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
765
		int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
412
766
413
		page_data = kmap(fw_priv->pages[page_nr]);
767
		page_data = kmap(buf->pages[page_nr]);
414
768
415
		memcpy(page_data + page_ofs, buffer, page_cnt);
769
		memcpy(page_data + page_ofs, buffer, page_cnt);
416
770
417
		kunmap(fw_priv->pages[page_nr]);
771
		kunmap(buf->pages[page_nr]);
418
		buffer += page_cnt;
772
		buffer += page_cnt;
419
		offset += page_cnt;
773
		offset += page_cnt;
420
		count -= page_cnt;
774
		count -= page_cnt;
421
	}
775
	}
422
776
423
	fw->size = max_t(size_t, offset, fw->size);
777
	buf->size = max_t(size_t, offset, buf->size);
424
out:
778
out:
425
	mutex_unlock(&fw_lock);
779
	mutex_unlock(&fw_lock);
426
	return retval;
780
	return retval;
Lines 433-443 Link Here
433
	.write = firmware_data_write,
787
	.write = firmware_data_write,
434
};
788
};
435
789
436
static void firmware_class_timeout(u_long data)
790
static void firmware_class_timeout_work(struct work_struct *work)
437
{
791
{
438
	struct firmware_priv *fw_priv = (struct firmware_priv *) data;
792
	struct firmware_priv *fw_priv = container_of(work,
793
			struct firmware_priv, timeout_work.work);
439
794
795
	mutex_lock(&fw_lock);
440
	fw_load_abort(fw_priv);
796
	fw_load_abort(fw_priv);
797
	mutex_unlock(&fw_lock);
441
}
798
}
442
799
443
static struct firmware_priv *
800
static struct firmware_priv *
Lines 447-516 Link Here
447
	struct firmware_priv *fw_priv;
804
	struct firmware_priv *fw_priv;
448
	struct device *f_dev;
805
	struct device *f_dev;
449
806
450
	fw_priv = kzalloc(sizeof(*fw_priv) + strlen(fw_name) + 1 , GFP_KERNEL);
807
	fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
451
	if (!fw_priv) {
808
	if (!fw_priv) {
452
		dev_err(device, "%s: kmalloc failed\n", __func__);
809
		dev_err(device, "%s: kmalloc failed\n", __func__);
453
		return ERR_PTR(-ENOMEM);
810
		fw_priv = ERR_PTR(-ENOMEM);
811
		goto exit;
454
	}
812
	}
455
813
456
	fw_priv->fw = firmware;
457
	fw_priv->nowait = nowait;
814
	fw_priv->nowait = nowait;
458
	strcpy(fw_priv->fw_id, fw_name);
815
	fw_priv->fw = firmware;
459
	init_completion(&fw_priv->completion);
816
	INIT_DELAYED_WORK(&fw_priv->timeout_work,
460
	setup_timer(&fw_priv->timeout,
817
		firmware_class_timeout_work);
461
		    firmware_class_timeout, (u_long) fw_priv);
462
818
463
	f_dev = &fw_priv->dev;
819
	f_dev = &fw_priv->dev;
464
820
465
	device_initialize(f_dev);
821
	device_initialize(f_dev);
466
	dev_set_name(f_dev, "%s", dev_name(device));
822
	dev_set_name(f_dev, "%s", fw_name);
467
	f_dev->parent = device;
823
	f_dev->parent = device;
468
	f_dev->class = &firmware_class;
824
	f_dev->class = &firmware_class;
469
825
exit:
470
	return fw_priv;
826
	return fw_priv;
471
}
827
}
472
828
473
static struct firmware_priv *
829
/* load a firmware via user helper */
474
_request_firmware_prepare(const struct firmware **firmware_p, const char *name,
475
			  struct device *device, bool uevent, bool nowait)
476
{
477
	struct firmware *firmware;
478
	struct firmware_priv *fw_priv;
479
480
	if (!firmware_p)
481
		return ERR_PTR(-EINVAL);
482
483
	*firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
484
	if (!firmware) {
485
		dev_err(device, "%s: kmalloc(struct firmware) failed\n",
486
			__func__);
487
		return ERR_PTR(-ENOMEM);
488
	}
489
490
	if (fw_get_builtin_firmware(firmware, name)) {
491
		dev_dbg(device, "firmware: using built-in firmware %s\n", name);
492
		return NULL;
493
	}
494
495
	fw_priv = fw_create_instance(firmware, name, device, uevent, nowait);
496
	if (IS_ERR(fw_priv)) {
497
		release_firmware(firmware);
498
		*firmware_p = NULL;
499
	}
500
	return fw_priv;
501
}
502
503
static void _request_firmware_cleanup(const struct firmware **firmware_p)
504
{
505
	release_firmware(*firmware_p);
506
	*firmware_p = NULL;
507
}
508
509
static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
830
static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
510
				  long timeout)
831
				  long timeout)
511
{
832
{
512
	int retval = 0;
833
	int retval = 0;
513
	struct device *f_dev = &fw_priv->dev;
834
	struct device *f_dev = &fw_priv->dev;
835
	struct firmware_buf *buf = fw_priv->buf;
836
837
	/* fall back on userspace loading */
838
	buf->is_paged_buf = true;
514
839
515
	dev_set_uevent_suppress(f_dev, true);
840
	dev_set_uevent_suppress(f_dev, true);
516
841
Lines 537-560 Link Here
537
862
538
	if (uevent) {
863
	if (uevent) {
539
		dev_set_uevent_suppress(f_dev, false);
864
		dev_set_uevent_suppress(f_dev, false);
540
		dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_id);
865
		dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
541
		if (timeout != MAX_SCHEDULE_TIMEOUT)
866
		if (timeout != MAX_SCHEDULE_TIMEOUT)
542
			mod_timer(&fw_priv->timeout,
867
			schedule_delayed_work(&fw_priv->timeout_work, timeout);
543
				  round_jiffies_up(jiffies + timeout));
544
868
545
		kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
869
		kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
546
	}
870
	}
547
871
548
	wait_for_completion(&fw_priv->completion);
872
	wait_for_completion(&buf->completion);
549
873
550
	set_bit(FW_STATUS_DONE, &fw_priv->status);
874
	cancel_delayed_work_sync(&fw_priv->timeout_work);
551
	del_timer_sync(&fw_priv->timeout);
552
553
	mutex_lock(&fw_lock);
554
	if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status))
555
		retval = -ENOENT;
556
	fw_priv->fw = NULL;
557
	mutex_unlock(&fw_lock);
558
875
559
	device_remove_file(f_dev, &dev_attr_loading);
876
	device_remove_file(f_dev, &dev_attr_loading);
560
err_del_bin_attr:
877
err_del_bin_attr:
Lines 566-571 Link Here
566
	return retval;
883
	return retval;
567
}
884
}
568
885
886
static int fw_load_from_user_helper(struct firmware *firmware,
887
				    const char *name, struct device *device,
888
				    bool uevent, bool nowait, long timeout)
889
{
890
	struct firmware_priv *fw_priv;
891
892
	fw_priv = fw_create_instance(firmware, name, device, uevent, nowait);
893
	if (IS_ERR(fw_priv))
894
		return PTR_ERR(fw_priv);
895
896
	fw_priv->buf = firmware->priv;
897
	return _request_firmware_load(fw_priv, uevent, timeout);
898
}
899
#else /* CONFIG_FW_LOADER_USER_HELPER */
900
static inline int
901
fw_load_from_user_helper(struct firmware *firmware, const char *name,
902
			 struct device *device, bool uevent, bool nowait,
903
			 long timeout)
904
{
905
	return -ENOENT;
906
}
907
908
/* No abort during direct loading */
909
#define is_fw_load_aborted(buf) false
910
911
#endif /* CONFIG_FW_LOADER_USER_HELPER */
912
913
914
/* wait until the shared firmware_buf becomes ready (or error) */
915
static int sync_cached_firmware_buf(struct firmware_buf *buf)
916
{
917
	int ret = 0;
918
919
	mutex_lock(&fw_lock);
920
	while (!test_bit(FW_STATUS_DONE, &buf->status)) {
921
		if (is_fw_load_aborted(buf)) {
922
			ret = -ENOENT;
923
			break;
924
		}
925
		mutex_unlock(&fw_lock);
926
		wait_for_completion(&buf->completion);
927
		mutex_lock(&fw_lock);
928
	}
929
	mutex_unlock(&fw_lock);
930
	return ret;
931
}
932
933
/* prepare firmware and firmware_buf structs;
934
 * return 0 if a firmware is already assigned, 1 if need to load one,
935
 * or a negative error code
936
 */
937
static int
938
_request_firmware_prepare(struct firmware **firmware_p, const char *name,
939
			  struct device *device)
940
{
941
	struct firmware *firmware;
942
	struct firmware_buf *buf;
943
	int ret;
944
945
	*firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
946
	if (!firmware) {
947
		dev_err(device, "%s: kmalloc(struct firmware) failed\n",
948
			__func__);
949
		return -ENOMEM;
950
	}
951
952
	if (fw_get_builtin_firmware(firmware, name)) {
953
		dev_dbg(device, "firmware: using built-in firmware %s\n", name);
954
		return 0; /* assigned */
955
	}
956
957
	ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
958
959
	/*
960
	 * bind with 'buf' now to avoid warning in failure path
961
	 * of requesting firmware.
962
	 */
963
	firmware->priv = buf;
964
965
	if (ret > 0) {
966
		ret = sync_cached_firmware_buf(buf);
967
		if (!ret) {
968
			fw_set_page_data(buf, firmware);
969
			return 0; /* assigned */
970
		}
971
	}
972
973
	if (ret < 0)
974
		return ret;
975
	return 1; /* need to load */
976
}
977
978
static int assign_firmware_buf(struct firmware *fw, struct device *device)
979
{
980
	struct firmware_buf *buf = fw->priv;
981
982
	mutex_lock(&fw_lock);
983
	if (!buf->size || is_fw_load_aborted(buf)) {
984
		mutex_unlock(&fw_lock);
985
		return -ENOENT;
986
	}
987
988
	/*
989
	 * add firmware name into devres list so that we can auto cache
990
	 * and uncache firmware for device.
991
	 *
992
	 * device may has been deleted already, but the problem
993
	 * should be fixed in devres or driver core.
994
	 */
995
	if (device)
996
		fw_add_devm_name(device, buf->fw_id);
997
998
	/*
999
	 * After caching firmware image is started, let it piggyback
1000
	 * on request firmware.
1001
	 */
1002
	if (buf->fwc->state == FW_LOADER_START_CACHE) {
1003
		if (fw_cache_piggyback_on_request(buf->fw_id))
1004
			kref_get(&buf->ref);
1005
	}
1006
1007
	/* pass the pages buffer to driver at the last minute */
1008
	fw_set_page_data(buf, fw);
1009
	mutex_unlock(&fw_lock);
1010
	return 0;
1011
}
1012
1013
/* called from request_firmware() and request_firmware_work_func() */
1014
static int
1015
_request_firmware(const struct firmware **firmware_p, const char *name,
1016
		  struct device *device, bool uevent, bool nowait)
1017
{
1018
	struct firmware *fw;
1019
	long timeout;
1020
	int ret;
1021
1022
	if (!firmware_p)
1023
		return -EINVAL;
1024
1025
	ret = _request_firmware_prepare(&fw, name, device);
1026
	if (ret <= 0) /* error or already assigned */
1027
		goto out;
1028
1029
	ret = 0;
1030
	timeout = firmware_loading_timeout();
1031
	if (nowait) {
1032
		timeout = usermodehelper_read_lock_wait(timeout);
1033
		if (!timeout) {
1034
			dev_dbg(device, "firmware: %s loading timed out\n",
1035
				name);
1036
			ret = -EBUSY;
1037
			goto out;
1038
		}
1039
	} else {
1040
		ret = usermodehelper_read_trylock();
1041
		if (WARN_ON(ret)) {
1042
			dev_err(device, "firmware: %s will not be loaded\n",
1043
				name);
1044
			goto out;
1045
		}
1046
	}
1047
1048
	if (!fw_get_filesystem_firmware(device, fw->priv))
1049
		ret = fw_load_from_user_helper(fw, name, device,
1050
					       uevent, nowait, timeout);
1051
	if (!ret)
1052
		ret = assign_firmware_buf(fw, device);
1053
1054
	usermodehelper_read_unlock();
1055
1056
 out:
1057
	if (ret < 0) {
1058
		release_firmware(fw);
1059
		fw = NULL;
1060
	}
1061
1062
	*firmware_p = fw;
1063
	return ret;
1064
}
1065
569
/**
1066
/**
570
 * request_firmware: - send firmware request and wait for it
1067
 * request_firmware: - send firmware request and wait for it
571
 * @firmware_p: pointer to firmware image
1068
 * @firmware_p: pointer to firmware image
Lines 580-610 Link Here
580
 *      @name will be used as $FIRMWARE in the uevent environment and
1077
 *      @name will be used as $FIRMWARE in the uevent environment and
581
 *      should be distinctive enough not to be confused with any other
1078
 *      should be distinctive enough not to be confused with any other
582
 *      firmware image for this or any other device.
1079
 *      firmware image for this or any other device.
1080
 *
1081
 *	Caller must hold the reference count of @device.
1082
 *
1083
 *	The function can be called safely inside device's suspend and
1084
 *	resume callback.
583
 **/
1085
 **/
584
int
1086
int
585
request_firmware(const struct firmware **firmware_p, const char *name,
1087
request_firmware(const struct firmware **firmware_p, const char *name,
586
                 struct device *device)
1088
                 struct device *device)
587
{
1089
{
588
	struct firmware_priv *fw_priv;
1090
	return _request_firmware(firmware_p, name, device, true, false);
589
	int ret;
590
591
	fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
592
					    false);
593
	if (IS_ERR_OR_NULL(fw_priv))
594
		return PTR_RET(fw_priv);
595
596
	ret = usermodehelper_read_trylock();
597
	if (WARN_ON(ret)) {
598
		dev_err(device, "firmware: %s will not be loaded\n", name);
599
	} else {
600
		ret = _request_firmware_load(fw_priv, true,
601
					firmware_loading_timeout());
602
		usermodehelper_read_unlock();
603
	}
604
	if (ret)
605
		_request_firmware_cleanup(firmware_p);
606
607
	return ret;
608
}
1091
}
609
1092
610
/**
1093
/**
Lines 635-666 Link Here
635
{
1118
{
636
	struct firmware_work *fw_work;
1119
	struct firmware_work *fw_work;
637
	const struct firmware *fw;
1120
	const struct firmware *fw;
638
	struct firmware_priv *fw_priv;
639
	long timeout;
640
	int ret;
641
1121
642
	fw_work = container_of(work, struct firmware_work, work);
1122
	fw_work = container_of(work, struct firmware_work, work);
643
	fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
644
			fw_work->uevent, true);
645
	if (IS_ERR_OR_NULL(fw_priv)) {
646
		ret = PTR_RET(fw_priv);
647
		goto out;
648
	}
649
650
	timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
651
	if (timeout) {
652
		ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
653
		usermodehelper_read_unlock();
654
	} else {
655
		dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
656
			fw_work->name);
657
		ret = -EAGAIN;
658
	}
659
	if (ret)
660
		_request_firmware_cleanup(&fw);
661
1123
662
 out:
1124
	_request_firmware(&fw, fw_work->name, fw_work->device,
1125
			  fw_work->uevent, true);
663
	fw_work->cont(fw, fw_work->context);
1126
	fw_work->cont(fw, fw_work->context);
1127
	put_device(fw_work->device); /* taken in request_firmware_nowait() */
664
1128
665
	module_put(fw_work->module);
1129
	module_put(fw_work->module);
666
	kfree(fw_work);
1130
	kfree(fw_work);
Lines 679-687 Link Here
679
 * @cont: function will be called asynchronously when the firmware
1143
 * @cont: function will be called asynchronously when the firmware
680
 *	request is over.
1144
 *	request is over.
681
 *
1145
 *
682
 *	Asynchronous variant of request_firmware() for user contexts where
1146
 *	Caller must hold the reference count of @device.
683
 *	it is not possible to sleep for long time. It can't be called
1147
 *
684
 *	in atomic contexts.
1148
 *	Asynchronous variant of request_firmware() for user contexts:
1149
 *		- sleep for as small periods as possible since it may
1150
 *		increase kernel boot time of built-in device drivers
1151
 *		requesting firmware in their ->probe() methods, if
1152
 *		@gfp is GFP_KERNEL.
1153
 *
1154
 *		- can't sleep at all if @gfp is GFP_ATOMIC.
685
 **/
1155
 **/
686
int
1156
int
687
request_firmware_nowait(
1157
request_firmware_nowait(
Lines 707-725 Link Here
707
		return -EFAULT;
1177
		return -EFAULT;
708
	}
1178
	}
709
1179
1180
	get_device(fw_work->device);
710
	INIT_WORK(&fw_work->work, request_firmware_work_func);
1181
	INIT_WORK(&fw_work->work, request_firmware_work_func);
711
	schedule_work(&fw_work->work);
1182
	schedule_work(&fw_work->work);
712
	return 0;
1183
	return 0;
713
}
1184
}
714
1185
1186
/**
1187
 * cache_firmware - cache one firmware image in kernel memory space
1188
 * @fw_name: the firmware image name
1189
 *
1190
 * Cache firmware in kernel memory so that drivers can use it when
1191
 * system isn't ready for them to request firmware image from userspace.
1192
 * Once it returns successfully, driver can use request_firmware or its
1193
 * nowait version to get the cached firmware without any interacting
1194
 * with userspace
1195
 *
1196
 * Return 0 if the firmware image has been cached successfully
1197
 * Return !0 otherwise
1198
 *
1199
 */
1200
int cache_firmware(const char *fw_name)
1201
{
1202
	int ret;
1203
	const struct firmware *fw;
1204
1205
	pr_debug("%s: %s\n", __func__, fw_name);
1206
1207
	ret = request_firmware(&fw, fw_name, NULL);
1208
	if (!ret)
1209
		kfree(fw);
1210
1211
	pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1212
1213
	return ret;
1214
}
1215
1216
/**
1217
 * uncache_firmware - remove one cached firmware image
1218
 * @fw_name: the firmware image name
1219
 *
1220
 * Uncache one firmware image which has been cached successfully
1221
 * before.
1222
 *
1223
 * Return 0 if the firmware cache has been removed successfully
1224
 * Return !0 otherwise
1225
 *
1226
 */
1227
int uncache_firmware(const char *fw_name)
1228
{
1229
	struct firmware_buf *buf;
1230
	struct firmware fw;
1231
1232
	pr_debug("%s: %s\n", __func__, fw_name);
1233
1234
	if (fw_get_builtin_firmware(&fw, fw_name))
1235
		return 0;
1236
1237
	buf = fw_lookup_buf(fw_name);
1238
	if (buf) {
1239
		fw_free_buf(buf);
1240
		return 0;
1241
	}
1242
1243
	return -EINVAL;
1244
}
1245
1246
#ifdef CONFIG_PM_SLEEP
1247
static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1248
1249
static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1250
{
1251
	struct fw_cache_entry *fce;
1252
1253
	fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1254
	if (!fce)
1255
		goto exit;
1256
1257
	strcpy(fce->name, name);
1258
exit:
1259
	return fce;
1260
}
1261
1262
static int __fw_entry_found(const char *name)
1263
{
1264
	struct firmware_cache *fwc = &fw_cache;
1265
	struct fw_cache_entry *fce;
1266
1267
	list_for_each_entry(fce, &fwc->fw_names, list) {
1268
		if (!strcmp(fce->name, name))
1269
			return 1;
1270
	}
1271
	return 0;
1272
}
1273
1274
static int fw_cache_piggyback_on_request(const char *name)
1275
{
1276
	struct firmware_cache *fwc = &fw_cache;
1277
	struct fw_cache_entry *fce;
1278
	int ret = 0;
1279
1280
	spin_lock(&fwc->name_lock);
1281
	if (__fw_entry_found(name))
1282
		goto found;
1283
1284
	fce = alloc_fw_cache_entry(name);
1285
	if (fce) {
1286
		ret = 1;
1287
		list_add(&fce->list, &fwc->fw_names);
1288
		pr_debug("%s: fw: %s\n", __func__, name);
1289
	}
1290
found:
1291
	spin_unlock(&fwc->name_lock);
1292
	return ret;
1293
}
1294
1295
static void free_fw_cache_entry(struct fw_cache_entry *fce)
1296
{
1297
	kfree(fce);
1298
}
1299
1300
static void __async_dev_cache_fw_image(void *fw_entry,
1301
				       async_cookie_t cookie)
1302
{
1303
	struct fw_cache_entry *fce = fw_entry;
1304
	struct firmware_cache *fwc = &fw_cache;
1305
	int ret;
1306
1307
	ret = cache_firmware(fce->name);
1308
	if (ret) {
1309
		spin_lock(&fwc->name_lock);
1310
		list_del(&fce->list);
1311
		spin_unlock(&fwc->name_lock);
1312
1313
		free_fw_cache_entry(fce);
1314
	}
1315
}
1316
1317
/* called with dev->devres_lock held */
1318
static void dev_create_fw_entry(struct device *dev, void *res,
1319
				void *data)
1320
{
1321
	struct fw_name_devm *fwn = res;
1322
	const char *fw_name = fwn->name;
1323
	struct list_head *head = data;
1324
	struct fw_cache_entry *fce;
1325
1326
	fce = alloc_fw_cache_entry(fw_name);
1327
	if (fce)
1328
		list_add(&fce->list, head);
1329
}
1330
1331
static int devm_name_match(struct device *dev, void *res,
1332
			   void *match_data)
1333
{
1334
	struct fw_name_devm *fwn = res;
1335
	return (fwn->magic == (unsigned long)match_data);
1336
}
1337
1338
static void dev_cache_fw_image(struct device *dev, void *data)
1339
{
1340
	LIST_HEAD(todo);
1341
	struct fw_cache_entry *fce;
1342
	struct fw_cache_entry *fce_next;
1343
	struct firmware_cache *fwc = &fw_cache;
1344
1345
	devres_for_each_res(dev, fw_name_devm_release,
1346
			    devm_name_match, &fw_cache,
1347
			    dev_create_fw_entry, &todo);
1348
1349
	list_for_each_entry_safe(fce, fce_next, &todo, list) {
1350
		list_del(&fce->list);
1351
1352
		spin_lock(&fwc->name_lock);
1353
		/* only one cache entry for one firmware */
1354
		if (!__fw_entry_found(fce->name)) {
1355
			list_add(&fce->list, &fwc->fw_names);
1356
		} else {
1357
			free_fw_cache_entry(fce);
1358
			fce = NULL;
1359
		}
1360
		spin_unlock(&fwc->name_lock);
1361
1362
		if (fce)
1363
			async_schedule_domain(__async_dev_cache_fw_image,
1364
					      (void *)fce,
1365
					      &fw_cache_domain);
1366
	}
1367
}
1368
1369
static void __device_uncache_fw_images(void)
1370
{
1371
	struct firmware_cache *fwc = &fw_cache;
1372
	struct fw_cache_entry *fce;
1373
1374
	spin_lock(&fwc->name_lock);
1375
	while (!list_empty(&fwc->fw_names)) {
1376
		fce = list_entry(fwc->fw_names.next,
1377
				struct fw_cache_entry, list);
1378
		list_del(&fce->list);
1379
		spin_unlock(&fwc->name_lock);
1380
1381
		uncache_firmware(fce->name);
1382
		free_fw_cache_entry(fce);
1383
1384
		spin_lock(&fwc->name_lock);
1385
	}
1386
	spin_unlock(&fwc->name_lock);
1387
}
1388
1389
/**
1390
 * device_cache_fw_images - cache devices' firmware
1391
 *
1392
 * If one device called request_firmware or its nowait version
1393
 * successfully before, the firmware names are recored into the
1394
 * device's devres link list, so device_cache_fw_images can call
1395
 * cache_firmware() to cache these firmwares for the device,
1396
 * then the device driver can load its firmwares easily at
1397
 * time when system is not ready to complete loading firmware.
1398
 */
1399
static void device_cache_fw_images(void)
1400
{
1401
	struct firmware_cache *fwc = &fw_cache;
1402
	int old_timeout;
1403
	DEFINE_WAIT(wait);
1404
1405
	pr_debug("%s\n", __func__);
1406
1407
	/* cancel uncache work */
1408
	cancel_delayed_work_sync(&fwc->work);
1409
1410
	/*
1411
	 * use small loading timeout for caching devices' firmware
1412
	 * because all these firmware images have been loaded
1413
	 * successfully at lease once, also system is ready for
1414
	 * completing firmware loading now. The maximum size of
1415
	 * firmware in current distributions is about 2M bytes,
1416
	 * so 10 secs should be enough.
1417
	 */
1418
	old_timeout = loading_timeout;
1419
	loading_timeout = 10;
1420
1421
	mutex_lock(&fw_lock);
1422
	fwc->state = FW_LOADER_START_CACHE;
1423
	dpm_for_each_dev(NULL, dev_cache_fw_image);
1424
	mutex_unlock(&fw_lock);
1425
1426
	/* wait for completion of caching firmware for all devices */
1427
	async_synchronize_full_domain(&fw_cache_domain);
1428
1429
	loading_timeout = old_timeout;
1430
}
1431
1432
/**
1433
 * device_uncache_fw_images - uncache devices' firmware
1434
 *
1435
 * uncache all firmwares which have been cached successfully
1436
 * by device_uncache_fw_images earlier
1437
 */
1438
static void device_uncache_fw_images(void)
1439
{
1440
	pr_debug("%s\n", __func__);
1441
	__device_uncache_fw_images();
1442
}
1443
1444
static void device_uncache_fw_images_work(struct work_struct *work)
1445
{
1446
	device_uncache_fw_images();
1447
}
1448
1449
/**
1450
 * device_uncache_fw_images_delay - uncache devices firmwares
1451
 * @delay: number of milliseconds to delay uncache device firmwares
1452
 *
1453
 * uncache all devices's firmwares which has been cached successfully
1454
 * by device_cache_fw_images after @delay milliseconds.
1455
 */
1456
static void device_uncache_fw_images_delay(unsigned long delay)
1457
{
1458
	schedule_delayed_work(&fw_cache.work,
1459
			msecs_to_jiffies(delay));
1460
}
1461
1462
static int fw_pm_notify(struct notifier_block *notify_block,
1463
			unsigned long mode, void *unused)
1464
{
1465
	switch (mode) {
1466
	case PM_HIBERNATION_PREPARE:
1467
	case PM_SUSPEND_PREPARE:
1468
		device_cache_fw_images();
1469
		break;
1470
1471
	case PM_POST_SUSPEND:
1472
	case PM_POST_HIBERNATION:
1473
	case PM_POST_RESTORE:
1474
		/*
1475
		 * In case that system sleep failed and syscore_suspend is
1476
		 * not called.
1477
		 */
1478
		mutex_lock(&fw_lock);
1479
		fw_cache.state = FW_LOADER_NO_CACHE;
1480
		mutex_unlock(&fw_lock);
1481
1482
		device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1483
		break;
1484
	}
1485
1486
	return 0;
1487
}
1488
1489
/* stop caching firmware once syscore_suspend is reached */
1490
static int fw_suspend(void)
1491
{
1492
	fw_cache.state = FW_LOADER_NO_CACHE;
1493
	return 0;
1494
}
1495
1496
static struct syscore_ops fw_syscore_ops = {
1497
	.suspend = fw_suspend,
1498
};
1499
#else
1500
static int fw_cache_piggyback_on_request(const char *name)
1501
{
1502
	return 0;
1503
}
1504
#endif
1505
1506
static void __init fw_cache_init(void)
1507
{
1508
	spin_lock_init(&fw_cache.lock);
1509
	INIT_LIST_HEAD(&fw_cache.head);
1510
	fw_cache.state = FW_LOADER_NO_CACHE;
1511
1512
#ifdef CONFIG_PM_SLEEP
1513
	spin_lock_init(&fw_cache.name_lock);
1514
	INIT_LIST_HEAD(&fw_cache.fw_names);
1515
1516
	INIT_DELAYED_WORK(&fw_cache.work,
1517
			  device_uncache_fw_images_work);
1518
1519
	fw_cache.pm_notify.notifier_call = fw_pm_notify;
1520
	register_pm_notifier(&fw_cache.pm_notify);
1521
1522
	register_syscore_ops(&fw_syscore_ops);
1523
#endif
1524
}
1525
715
static int __init firmware_class_init(void)
1526
static int __init firmware_class_init(void)
716
{
1527
{
1528
	fw_cache_init();
1529
#ifdef CONFIG_FW_LOADER_USER_HELPER
717
	return class_register(&firmware_class);
1530
	return class_register(&firmware_class);
1531
#else
1532
	return 0;
1533
#endif
718
}
1534
}
719
1535
720
static void __exit firmware_class_exit(void)
1536
static void __exit firmware_class_exit(void)
721
{
1537
{
1538
#ifdef CONFIG_PM_SLEEP
1539
	unregister_syscore_ops(&fw_syscore_ops);
1540
	unregister_pm_notifier(&fw_cache.pm_notify);
1541
#endif
1542
#ifdef CONFIG_FW_LOADER_USER_HELPER
722
	class_unregister(&firmware_class);
1543
	class_unregister(&firmware_class);
1544
#endif
723
}
1545
}
724
1546
725
fs_initcall(firmware_class_init);
1547
fs_initcall(firmware_class_init);
Lines 728-730 Link Here
728
EXPORT_SYMBOL(release_firmware);
1550
EXPORT_SYMBOL(release_firmware);
729
EXPORT_SYMBOL(request_firmware);
1551
EXPORT_SYMBOL(request_firmware);
730
EXPORT_SYMBOL(request_firmware_nowait);
1552
EXPORT_SYMBOL(request_firmware_nowait);
1553
EXPORT_SYMBOL_GPL(cache_firmware);
1554
EXPORT_SYMBOL_GPL(uncache_firmware);
(-)a/drivers/base/Kconfig (+11 lines)
Lines 145-150 config EXTRA_FIRMWARE_DIR Link Here
145
	  this option you can point it elsewhere, such as /lib/firmware/ or
145
	  this option you can point it elsewhere, such as /lib/firmware/ or
146
	  some other directory containing the firmware files.
146
	  some other directory containing the firmware files.
147
147
148
config FW_LOADER_USER_HELPER
149
	bool "Fallback user-helper invocation for firmware loading"
150
	depends on FW_LOADER
151
	default y
152
	help
153
	  This option enables / disables the invocation of user-helper
154
	  (e.g. udev) for loading firmware files as a fallback after the
155
	  direct file loading in kernel fails.  The user-mode helper is
156
	  no longer required unless you have a special firmware file that
157
	  resides in a non-standard path.
158
148
config DEBUG_DRIVER
159
config DEBUG_DRIVER
149
	bool "Driver Core verbose debug messages"
160
	bool "Driver Core verbose debug messages"
150
	depends on DEBUG_KERNEL
161
	depends on DEBUG_KERNEL
(-)linux-3.4.83/firmware_class/README (-8 / +28 lines)
Lines 18-49 Link Here
18
 High level behavior (mixed):
18
 High level behavior (mixed):
19
 ============================
19
 ============================
20
20
21
 kernel(driver): calls request_firmware(&fw_entry, $FIRMWARE, device)
21
 1), kernel(driver):
22
	- calls request_firmware(&fw_entry, $FIRMWARE, device)
23
	- kernel searchs the fimware image with name $FIRMWARE directly
24
	in the below search path of root filesystem:
25
		User customized search path by module parameter 'path'[1]
26
		"/lib/firmware/updates/" UTS_RELEASE,
27
		"/lib/firmware/updates",
28
		"/lib/firmware/" UTS_RELEASE,
29
		"/lib/firmware"
30
	- If found, goto 7), else goto 2)
31
32
	[1], the 'path' is a string parameter which length should be less
33
	than 256, user should pass 'firmware_class.path=$CUSTOMIZED_PATH'
34
	if firmware_class is built in kernel(the general situation)
22
35
23
 userspace:
36
 2), userspace:
24
 	- /sys/class/firmware/xxx/{loading,data} appear.
37
 	- /sys/class/firmware/xxx/{loading,data} appear.
25
	- hotplug gets called with a firmware identifier in $FIRMWARE
38
	- hotplug gets called with a firmware identifier in $FIRMWARE
26
	  and the usual hotplug environment.
39
	  and the usual hotplug environment.
27
		- hotplug: echo 1 > /sys/class/firmware/xxx/loading
40
		- hotplug: echo 1 > /sys/class/firmware/xxx/loading
28
41
29
 kernel: Discard any previous partial load.
42
 3), kernel: Discard any previous partial load.
30
43
31
 userspace:
44
 4), userspace:
32
		- hotplug: cat appropriate_firmware_image > \
45
		- hotplug: cat appropriate_firmware_image > \
33
					/sys/class/firmware/xxx/data
46
					/sys/class/firmware/xxx/data
34
47
35
 kernel: grows a buffer in PAGE_SIZE increments to hold the image as it
48
 5), kernel: grows a buffer in PAGE_SIZE increments to hold the image as it
36
	 comes in.
49
	 comes in.
37
50
38
 userspace:
51
 6), userspace:
39
		- hotplug: echo 0 > /sys/class/firmware/xxx/loading
52
		- hotplug: echo 0 > /sys/class/firmware/xxx/loading
40
53
41
 kernel: request_firmware() returns and the driver has the firmware
54
 7), kernel: request_firmware() returns and the driver has the firmware
42
	 image in fw_entry->{data,size}. If something went wrong
55
	 image in fw_entry->{data,size}. If something went wrong
43
	 request_firmware() returns non-zero and fw_entry is set to
56
	 request_firmware() returns non-zero and fw_entry is set to
44
	 NULL.
57
	 NULL.
45
58
46
 kernel(driver): Driver code calls release_firmware(fw_entry) releasing
59
 8), kernel(driver): Driver code calls release_firmware(fw_entry) releasing
47
		 the firmware image and any related resource.
60
		 the firmware image and any related resource.
48
61
49
 High level behavior (driver code):
62
 High level behavior (driver code):
Lines 106-108 Link Here
106
   on the setup, so I think that the choice on what firmware to make
119
   on the setup, so I think that the choice on what firmware to make
107
   persistent should be left to userspace.
120
   persistent should be left to userspace.
108
121
122
 about firmware cache:
123
 --------------------
124
 After firmware cache mechanism is introduced during system sleep,
125
 request_firmware can be called safely inside device's suspend and
126
 resume callback, and callers need't cache the firmware by
127
 themselves any more for dealing with firmware loss during system
128
 resume.

Return to bug 503886