firmware loader: use small timeout for cache device firmware
[deliverable/linux.git] / drivers / base / firmware_class.c
CommitLineData
1da177e4
LT
1/*
2 * firmware_class.c - Multi purpose firmware loading support
3 *
87d37a4f 4 * Copyright (c) 2003 Manuel Estrada Sainz
1da177e4
LT
5 *
6 * Please see Documentation/firmware_class/ for more information.
7 *
8 */
9
c59ede7b 10#include <linux/capability.h>
1da177e4
LT
11#include <linux/device.h>
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/timer.h>
15#include <linux/vmalloc.h>
16#include <linux/interrupt.h>
17#include <linux/bitops.h>
cad1e55d 18#include <linux/mutex.h>
a36cf844 19#include <linux/workqueue.h>
6e03a201 20#include <linux/highmem.h>
1da177e4 21#include <linux/firmware.h>
5a0e3ad6 22#include <linux/slab.h>
a36cf844 23#include <linux/sched.h>
1f2b7959 24#include <linux/list.h>
37276a51
ML
25#include <linux/async.h>
26#include <linux/pm.h>
27
28#include "base.h"
29#include "power/power.h"
1da177e4 30
87d37a4f 31MODULE_AUTHOR("Manuel Estrada Sainz");
1da177e4
LT
32MODULE_DESCRIPTION("Multi purpose firmware loading support");
33MODULE_LICENSE("GPL");
34
bcb9bd18
DT
35/* Builtin firmware support */
36
37#ifdef CONFIG_FW_LOADER
38
39extern struct builtin_fw __start_builtin_fw[];
40extern struct builtin_fw __end_builtin_fw[];
41
42static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
43{
44 struct builtin_fw *b_fw;
45
46 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
47 if (strcmp(name, b_fw->name) == 0) {
48 fw->size = b_fw->size;
49 fw->data = b_fw->data;
50 return true;
51 }
52 }
53
54 return false;
55}
56
57static bool fw_is_builtin_firmware(const struct firmware *fw)
58{
59 struct builtin_fw *b_fw;
60
61 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
62 if (fw->data == b_fw->data)
63 return true;
64
65 return false;
66}
67
68#else /* Module case - no builtin firmware support */
69
70static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
71{
72 return false;
73}
74
75static inline bool fw_is_builtin_firmware(const struct firmware *fw)
76{
77 return false;
78}
79#endif
80
1da177e4
LT
81enum {
82 FW_STATUS_LOADING,
83 FW_STATUS_DONE,
84 FW_STATUS_ABORT,
1da177e4
LT
85};
86
2f65168d 87static int loading_timeout = 60; /* In seconds */
1da177e4 88
9b78c1da
RW
89static inline long firmware_loading_timeout(void)
90{
91 return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
92}
93
1f2b7959
ML
94struct firmware_cache {
95 /* firmware_buf instance will be added into the below list */
96 spinlock_t lock;
97 struct list_head head;
37276a51
ML
98
99 /*
100 * Names of firmware images which have been cached successfully
101 * will be added into the below list so that device uncache
102 * helper can trace which firmware images have been cached
103 * before.
104 */
105 spinlock_t name_lock;
106 struct list_head fw_names;
107
108 wait_queue_head_t wait_queue;
109 int cnt;
110 struct delayed_work work;
1f2b7959 111};
1da177e4 112
1244691c 113struct firmware_buf {
1f2b7959
ML
114 struct kref ref;
115 struct list_head list;
1da177e4 116 struct completion completion;
1f2b7959 117 struct firmware_cache *fwc;
1da177e4 118 unsigned long status;
65710cb6
ML
119 void *data;
120 size_t size;
6e03a201
DW
121 struct page **pages;
122 int nr_pages;
123 int page_array_size;
1244691c
ML
124 char fw_id[];
125};
126
37276a51
ML
127struct fw_cache_entry {
128 struct list_head list;
129 char name[];
130};
131
1244691c 132struct firmware_priv {
1da177e4 133 struct timer_list timeout;
e9045f91 134 bool nowait;
1244691c
ML
135 struct device dev;
136 struct firmware_buf *buf;
1f2b7959 137 struct firmware *fw;
1da177e4
LT
138};
139
f531f05a
ML
140struct fw_name_devm {
141 unsigned long magic;
142 char name[];
143};
144
1f2b7959
ML
145#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
146
147/* fw_lock could be moved to 'struct firmware_priv' but since it is just
148 * guarding for corner cases a global lock should be OK */
149static DEFINE_MUTEX(fw_lock);
150
151static struct firmware_cache fw_cache;
152
153static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
154 struct firmware_cache *fwc)
155{
156 struct firmware_buf *buf;
157
158 buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
159
160 if (!buf)
161 return buf;
162
163 kref_init(&buf->ref);
164 strcpy(buf->fw_id, fw_name);
165 buf->fwc = fwc;
166 init_completion(&buf->completion);
167
168 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
169
170 return buf;
171}
172
2887b395
ML
173static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
174{
175 struct firmware_buf *tmp;
176 struct firmware_cache *fwc = &fw_cache;
177
178 list_for_each_entry(tmp, &fwc->head, list)
179 if (!strcmp(tmp->fw_id, fw_name))
180 return tmp;
181 return NULL;
182}
183
1f2b7959
ML
184static int fw_lookup_and_allocate_buf(const char *fw_name,
185 struct firmware_cache *fwc,
186 struct firmware_buf **buf)
187{
188 struct firmware_buf *tmp;
189
190 spin_lock(&fwc->lock);
2887b395
ML
191 tmp = __fw_lookup_buf(fw_name);
192 if (tmp) {
193 kref_get(&tmp->ref);
194 spin_unlock(&fwc->lock);
195 *buf = tmp;
196 return 1;
197 }
1f2b7959
ML
198 tmp = __allocate_fw_buf(fw_name, fwc);
199 if (tmp)
200 list_add(&tmp->list, &fwc->head);
201 spin_unlock(&fwc->lock);
202
203 *buf = tmp;
204
205 return tmp ? 0 : -ENOMEM;
206}
207
2887b395
ML
208static struct firmware_buf *fw_lookup_buf(const char *fw_name)
209{
210 struct firmware_buf *tmp;
211 struct firmware_cache *fwc = &fw_cache;
212
213 spin_lock(&fwc->lock);
214 tmp = __fw_lookup_buf(fw_name);
215 spin_unlock(&fwc->lock);
216
217 return tmp;
218}
219
1f2b7959
ML
220static void __fw_free_buf(struct kref *ref)
221{
222 struct firmware_buf *buf = to_fwbuf(ref);
223 struct firmware_cache *fwc = buf->fwc;
224 int i;
225
226 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
227 __func__, buf->fw_id, buf, buf->data,
228 (unsigned int)buf->size);
229
230 spin_lock(&fwc->lock);
231 list_del(&buf->list);
232 spin_unlock(&fwc->lock);
233
234 vunmap(buf->data);
235 for (i = 0; i < buf->nr_pages; i++)
236 __free_page(buf->pages[i]);
237 kfree(buf->pages);
238 kfree(buf);
239}
240
241static void fw_free_buf(struct firmware_buf *buf)
242{
243 kref_put(&buf->ref, __fw_free_buf);
244}
245
f8a4bd34
DT
246static struct firmware_priv *to_firmware_priv(struct device *dev)
247{
248 return container_of(dev, struct firmware_priv, dev);
249}
250
251static void fw_load_abort(struct firmware_priv *fw_priv)
1da177e4 252{
1244691c
ML
253 struct firmware_buf *buf = fw_priv->buf;
254
255 set_bit(FW_STATUS_ABORT, &buf->status);
1f2b7959 256 complete_all(&buf->completion);
1da177e4
LT
257}
258
f8a4bd34
DT
259static ssize_t firmware_timeout_show(struct class *class,
260 struct class_attribute *attr,
261 char *buf)
1da177e4
LT
262{
263 return sprintf(buf, "%d\n", loading_timeout);
264}
265
266/**
eb8e3179
RD
267 * firmware_timeout_store - set number of seconds to wait for firmware
268 * @class: device class pointer
e59817bf 269 * @attr: device attribute pointer
eb8e3179
RD
270 * @buf: buffer to scan for timeout value
271 * @count: number of bytes in @buf
272 *
1da177e4 273 * Sets the number of seconds to wait for the firmware. Once
eb8e3179 274 * this expires an error will be returned to the driver and no
1da177e4
LT
275 * firmware will be provided.
276 *
eb8e3179 277 * Note: zero means 'wait forever'.
1da177e4 278 **/
f8a4bd34
DT
279static ssize_t firmware_timeout_store(struct class *class,
280 struct class_attribute *attr,
281 const char *buf, size_t count)
1da177e4
LT
282{
283 loading_timeout = simple_strtol(buf, NULL, 10);
b92eac01
SG
284 if (loading_timeout < 0)
285 loading_timeout = 0;
f8a4bd34 286
1da177e4
LT
287 return count;
288}
289
673fae90
DT
290static struct class_attribute firmware_class_attrs[] = {
291 __ATTR(timeout, S_IWUSR | S_IRUGO,
292 firmware_timeout_show, firmware_timeout_store),
293 __ATTR_NULL
294};
1da177e4 295
1244691c
ML
296static void fw_dev_release(struct device *dev)
297{
298 struct firmware_priv *fw_priv = to_firmware_priv(dev);
65710cb6 299
673fae90 300 kfree(fw_priv);
673fae90
DT
301
302 module_put(THIS_MODULE);
303}
1da177e4 304
7eff2e7a 305static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
1da177e4 306{
f8a4bd34 307 struct firmware_priv *fw_priv = to_firmware_priv(dev);
1da177e4 308
1244691c 309 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
1da177e4 310 return -ENOMEM;
7eff2e7a 311 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
6897089c 312 return -ENOMEM;
e9045f91
JB
313 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
314 return -ENOMEM;
1da177e4
LT
315
316 return 0;
317}
318
1b81d663
AB
319static struct class firmware_class = {
320 .name = "firmware",
673fae90 321 .class_attrs = firmware_class_attrs,
e55c8790
GKH
322 .dev_uevent = firmware_uevent,
323 .dev_release = fw_dev_release,
1b81d663
AB
324};
325
e55c8790
GKH
326static ssize_t firmware_loading_show(struct device *dev,
327 struct device_attribute *attr, char *buf)
1da177e4 328{
f8a4bd34 329 struct firmware_priv *fw_priv = to_firmware_priv(dev);
1244691c 330 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
f8a4bd34 331
1da177e4
LT
332 return sprintf(buf, "%d\n", loading);
333}
334
65710cb6 335/* firmware holds the ownership of pages */
dd336c55
DW
336static void firmware_free_data(const struct firmware *fw)
337{
1f2b7959
ML
338 WARN_ON(!fw->priv);
339 fw_free_buf(fw->priv);
dd336c55
DW
340}
341
6e03a201
DW
342/* Some architectures don't have PAGE_KERNEL_RO */
343#ifndef PAGE_KERNEL_RO
344#define PAGE_KERNEL_RO PAGE_KERNEL
345#endif
1da177e4 346/**
eb8e3179 347 * firmware_loading_store - set value in the 'loading' control file
e55c8790 348 * @dev: device pointer
af9997e4 349 * @attr: device attribute pointer
eb8e3179
RD
350 * @buf: buffer to scan for loading control value
351 * @count: number of bytes in @buf
352 *
1da177e4
LT
353 * The relevant values are:
354 *
355 * 1: Start a load, discarding any previous partial load.
eb8e3179 356 * 0: Conclude the load and hand the data to the driver code.
1da177e4
LT
357 * -1: Conclude the load with an error and discard any written data.
358 **/
e55c8790
GKH
359static ssize_t firmware_loading_store(struct device *dev,
360 struct device_attribute *attr,
361 const char *buf, size_t count)
1da177e4 362{
f8a4bd34 363 struct firmware_priv *fw_priv = to_firmware_priv(dev);
1244691c 364 struct firmware_buf *fw_buf = fw_priv->buf;
1da177e4 365 int loading = simple_strtol(buf, NULL, 10);
6e03a201 366 int i;
1da177e4 367
eea915bb
NH
368 mutex_lock(&fw_lock);
369
1244691c 370 if (!fw_buf)
eea915bb
NH
371 goto out;
372
1da177e4
LT
373 switch (loading) {
374 case 1:
65710cb6 375 /* discarding any previous partial load */
1244691c
ML
376 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
377 for (i = 0; i < fw_buf->nr_pages; i++)
378 __free_page(fw_buf->pages[i]);
379 kfree(fw_buf->pages);
380 fw_buf->pages = NULL;
381 fw_buf->page_array_size = 0;
382 fw_buf->nr_pages = 0;
383 set_bit(FW_STATUS_LOADING, &fw_buf->status);
28eefa75 384 }
1da177e4
LT
385 break;
386 case 0:
1244691c
ML
387 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
388 set_bit(FW_STATUS_DONE, &fw_buf->status);
389 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
1f2b7959 390 complete_all(&fw_buf->completion);
1da177e4
LT
391 break;
392 }
393 /* fallthrough */
394 default:
266a813c 395 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
1da177e4
LT
396 /* fallthrough */
397 case -1:
398 fw_load_abort(fw_priv);
399 break;
400 }
eea915bb
NH
401out:
402 mutex_unlock(&fw_lock);
1da177e4
LT
403 return count;
404}
405
e55c8790 406static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
1da177e4 407
f8a4bd34
DT
408static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
409 struct bin_attribute *bin_attr,
410 char *buffer, loff_t offset, size_t count)
1da177e4 411{
b0d1f807 412 struct device *dev = kobj_to_dev(kobj);
f8a4bd34 413 struct firmware_priv *fw_priv = to_firmware_priv(dev);
1244691c 414 struct firmware_buf *buf;
f37e6617 415 ssize_t ret_count;
1da177e4 416
cad1e55d 417 mutex_lock(&fw_lock);
1244691c
ML
418 buf = fw_priv->buf;
419 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
1da177e4
LT
420 ret_count = -ENODEV;
421 goto out;
422 }
1244691c 423 if (offset > buf->size) {
308975fa
JS
424 ret_count = 0;
425 goto out;
426 }
1244691c
ML
427 if (count > buf->size - offset)
428 count = buf->size - offset;
6e03a201
DW
429
430 ret_count = count;
431
432 while (count) {
433 void *page_data;
434 int page_nr = offset >> PAGE_SHIFT;
435 int page_ofs = offset & (PAGE_SIZE-1);
436 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
437
1244691c 438 page_data = kmap(buf->pages[page_nr]);
6e03a201
DW
439
440 memcpy(buffer, page_data + page_ofs, page_cnt);
441
1244691c 442 kunmap(buf->pages[page_nr]);
6e03a201
DW
443 buffer += page_cnt;
444 offset += page_cnt;
445 count -= page_cnt;
446 }
1da177e4 447out:
cad1e55d 448 mutex_unlock(&fw_lock);
1da177e4
LT
449 return ret_count;
450}
eb8e3179 451
f8a4bd34 452static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
1da177e4 453{
1244691c 454 struct firmware_buf *buf = fw_priv->buf;
6e03a201
DW
455 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
456
457 /* If the array of pages is too small, grow it... */
1244691c 458 if (buf->page_array_size < pages_needed) {
6e03a201 459 int new_array_size = max(pages_needed,
1244691c 460 buf->page_array_size * 2);
6e03a201
DW
461 struct page **new_pages;
462
463 new_pages = kmalloc(new_array_size * sizeof(void *),
464 GFP_KERNEL);
465 if (!new_pages) {
466 fw_load_abort(fw_priv);
467 return -ENOMEM;
468 }
1244691c
ML
469 memcpy(new_pages, buf->pages,
470 buf->page_array_size * sizeof(void *));
471 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
472 (new_array_size - buf->page_array_size));
473 kfree(buf->pages);
474 buf->pages = new_pages;
475 buf->page_array_size = new_array_size;
6e03a201 476 }
1da177e4 477
1244691c
ML
478 while (buf->nr_pages < pages_needed) {
479 buf->pages[buf->nr_pages] =
6e03a201 480 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
1da177e4 481
1244691c 482 if (!buf->pages[buf->nr_pages]) {
6e03a201
DW
483 fw_load_abort(fw_priv);
484 return -ENOMEM;
485 }
1244691c 486 buf->nr_pages++;
1da177e4 487 }
1da177e4
LT
488 return 0;
489}
490
491/**
eb8e3179 492 * firmware_data_write - write method for firmware
2c3c8bea 493 * @filp: open sysfs file
e55c8790 494 * @kobj: kobject for the device
42e61f4a 495 * @bin_attr: bin_attr structure
eb8e3179
RD
496 * @buffer: buffer being written
497 * @offset: buffer offset for write in total data store area
498 * @count: buffer size
1da177e4 499 *
eb8e3179 500 * Data written to the 'data' attribute will be later handed to
1da177e4
LT
501 * the driver as a firmware image.
502 **/
f8a4bd34
DT
503static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
504 struct bin_attribute *bin_attr,
505 char *buffer, loff_t offset, size_t count)
1da177e4 506{
b0d1f807 507 struct device *dev = kobj_to_dev(kobj);
f8a4bd34 508 struct firmware_priv *fw_priv = to_firmware_priv(dev);
1244691c 509 struct firmware_buf *buf;
1da177e4
LT
510 ssize_t retval;
511
512 if (!capable(CAP_SYS_RAWIO))
513 return -EPERM;
b92eac01 514
cad1e55d 515 mutex_lock(&fw_lock);
1244691c
ML
516 buf = fw_priv->buf;
517 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
1da177e4
LT
518 retval = -ENODEV;
519 goto out;
520 }
65710cb6 521
1da177e4
LT
522 retval = fw_realloc_buffer(fw_priv, offset + count);
523 if (retval)
524 goto out;
525
1da177e4 526 retval = count;
6e03a201
DW
527
528 while (count) {
529 void *page_data;
530 int page_nr = offset >> PAGE_SHIFT;
531 int page_ofs = offset & (PAGE_SIZE - 1);
532 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
533
1244691c 534 page_data = kmap(buf->pages[page_nr]);
6e03a201
DW
535
536 memcpy(page_data + page_ofs, buffer, page_cnt);
537
1244691c 538 kunmap(buf->pages[page_nr]);
6e03a201
DW
539 buffer += page_cnt;
540 offset += page_cnt;
541 count -= page_cnt;
542 }
543
1244691c 544 buf->size = max_t(size_t, offset, buf->size);
1da177e4 545out:
cad1e55d 546 mutex_unlock(&fw_lock);
1da177e4
LT
547 return retval;
548}
eb8e3179 549
0983ca2d
DT
550static struct bin_attribute firmware_attr_data = {
551 .attr = { .name = "data", .mode = 0644 },
1da177e4
LT
552 .size = 0,
553 .read = firmware_data_read,
554 .write = firmware_data_write,
555};
556
f8a4bd34 557static void firmware_class_timeout(u_long data)
1da177e4
LT
558{
559 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
f8a4bd34 560
1da177e4
LT
561 fw_load_abort(fw_priv);
562}
563
f8a4bd34 564static struct firmware_priv *
dddb5549 565fw_create_instance(struct firmware *firmware, const char *fw_name,
f8a4bd34 566 struct device *device, bool uevent, bool nowait)
1da177e4 567{
f8a4bd34
DT
568 struct firmware_priv *fw_priv;
569 struct device *f_dev;
1da177e4 570
1244691c 571 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
f8a4bd34 572 if (!fw_priv) {
266a813c 573 dev_err(device, "%s: kmalloc failed\n", __func__);
1244691c
ML
574 fw_priv = ERR_PTR(-ENOMEM);
575 goto exit;
576 }
577
f8a4bd34 578 fw_priv->nowait = nowait;
1f2b7959 579 fw_priv->fw = firmware;
f8a4bd34
DT
580 setup_timer(&fw_priv->timeout,
581 firmware_class_timeout, (u_long) fw_priv);
1da177e4 582
f8a4bd34
DT
583 f_dev = &fw_priv->dev;
584
585 device_initialize(f_dev);
99c2aa72 586 dev_set_name(f_dev, "%s", fw_name);
e55c8790
GKH
587 f_dev->parent = device;
588 f_dev->class = &firmware_class;
1244691c 589exit:
f8a4bd34 590 return fw_priv;
1da177e4
LT
591}
592
1f2b7959
ML
593/* one pages buffer is mapped/unmapped only once */
594static int fw_map_pages_buf(struct firmware_buf *buf)
595{
596 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
597 if (!buf->data)
598 return -ENOMEM;
599 return 0;
600}
601
602/* store the pages buffer info firmware from buf */
603static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
604{
605 fw->priv = buf;
606 fw->pages = buf->pages;
607 fw->size = buf->size;
608 fw->data = buf->data;
609
610 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
611 __func__, buf->fw_id, buf, buf->data,
612 (unsigned int)buf->size);
613}
614
f531f05a
ML
615static void fw_name_devm_release(struct device *dev, void *res)
616{
617 struct fw_name_devm *fwn = res;
618
619 if (fwn->magic == (unsigned long)&fw_cache)
620 pr_debug("%s: fw_name-%s devm-%p released\n",
621 __func__, fwn->name, res);
622}
623
624static int fw_devm_match(struct device *dev, void *res,
625 void *match_data)
626{
627 struct fw_name_devm *fwn = res;
628
629 return (fwn->magic == (unsigned long)&fw_cache) &&
630 !strcmp(fwn->name, match_data);
631}
632
633static struct fw_name_devm *fw_find_devm_name(struct device *dev,
634 const char *name)
635{
636 struct fw_name_devm *fwn;
637
638 fwn = devres_find(dev, fw_name_devm_release,
639 fw_devm_match, (void *)name);
640 return fwn;
641}
642
643/* add firmware name into devres list */
644static int fw_add_devm_name(struct device *dev, const char *name)
645{
646 struct fw_name_devm *fwn;
647
648 fwn = fw_find_devm_name(dev, name);
649 if (fwn)
650 return 1;
651
652 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
653 strlen(name) + 1, GFP_KERNEL);
654 if (!fwn)
655 return -ENOMEM;
656
657 fwn->magic = (unsigned long)&fw_cache;
658 strcpy(fwn->name, name);
659 devres_add(dev, fwn);
660
661 return 0;
662}
663
1f2b7959
ML
664static void _request_firmware_cleanup(const struct firmware **firmware_p)
665{
666 release_firmware(*firmware_p);
667 *firmware_p = NULL;
668}
669
dddb5549
SB
670static struct firmware_priv *
671_request_firmware_prepare(const struct firmware **firmware_p, const char *name,
672 struct device *device, bool uevent, bool nowait)
1da177e4 673{
1da177e4 674 struct firmware *firmware;
1f2b7959
ML
675 struct firmware_priv *fw_priv = NULL;
676 struct firmware_buf *buf;
677 int ret;
1da177e4
LT
678
679 if (!firmware_p)
dddb5549 680 return ERR_PTR(-EINVAL);
1da177e4 681
4aed0644 682 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
1da177e4 683 if (!firmware) {
266a813c
BH
684 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
685 __func__);
dddb5549 686 return ERR_PTR(-ENOMEM);
1da177e4 687 }
1da177e4 688
bcb9bd18 689 if (fw_get_builtin_firmware(firmware, name)) {
6f18ff91 690 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
dddb5549 691 return NULL;
5658c769
DW
692 }
693
1f2b7959
ML
694 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
695 if (!ret)
696 fw_priv = fw_create_instance(firmware, name, device,
697 uevent, nowait);
698
699 if (IS_ERR(fw_priv) || ret < 0) {
700 kfree(firmware);
dddb5549 701 *firmware_p = NULL;
1f2b7959
ML
702 return ERR_PTR(-ENOMEM);
703 } else if (fw_priv) {
704 fw_priv->buf = buf;
705
706 /*
707 * bind with 'buf' now to avoid warning in failure path
708 * of requesting firmware.
709 */
710 firmware->priv = buf;
711 return fw_priv;
dddb5549 712 }
811fa400 713
1f2b7959
ML
714 /* share the cached buf, which is inprogessing or completed */
715 check_status:
716 mutex_lock(&fw_lock);
717 if (test_bit(FW_STATUS_ABORT, &buf->status)) {
718 fw_priv = ERR_PTR(-ENOENT);
719 _request_firmware_cleanup(firmware_p);
720 goto exit;
721 } else if (test_bit(FW_STATUS_DONE, &buf->status)) {
722 fw_priv = NULL;
723 fw_set_page_data(buf, firmware);
724 goto exit;
725 }
726 mutex_unlock(&fw_lock);
727 wait_for_completion(&buf->completion);
728 goto check_status;
65710cb6 729
1f2b7959
ML
730exit:
731 mutex_unlock(&fw_lock);
732 return fw_priv;
65710cb6
ML
733}
734
dddb5549
SB
735static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
736 long timeout)
811fa400 737{
9b78c1da 738 int retval = 0;
dddb5549 739 struct device *f_dev = &fw_priv->dev;
1244691c 740 struct firmware_buf *buf = fw_priv->buf;
dddb5549
SB
741
742 dev_set_uevent_suppress(f_dev, true);
caca9510 743
dddb5549
SB
744 /* Need to pin this module until class device is destroyed */
745 __module_get(THIS_MODULE);
5658c769 746
dddb5549
SB
747 retval = device_add(f_dev);
748 if (retval) {
749 dev_err(f_dev, "%s: device_register failed\n", __func__);
750 goto err_put_dev;
751 }
752
753 retval = device_create_bin_file(f_dev, &firmware_attr_data);
754 if (retval) {
755 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
756 goto err_del_dev;
757 }
758
759 retval = device_create_file(f_dev, &dev_attr_loading);
760 if (retval) {
761 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
762 goto err_del_bin_attr;
763 }
1da177e4 764
312c004d 765 if (uevent) {
dddb5549 766 dev_set_uevent_suppress(f_dev, false);
1244691c 767 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
9b78c1da 768 if (timeout != MAX_SCHEDULE_TIMEOUT)
f8a4bd34 769 mod_timer(&fw_priv->timeout,
9b78c1da 770 round_jiffies_up(jiffies + timeout));
f8a4bd34
DT
771
772 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
773 }
774
1244691c 775 wait_for_completion(&buf->completion);
1da177e4 776
f8a4bd34 777 del_timer_sync(&fw_priv->timeout);
1da177e4 778
cad1e55d 779 mutex_lock(&fw_lock);
1244691c 780 if (!buf->size || test_bit(FW_STATUS_ABORT, &buf->status))
1da177e4 781 retval = -ENOENT;
65710cb6 782
f531f05a
ML
783 /*
784 * add firmware name into devres list so that we can auto cache
785 * and uncache firmware for device.
786 *
787 * f_dev->parent may has been deleted already, but the problem
788 * should be fixed in devres or driver core.
789 */
790 if (!retval && f_dev->parent)
791 fw_add_devm_name(f_dev->parent, buf->fw_id);
792
65710cb6 793 if (!retval)
1f2b7959
ML
794 retval = fw_map_pages_buf(buf);
795
796 /* pass the pages buffer to driver at the last minute */
797 fw_set_page_data(buf, fw_priv->fw);
1244691c 798
1244691c 799 fw_priv->buf = NULL;
cad1e55d 800 mutex_unlock(&fw_lock);
1da177e4 801
dddb5549
SB
802 device_remove_file(f_dev, &dev_attr_loading);
803err_del_bin_attr:
804 device_remove_bin_file(f_dev, &firmware_attr_data);
805err_del_dev:
806 device_del(f_dev);
807err_put_dev:
808 put_device(f_dev);
1da177e4
LT
809 return retval;
810}
811
6e3eaab0 812/**
312c004d 813 * request_firmware: - send firmware request and wait for it
eb8e3179
RD
814 * @firmware_p: pointer to firmware image
815 * @name: name of firmware file
816 * @device: device for which firmware is being loaded
817 *
818 * @firmware_p will be used to return a firmware image by the name
6e3eaab0
AS
819 * of @name for device @device.
820 *
821 * Should be called from user context where sleeping is allowed.
822 *
312c004d 823 * @name will be used as $FIRMWARE in the uevent environment and
6e3eaab0
AS
824 * should be distinctive enough not to be confused with any other
825 * firmware image for this or any other device.
0cfc1e1e
ML
826 *
827 * Caller must hold the reference count of @device.
6e3eaab0
AS
828 **/
829int
830request_firmware(const struct firmware **firmware_p, const char *name,
831 struct device *device)
832{
dddb5549 833 struct firmware_priv *fw_priv;
811fa400
RW
834 int ret;
835
dddb5549
SB
836 fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
837 false);
838 if (IS_ERR_OR_NULL(fw_priv))
839 return PTR_RET(fw_priv);
811fa400 840
9b78c1da
RW
841 ret = usermodehelper_read_trylock();
842 if (WARN_ON(ret)) {
843 dev_err(device, "firmware: %s will not be loaded\n", name);
844 } else {
dddb5549 845 ret = _request_firmware_load(fw_priv, true,
9b78c1da
RW
846 firmware_loading_timeout());
847 usermodehelper_read_unlock();
848 }
811fa400
RW
849 if (ret)
850 _request_firmware_cleanup(firmware_p);
851
852 return ret;
6e3eaab0
AS
853}
854
1da177e4
LT
855/**
856 * release_firmware: - release the resource associated with a firmware image
eb8e3179 857 * @fw: firmware resource to release
1da177e4 858 **/
bcb9bd18 859void release_firmware(const struct firmware *fw)
1da177e4
LT
860{
861 if (fw) {
bcb9bd18
DT
862 if (!fw_is_builtin_firmware(fw))
863 firmware_free_data(fw);
1da177e4
LT
864 kfree(fw);
865 }
866}
867
1da177e4
LT
868/* Async support */
869struct firmware_work {
870 struct work_struct work;
871 struct module *module;
872 const char *name;
873 struct device *device;
874 void *context;
875 void (*cont)(const struct firmware *fw, void *context);
072fc8f0 876 bool uevent;
1da177e4
LT
877};
878
a36cf844 879static void request_firmware_work_func(struct work_struct *work)
1da177e4 880{
a36cf844 881 struct firmware_work *fw_work;
1da177e4 882 const struct firmware *fw;
dddb5549 883 struct firmware_priv *fw_priv;
9b78c1da 884 long timeout;
113fab13 885 int ret;
f8a4bd34 886
a36cf844 887 fw_work = container_of(work, struct firmware_work, work);
dddb5549
SB
888 fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
889 fw_work->uevent, true);
890 if (IS_ERR_OR_NULL(fw_priv)) {
891 ret = PTR_RET(fw_priv);
811fa400 892 goto out;
dddb5549 893 }
811fa400 894
9b78c1da
RW
895 timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
896 if (timeout) {
dddb5549 897 ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
9b78c1da
RW
898 usermodehelper_read_unlock();
899 } else {
900 dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
901 fw_work->name);
902 ret = -EAGAIN;
903 }
811fa400
RW
904 if (ret)
905 _request_firmware_cleanup(&fw);
906
907 out:
9ebfbd45 908 fw_work->cont(fw, fw_work->context);
0cfc1e1e 909 put_device(fw_work->device);
9ebfbd45 910
1da177e4
LT
911 module_put(fw_work->module);
912 kfree(fw_work);
1da177e4
LT
913}
914
915/**
3c31f07a 916 * request_firmware_nowait - asynchronous version of request_firmware
eb8e3179 917 * @module: module requesting the firmware
312c004d 918 * @uevent: sends uevent to copy the firmware image if this flag
eb8e3179
RD
919 * is non-zero else the firmware copy must be done manually.
920 * @name: name of firmware file
921 * @device: device for which firmware is being loaded
9ebfbd45 922 * @gfp: allocation flags
eb8e3179
RD
923 * @context: will be passed over to @cont, and
924 * @fw may be %NULL if firmware request fails.
925 * @cont: function will be called asynchronously when the firmware
926 * request is over.
1da177e4 927 *
0cfc1e1e
ML
928 * Caller must hold the reference count of @device.
929 *
6f21a62a
ML
930 * Asynchronous variant of request_firmware() for user contexts:
931 * - sleep for as small periods as possible since it may
932 * increase kernel boot time of built-in device drivers
933 * requesting firmware in their ->probe() methods, if
934 * @gfp is GFP_KERNEL.
935 *
936 * - can't sleep at all if @gfp is GFP_ATOMIC.
1da177e4
LT
937 **/
938int
939request_firmware_nowait(
072fc8f0 940 struct module *module, bool uevent,
9ebfbd45 941 const char *name, struct device *device, gfp_t gfp, void *context,
1da177e4
LT
942 void (*cont)(const struct firmware *fw, void *context))
943{
f8a4bd34 944 struct firmware_work *fw_work;
1da177e4 945
f8a4bd34 946 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
1da177e4
LT
947 if (!fw_work)
948 return -ENOMEM;
f8a4bd34
DT
949
950 fw_work->module = module;
951 fw_work->name = name;
952 fw_work->device = device;
953 fw_work->context = context;
954 fw_work->cont = cont;
955 fw_work->uevent = uevent;
956
1da177e4
LT
957 if (!try_module_get(module)) {
958 kfree(fw_work);
959 return -EFAULT;
960 }
961
0cfc1e1e 962 get_device(fw_work->device);
a36cf844
SB
963 INIT_WORK(&fw_work->work, request_firmware_work_func);
964 schedule_work(&fw_work->work);
1da177e4
LT
965 return 0;
966}
967
2887b395
ML
968/**
969 * cache_firmware - cache one firmware image in kernel memory space
970 * @fw_name: the firmware image name
971 *
972 * Cache firmware in kernel memory so that drivers can use it when
973 * system isn't ready for them to request firmware image from userspace.
974 * Once it returns successfully, driver can use request_firmware or its
975 * nowait version to get the cached firmware without any interacting
976 * with userspace
977 *
978 * Return 0 if the firmware image has been cached successfully
979 * Return !0 otherwise
980 *
981 */
982int cache_firmware(const char *fw_name)
983{
984 int ret;
985 const struct firmware *fw;
986
987 pr_debug("%s: %s\n", __func__, fw_name);
988
989 ret = request_firmware(&fw, fw_name, NULL);
990 if (!ret)
991 kfree(fw);
992
993 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
994
995 return ret;
996}
997
998/**
999 * uncache_firmware - remove one cached firmware image
1000 * @fw_name: the firmware image name
1001 *
1002 * Uncache one firmware image which has been cached successfully
1003 * before.
1004 *
1005 * Return 0 if the firmware cache has been removed successfully
1006 * Return !0 otherwise
1007 *
1008 */
1009int uncache_firmware(const char *fw_name)
1010{
1011 struct firmware_buf *buf;
1012 struct firmware fw;
1013
1014 pr_debug("%s: %s\n", __func__, fw_name);
1015
1016 if (fw_get_builtin_firmware(&fw, fw_name))
1017 return 0;
1018
1019 buf = fw_lookup_buf(fw_name);
1020 if (buf) {
1021 fw_free_buf(buf);
1022 return 0;
1023 }
1024
1025 return -EINVAL;
1026}
1027
37276a51
ML
1028static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1029{
1030 struct fw_cache_entry *fce;
1031
1032 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1033 if (!fce)
1034 goto exit;
1035
1036 strcpy(fce->name, name);
1037exit:
1038 return fce;
1039}
1040
1041static void free_fw_cache_entry(struct fw_cache_entry *fce)
1042{
1043 kfree(fce);
1044}
1045
1046static void __async_dev_cache_fw_image(void *fw_entry,
1047 async_cookie_t cookie)
1048{
1049 struct fw_cache_entry *fce = fw_entry;
1050 struct firmware_cache *fwc = &fw_cache;
1051 int ret;
1052
1053 ret = cache_firmware(fce->name);
1054 if (ret)
1055 goto free;
1056
1057 spin_lock(&fwc->name_lock);
1058 list_add(&fce->list, &fwc->fw_names);
1059 spin_unlock(&fwc->name_lock);
1060 goto drop_ref;
1061
1062free:
1063 free_fw_cache_entry(fce);
1064drop_ref:
1065 spin_lock(&fwc->name_lock);
1066 fwc->cnt--;
1067 spin_unlock(&fwc->name_lock);
1068
1069 wake_up(&fwc->wait_queue);
1070}
1071
1072/* called with dev->devres_lock held */
1073static void dev_create_fw_entry(struct device *dev, void *res,
1074 void *data)
1075{
1076 struct fw_name_devm *fwn = res;
1077 const char *fw_name = fwn->name;
1078 struct list_head *head = data;
1079 struct fw_cache_entry *fce;
1080
1081 fce = alloc_fw_cache_entry(fw_name);
1082 if (fce)
1083 list_add(&fce->list, head);
1084}
1085
1086static int devm_name_match(struct device *dev, void *res,
1087 void *match_data)
1088{
1089 struct fw_name_devm *fwn = res;
1090 return (fwn->magic == (unsigned long)match_data);
1091}
1092
1093static void dev_cache_fw_image(struct device *dev)
1094{
1095 LIST_HEAD(todo);
1096 struct fw_cache_entry *fce;
1097 struct fw_cache_entry *fce_next;
1098 struct firmware_cache *fwc = &fw_cache;
1099
1100 devres_for_each_res(dev, fw_name_devm_release,
1101 devm_name_match, &fw_cache,
1102 dev_create_fw_entry, &todo);
1103
1104 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1105 list_del(&fce->list);
1106
1107 spin_lock(&fwc->name_lock);
1108 fwc->cnt++;
1109 spin_unlock(&fwc->name_lock);
1110
1111 async_schedule(__async_dev_cache_fw_image, (void *)fce);
1112 }
1113}
1114
1115static void __device_uncache_fw_images(void)
1116{
1117 struct firmware_cache *fwc = &fw_cache;
1118 struct fw_cache_entry *fce;
1119
1120 spin_lock(&fwc->name_lock);
1121 while (!list_empty(&fwc->fw_names)) {
1122 fce = list_entry(fwc->fw_names.next,
1123 struct fw_cache_entry, list);
1124 list_del(&fce->list);
1125 spin_unlock(&fwc->name_lock);
1126
1127 uncache_firmware(fce->name);
1128 free_fw_cache_entry(fce);
1129
1130 spin_lock(&fwc->name_lock);
1131 }
1132 spin_unlock(&fwc->name_lock);
1133}
1134
1135/**
1136 * device_cache_fw_images - cache devices' firmware
1137 *
1138 * If one device called request_firmware or its nowait version
1139 * successfully before, the firmware names are recored into the
1140 * device's devres link list, so device_cache_fw_images can call
1141 * cache_firmware() to cache these firmwares for the device,
1142 * then the device driver can load its firmwares easily at
1143 * time when system is not ready to complete loading firmware.
1144 */
1145static void device_cache_fw_images(void)
1146{
1147 struct firmware_cache *fwc = &fw_cache;
1148 struct device *dev;
ffe53f6f 1149 int old_timeout;
37276a51
ML
1150 DEFINE_WAIT(wait);
1151
1152 pr_debug("%s\n", __func__);
1153
ffe53f6f
ML
1154 /*
1155 * use small loading timeout for caching devices' firmware
1156 * because all these firmware images have been loaded
1157 * successfully at lease once, also system is ready for
1158 * completing firmware loading now. The maximum size of
1159 * firmware in current distributions is about 2M bytes,
1160 * so 10 secs should be enough.
1161 */
1162 old_timeout = loading_timeout;
1163 loading_timeout = 10;
1164
37276a51
ML
1165 device_pm_lock();
1166 list_for_each_entry(dev, &dpm_list, power.entry)
1167 dev_cache_fw_image(dev);
1168 device_pm_unlock();
1169
1170 /* wait for completion of caching firmware for all devices */
1171 spin_lock(&fwc->name_lock);
1172 for (;;) {
1173 prepare_to_wait(&fwc->wait_queue, &wait,
1174 TASK_UNINTERRUPTIBLE);
1175 if (!fwc->cnt)
1176 break;
1177
1178 spin_unlock(&fwc->name_lock);
1179
1180 schedule();
1181
1182 spin_lock(&fwc->name_lock);
1183 }
1184 spin_unlock(&fwc->name_lock);
1185 finish_wait(&fwc->wait_queue, &wait);
ffe53f6f
ML
1186
1187 loading_timeout = old_timeout;
37276a51
ML
1188}
1189
1190/**
1191 * device_uncache_fw_images - uncache devices' firmware
1192 *
1193 * uncache all firmwares which have been cached successfully
1194 * by device_uncache_fw_images earlier
1195 */
1196static void device_uncache_fw_images(void)
1197{
1198 pr_debug("%s\n", __func__);
1199 __device_uncache_fw_images();
1200}
1201
1202static void device_uncache_fw_images_work(struct work_struct *work)
1203{
1204 device_uncache_fw_images();
1205}
1206
1207/**
1208 * device_uncache_fw_images_delay - uncache devices firmwares
1209 * @delay: number of milliseconds to delay uncache device firmwares
1210 *
1211 * uncache all devices's firmwares which has been cached successfully
1212 * by device_cache_fw_images after @delay milliseconds.
1213 */
1214static void device_uncache_fw_images_delay(unsigned long delay)
1215{
1216 schedule_delayed_work(&fw_cache.work,
1217 msecs_to_jiffies(delay));
1218}
1219
1220static void __init fw_cache_init(void)
1221{
1222 spin_lock_init(&fw_cache.lock);
1223 INIT_LIST_HEAD(&fw_cache.head);
1224
1225 spin_lock_init(&fw_cache.name_lock);
1226 INIT_LIST_HEAD(&fw_cache.fw_names);
1227 fw_cache.cnt = 0;
1228
1229 init_waitqueue_head(&fw_cache.wait_queue);
1230 INIT_DELAYED_WORK(&fw_cache.work,
1231 device_uncache_fw_images_work);
1232}
1233
673fae90 1234static int __init firmware_class_init(void)
1da177e4 1235{
1f2b7959 1236 fw_cache_init();
673fae90 1237 return class_register(&firmware_class);
1da177e4 1238}
673fae90
DT
1239
1240static void __exit firmware_class_exit(void)
1da177e4
LT
1241{
1242 class_unregister(&firmware_class);
1243}
1244
a30a6a2c 1245fs_initcall(firmware_class_init);
1da177e4
LT
1246module_exit(firmware_class_exit);
1247
1248EXPORT_SYMBOL(release_firmware);
1249EXPORT_SYMBOL(request_firmware);
1250EXPORT_SYMBOL(request_firmware_nowait);
2887b395
ML
1251EXPORT_SYMBOL_GPL(cache_firmware);
1252EXPORT_SYMBOL_GPL(uncache_firmware);
This page took 0.729123 seconds and 5 git commands to generate.