blob: 01e21037d8feb5c04e6aedee608e9cdedc0d87bd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * firmware_class.c - Multi purpose firmware loading support
3 *
Markus Rechberger87d37a42007-06-04 18:45:44 +02004 * Copyright (c) 2003 Manuel Estrada Sainz
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Please see Documentation/firmware_class/ for more information.
7 *
8 */
9
Randy.Dunlapc59ede72006-01-11 12:17:46 -080010#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#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>
Laura Garciacad1e552006-05-23 23:22:38 +020018#include <linux/mutex.h>
Stephen Boyda36cf842012-03-28 23:31:00 +020019#include <linux/workqueue.h>
David Woodhouse6e03a202009-04-09 22:04:07 -070020#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/firmware.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Stephen Boyda36cf842012-03-28 23:31:00 +020023#include <linux/sched.h>
Linus Torvaldsabb139e2012-10-03 15:58:32 -070024#include <linux/file.h>
Ming Lei1f2b7952012-08-04 12:01:21 +080025#include <linux/list.h>
Ming Lei37276a52012-08-04 12:01:27 +080026#include <linux/async.h>
27#include <linux/pm.h>
Ming Lei07646d92012-08-04 12:01:29 +080028#include <linux/suspend.h>
Ming Leiac39b3e2012-08-20 19:04:16 +080029#include <linux/syscore_ops.h>
Ming Lei37276a52012-08-04 12:01:27 +080030
Linus Torvaldsabb139e2012-10-03 15:58:32 -070031#include <generated/utsrelease.h>
32
Ming Lei37276a52012-08-04 12:01:27 +080033#include "base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Markus Rechberger87d37a42007-06-04 18:45:44 +020035MODULE_AUTHOR("Manuel Estrada Sainz");
Linus Torvalds1da177e2005-04-16 15:20:36 -070036MODULE_DESCRIPTION("Multi purpose firmware loading support");
37MODULE_LICENSE("GPL");
38
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080039/* Builtin firmware support */
40
41#ifdef CONFIG_FW_LOADER
42
43extern struct builtin_fw __start_builtin_fw[];
44extern struct builtin_fw __end_builtin_fw[];
45
46static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
47{
48 struct builtin_fw *b_fw;
49
50 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
51 if (strcmp(name, b_fw->name) == 0) {
52 fw->size = b_fw->size;
53 fw->data = b_fw->data;
54 return true;
55 }
56 }
57
58 return false;
59}
60
61static bool fw_is_builtin_firmware(const struct firmware *fw)
62{
63 struct builtin_fw *b_fw;
64
65 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
66 if (fw->data == b_fw->data)
67 return true;
68
69 return false;
70}
71
72#else /* Module case - no builtin firmware support */
73
74static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
75{
76 return false;
77}
78
79static inline bool fw_is_builtin_firmware(const struct firmware *fw)
80{
81 return false;
82}
83#endif
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085enum {
86 FW_STATUS_LOADING,
87 FW_STATUS_DONE,
88 FW_STATUS_ABORT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070089};
90
Dave Jones2f651682007-01-25 15:56:15 -050091static int loading_timeout = 60; /* In seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +020093static inline long firmware_loading_timeout(void)
94{
95 return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
96}
97
Ming Lei1f2b7952012-08-04 12:01:21 +080098struct firmware_cache {
99 /* firmware_buf instance will be added into the below list */
100 spinlock_t lock;
101 struct list_head head;
Ming Leicfe016b2012-09-08 17:32:30 +0800102 int state;
Ming Lei37276a52012-08-04 12:01:27 +0800103
Ming Leicfe016b2012-09-08 17:32:30 +0800104#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +0800105 /*
106 * Names of firmware images which have been cached successfully
107 * will be added into the below list so that device uncache
108 * helper can trace which firmware images have been cached
109 * before.
110 */
111 spinlock_t name_lock;
112 struct list_head fw_names;
113
Ming Lei37276a52012-08-04 12:01:27 +0800114 struct delayed_work work;
Ming Lei07646d92012-08-04 12:01:29 +0800115
116 struct notifier_block pm_notify;
Ming Leicfe016b2012-09-08 17:32:30 +0800117#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800118};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Ming Lei12446912012-08-04 12:01:20 +0800120struct firmware_buf {
Ming Lei1f2b7952012-08-04 12:01:21 +0800121 struct kref ref;
122 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 struct completion completion;
Ming Lei1f2b7952012-08-04 12:01:21 +0800124 struct firmware_cache *fwc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 unsigned long status;
Ming Lei65710cb2012-08-04 12:01:16 +0800126 void *data;
127 size_t size;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100128#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100129 bool is_paged_buf;
David Woodhouse6e03a202009-04-09 22:04:07 -0700130 struct page **pages;
131 int nr_pages;
132 int page_array_size;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100133#endif
Dmitry Torokhove1771232010-03-13 23:49:23 -0800134 char fw_id[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136
Ming Lei37276a52012-08-04 12:01:27 +0800137struct fw_cache_entry {
138 struct list_head list;
139 char name[];
140};
141
Ming Leif531f05a2012-08-04 12:01:25 +0800142struct fw_name_devm {
143 unsigned long magic;
144 char name[];
145};
146
Ming Lei1f2b7952012-08-04 12:01:21 +0800147#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
148
Ming Leiac39b3e2012-08-20 19:04:16 +0800149#define FW_LOADER_NO_CACHE 0
150#define FW_LOADER_START_CACHE 1
151
152static int fw_cache_piggyback_on_request(const char *name);
153
Ming Lei1f2b7952012-08-04 12:01:21 +0800154/* fw_lock could be moved to 'struct firmware_priv' but since it is just
155 * guarding for corner cases a global lock should be OK */
156static DEFINE_MUTEX(fw_lock);
157
158static struct firmware_cache fw_cache;
159
160static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
161 struct firmware_cache *fwc)
162{
163 struct firmware_buf *buf;
164
165 buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
166
167 if (!buf)
168 return buf;
169
170 kref_init(&buf->ref);
171 strcpy(buf->fw_id, fw_name);
172 buf->fwc = fwc;
173 init_completion(&buf->completion);
174
175 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
176
177 return buf;
178}
179
Ming Lei2887b392012-08-04 12:01:22 +0800180static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
181{
182 struct firmware_buf *tmp;
183 struct firmware_cache *fwc = &fw_cache;
184
185 list_for_each_entry(tmp, &fwc->head, list)
186 if (!strcmp(tmp->fw_id, fw_name))
187 return tmp;
188 return NULL;
189}
190
Ming Lei1f2b7952012-08-04 12:01:21 +0800191static int fw_lookup_and_allocate_buf(const char *fw_name,
192 struct firmware_cache *fwc,
193 struct firmware_buf **buf)
194{
195 struct firmware_buf *tmp;
196
197 spin_lock(&fwc->lock);
Ming Lei2887b392012-08-04 12:01:22 +0800198 tmp = __fw_lookup_buf(fw_name);
199 if (tmp) {
200 kref_get(&tmp->ref);
201 spin_unlock(&fwc->lock);
202 *buf = tmp;
203 return 1;
204 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800205 tmp = __allocate_fw_buf(fw_name, fwc);
206 if (tmp)
207 list_add(&tmp->list, &fwc->head);
208 spin_unlock(&fwc->lock);
209
210 *buf = tmp;
211
212 return tmp ? 0 : -ENOMEM;
213}
214
Ming Lei2887b392012-08-04 12:01:22 +0800215static struct firmware_buf *fw_lookup_buf(const char *fw_name)
216{
217 struct firmware_buf *tmp;
218 struct firmware_cache *fwc = &fw_cache;
219
220 spin_lock(&fwc->lock);
221 tmp = __fw_lookup_buf(fw_name);
222 spin_unlock(&fwc->lock);
223
224 return tmp;
225}
226
Ming Lei1f2b7952012-08-04 12:01:21 +0800227static void __fw_free_buf(struct kref *ref)
228{
229 struct firmware_buf *buf = to_fwbuf(ref);
230 struct firmware_cache *fwc = buf->fwc;
Ming Lei1f2b7952012-08-04 12:01:21 +0800231
232 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
233 __func__, buf->fw_id, buf, buf->data,
234 (unsigned int)buf->size);
235
Ming Lei1f2b7952012-08-04 12:01:21 +0800236 list_del(&buf->list);
237 spin_unlock(&fwc->lock);
238
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100239#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100240 if (buf->is_paged_buf) {
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100241 int i;
Ming Lei746058f2012-10-09 12:01:03 +0800242 vunmap(buf->data);
243 for (i = 0; i < buf->nr_pages; i++)
244 __free_page(buf->pages[i]);
245 kfree(buf->pages);
246 } else
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100247#endif
Ming Lei746058f2012-10-09 12:01:03 +0800248 vfree(buf->data);
Ming Lei1f2b7952012-08-04 12:01:21 +0800249 kfree(buf);
250}
251
252static void fw_free_buf(struct firmware_buf *buf)
253{
Chuansheng Liubd9eb7f2012-11-10 01:27:22 +0800254 struct firmware_cache *fwc = buf->fwc;
255 spin_lock(&fwc->lock);
256 if (!kref_put(&buf->ref, __fw_free_buf))
257 spin_unlock(&fwc->lock);
Ming Lei1f2b7952012-08-04 12:01:21 +0800258}
259
Ming Lei746058f2012-10-09 12:01:03 +0800260/* direct firmware loading support */
Ming Lei27602842012-11-03 17:47:58 +0800261static char fw_path_para[256];
262static const char * const fw_path[] = {
263 fw_path_para,
Ming Lei746058f2012-10-09 12:01:03 +0800264 "/lib/firmware/updates/" UTS_RELEASE,
265 "/lib/firmware/updates",
266 "/lib/firmware/" UTS_RELEASE,
267 "/lib/firmware"
268};
269
Ming Lei27602842012-11-03 17:47:58 +0800270/*
271 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
272 * from kernel command line because firmware_class is generally built in
273 * kernel instead of module.
274 */
275module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
276MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
277
Ming Lei746058f2012-10-09 12:01:03 +0800278/* Don't inline this: 'struct kstat' is biggish */
Cesar Eduardo Barros60dac5e2012-10-27 20:37:10 -0200279static noinline_for_stack long fw_file_size(struct file *file)
Ming Lei746058f2012-10-09 12:01:03 +0800280{
281 struct kstat st;
Al Viro3dadecc2013-01-24 02:18:08 -0500282 if (vfs_getattr(&file->f_path, &st))
Ming Lei746058f2012-10-09 12:01:03 +0800283 return -1;
284 if (!S_ISREG(st.mode))
285 return -1;
286 if (st.size != (long)st.size)
287 return -1;
288 return st.size;
289}
290
291static bool fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf)
292{
293 long size;
294 char *buf;
295
296 size = fw_file_size(file);
Luciano Coelho4adf07f2013-01-15 10:43:43 +0200297 if (size <= 0)
Ming Lei746058f2012-10-09 12:01:03 +0800298 return false;
299 buf = vmalloc(size);
300 if (!buf)
301 return false;
302 if (kernel_read(file, 0, buf, size) != size) {
303 vfree(buf);
304 return false;
305 }
306 fw_buf->data = buf;
307 fw_buf->size = size;
308 return true;
309}
310
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100311static bool fw_get_filesystem_firmware(struct device *device,
312 struct firmware_buf *buf)
Ming Lei746058f2012-10-09 12:01:03 +0800313{
314 int i;
315 bool success = false;
316 char *path = __getname();
317
318 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
319 struct file *file;
Ming Lei27602842012-11-03 17:47:58 +0800320
321 /* skip the unset customized path */
322 if (!fw_path[i][0])
323 continue;
324
Ming Lei746058f2012-10-09 12:01:03 +0800325 snprintf(path, PATH_MAX, "%s/%s", fw_path[i], buf->fw_id);
326
327 file = filp_open(path, O_RDONLY, 0);
328 if (IS_ERR(file))
329 continue;
330 success = fw_read_file_contents(file, buf);
331 fput(file);
332 if (success)
333 break;
334 }
335 __putname(path);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100336
337 if (success) {
338 dev_dbg(device, "firmware: direct-loading firmware %s\n",
339 buf->fw_id);
340 mutex_lock(&fw_lock);
341 set_bit(FW_STATUS_DONE, &buf->status);
342 complete_all(&buf->completion);
343 mutex_unlock(&fw_lock);
344 }
345
Ming Lei746058f2012-10-09 12:01:03 +0800346 return success;
347}
348
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100349/* firmware holds the ownership of pages */
350static void firmware_free_data(const struct firmware *fw)
351{
352 /* Loaded directly? */
353 if (!fw->priv) {
354 vfree(fw->data);
355 return;
356 }
357 fw_free_buf(fw->priv);
358}
359
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100360/* store the pages buffer info firmware from buf */
361static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
362{
363 fw->priv = buf;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100364#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100365 fw->pages = buf->pages;
366#endif
367 fw->size = buf->size;
368 fw->data = buf->data;
369
370 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
371 __func__, buf->fw_id, buf, buf->data,
372 (unsigned int)buf->size);
373}
374
375#ifdef CONFIG_PM_SLEEP
376static void fw_name_devm_release(struct device *dev, void *res)
377{
378 struct fw_name_devm *fwn = res;
379
380 if (fwn->magic == (unsigned long)&fw_cache)
381 pr_debug("%s: fw_name-%s devm-%p released\n",
382 __func__, fwn->name, res);
383}
384
385static int fw_devm_match(struct device *dev, void *res,
386 void *match_data)
387{
388 struct fw_name_devm *fwn = res;
389
390 return (fwn->magic == (unsigned long)&fw_cache) &&
391 !strcmp(fwn->name, match_data);
392}
393
394static struct fw_name_devm *fw_find_devm_name(struct device *dev,
395 const char *name)
396{
397 struct fw_name_devm *fwn;
398
399 fwn = devres_find(dev, fw_name_devm_release,
400 fw_devm_match, (void *)name);
401 return fwn;
402}
403
404/* add firmware name into devres list */
405static int fw_add_devm_name(struct device *dev, const char *name)
406{
407 struct fw_name_devm *fwn;
408
409 fwn = fw_find_devm_name(dev, name);
410 if (fwn)
411 return 1;
412
413 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
414 strlen(name) + 1, GFP_KERNEL);
415 if (!fwn)
416 return -ENOMEM;
417
418 fwn->magic = (unsigned long)&fw_cache;
419 strcpy(fwn->name, name);
420 devres_add(dev, fwn);
421
422 return 0;
423}
424#else
425static int fw_add_devm_name(struct device *dev, const char *name)
426{
427 return 0;
428}
429#endif
430
431
432/*
433 * user-mode helper code
434 */
435#ifdef CONFIG_FW_LOADER_USER_HELPER
436struct firmware_priv {
437 struct delayed_work timeout_work;
438 bool nowait;
439 struct device dev;
440 struct firmware_buf *buf;
441 struct firmware *fw;
442};
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100443
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700444static struct firmware_priv *to_firmware_priv(struct device *dev)
445{
446 return container_of(dev, struct firmware_priv, dev);
447}
448
449static void fw_load_abort(struct firmware_priv *fw_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Ming Lei12446912012-08-04 12:01:20 +0800451 struct firmware_buf *buf = fw_priv->buf;
452
Ming Lei87597932013-06-15 16:36:38 +0800453 /*
454 * There is a small window in which user can write to 'loading'
455 * between loading done and disappearance of 'loading'
456 */
457 if (test_bit(FW_STATUS_DONE, &buf->status))
458 return;
459
Ming Lei12446912012-08-04 12:01:20 +0800460 set_bit(FW_STATUS_ABORT, &buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800461 complete_all(&buf->completion);
Ming Lei87597932013-06-15 16:36:38 +0800462
463 /* avoid user action after loading abort */
464 fw_priv->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Takashi Iwai807be032013-01-31 11:13:57 +0100467#define is_fw_load_aborted(buf) \
468 test_bit(FW_STATUS_ABORT, &(buf)->status)
469
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700470static ssize_t firmware_timeout_show(struct class *class,
471 struct class_attribute *attr,
472 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 return sprintf(buf, "%d\n", loading_timeout);
475}
476
477/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800478 * firmware_timeout_store - set number of seconds to wait for firmware
479 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800480 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800481 * @buf: buffer to scan for timeout value
482 * @count: number of bytes in @buf
483 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800485 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 * firmware will be provided.
487 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800488 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700490static ssize_t firmware_timeout_store(struct class *class,
491 struct class_attribute *attr,
492 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700495 if (loading_timeout < 0)
496 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return count;
499}
500
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800501static struct class_attribute firmware_class_attrs[] = {
502 __ATTR(timeout, S_IWUSR | S_IRUGO,
503 firmware_timeout_show, firmware_timeout_store),
504 __ATTR_NULL
505};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800507static void fw_dev_release(struct device *dev)
508{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700509 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800510
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800511 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800512
513 module_put(THIS_MODULE);
514}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Kay Sievers7eff2e72007-08-14 15:15:12 +0200516static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700518 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Ming Lei12446912012-08-04 12:01:20 +0800520 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200522 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700523 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200524 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
525 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 return 0;
528}
529
Adrian Bunk1b81d662006-05-20 15:00:16 -0700530static struct class firmware_class = {
531 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800532 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700533 .dev_uevent = firmware_uevent,
534 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700535};
536
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700537static ssize_t firmware_loading_show(struct device *dev,
538 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700540 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800541 int loading = 0;
542
543 mutex_lock(&fw_lock);
544 if (fw_priv->buf)
545 loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
546 mutex_unlock(&fw_lock);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return sprintf(buf, "%d\n", loading);
549}
550
David Woodhouse6e03a202009-04-09 22:04:07 -0700551/* Some architectures don't have PAGE_KERNEL_RO */
552#ifndef PAGE_KERNEL_RO
553#define PAGE_KERNEL_RO PAGE_KERNEL
554#endif
Ming Lei253c9242012-10-09 12:01:02 +0800555
556/* one pages buffer should be mapped/unmapped only once */
557static int fw_map_pages_buf(struct firmware_buf *buf)
558{
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100559 if (!buf->is_paged_buf)
Ming Lei746058f2012-10-09 12:01:03 +0800560 return 0;
561
Ming Lei253c9242012-10-09 12:01:02 +0800562 if (buf->data)
563 vunmap(buf->data);
564 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
565 if (!buf->data)
566 return -ENOMEM;
567 return 0;
568}
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800571 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700572 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800573 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800574 * @buf: buffer to scan for loading control value
575 * @count: number of bytes in @buf
576 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 * The relevant values are:
578 *
579 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800580 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 * -1: Conclude the load with an error and discard any written data.
582 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700583static ssize_t firmware_loading_store(struct device *dev,
584 struct device_attribute *attr,
585 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700587 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800588 struct firmware_buf *fw_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700590 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Neil Hormaneea915b2012-01-02 15:31:23 -0500592 mutex_lock(&fw_lock);
Ming Lei87597932013-06-15 16:36:38 +0800593 fw_buf = fw_priv->buf;
Ming Lei12446912012-08-04 12:01:20 +0800594 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500595 goto out;
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 switch (loading) {
598 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800599 /* discarding any previous partial load */
Ming Lei12446912012-08-04 12:01:20 +0800600 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
601 for (i = 0; i < fw_buf->nr_pages; i++)
602 __free_page(fw_buf->pages[i]);
603 kfree(fw_buf->pages);
604 fw_buf->pages = NULL;
605 fw_buf->page_array_size = 0;
606 fw_buf->nr_pages = 0;
607 set_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei28eefa72012-08-04 12:01:17 +0800608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 break;
610 case 0:
Ming Lei12446912012-08-04 12:01:20 +0800611 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
612 set_bit(FW_STATUS_DONE, &fw_buf->status);
613 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei253c9242012-10-09 12:01:02 +0800614
615 /*
616 * Several loading requests may be pending on
617 * one same firmware buf, so let all requests
618 * see the mapped 'buf->data' once the loading
619 * is completed.
620 * */
621 fw_map_pages_buf(fw_buf);
Ming Lei1f2b7952012-08-04 12:01:21 +0800622 complete_all(&fw_buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 break;
624 }
625 /* fallthrough */
626 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700627 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 /* fallthrough */
629 case -1:
630 fw_load_abort(fw_priv);
631 break;
632 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500633out:
634 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return count;
636}
637
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700638static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700640static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
641 struct bin_attribute *bin_attr,
642 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200644 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700645 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800646 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700647 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Laura Garciacad1e552006-05-23 23:22:38 +0200649 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800650 buf = fw_priv->buf;
651 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 ret_count = -ENODEV;
653 goto out;
654 }
Ming Lei12446912012-08-04 12:01:20 +0800655 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200656 ret_count = 0;
657 goto out;
658 }
Ming Lei12446912012-08-04 12:01:20 +0800659 if (count > buf->size - offset)
660 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700661
662 ret_count = count;
663
664 while (count) {
665 void *page_data;
666 int page_nr = offset >> PAGE_SHIFT;
667 int page_ofs = offset & (PAGE_SIZE-1);
668 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
669
Ming Lei12446912012-08-04 12:01:20 +0800670 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700671
672 memcpy(buffer, page_data + page_ofs, page_cnt);
673
Ming Lei12446912012-08-04 12:01:20 +0800674 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700675 buffer += page_cnt;
676 offset += page_cnt;
677 count -= page_cnt;
678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679out:
Laura Garciacad1e552006-05-23 23:22:38 +0200680 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return ret_count;
682}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800683
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700684static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Ming Lei12446912012-08-04 12:01:20 +0800686 struct firmware_buf *buf = fw_priv->buf;
David Woodhouse6e03a202009-04-09 22:04:07 -0700687 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
David Woodhouse6e03a202009-04-09 22:04:07 -0700689 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800690 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700691 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800692 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700693 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
David Woodhouse6e03a202009-04-09 22:04:07 -0700695 new_pages = kmalloc(new_array_size * sizeof(void *),
696 GFP_KERNEL);
697 if (!new_pages) {
698 fw_load_abort(fw_priv);
699 return -ENOMEM;
700 }
Ming Lei12446912012-08-04 12:01:20 +0800701 memcpy(new_pages, buf->pages,
702 buf->page_array_size * sizeof(void *));
703 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
704 (new_array_size - buf->page_array_size));
705 kfree(buf->pages);
706 buf->pages = new_pages;
707 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700709
Ming Lei12446912012-08-04 12:01:20 +0800710 while (buf->nr_pages < pages_needed) {
711 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700712 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
713
Ming Lei12446912012-08-04 12:01:20 +0800714 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700715 fw_load_abort(fw_priv);
716 return -ENOMEM;
717 }
Ming Lei12446912012-08-04 12:01:20 +0800718 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return 0;
721}
722
723/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800724 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700725 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700726 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700727 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800728 * @buffer: buffer being written
729 * @offset: buffer offset for write in total data store area
730 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800732 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 * the driver as a firmware image.
734 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700735static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
736 struct bin_attribute *bin_attr,
737 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200739 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700740 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800741 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 ssize_t retval;
743
744 if (!capable(CAP_SYS_RAWIO))
745 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700746
Laura Garciacad1e552006-05-23 23:22:38 +0200747 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800748 buf = fw_priv->buf;
749 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 retval = -ENODEV;
751 goto out;
752 }
Ming Lei65710cb2012-08-04 12:01:16 +0800753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 retval = fw_realloc_buffer(fw_priv, offset + count);
755 if (retval)
756 goto out;
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700759
760 while (count) {
761 void *page_data;
762 int page_nr = offset >> PAGE_SHIFT;
763 int page_ofs = offset & (PAGE_SIZE - 1);
764 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
765
Ming Lei12446912012-08-04 12:01:20 +0800766 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700767
768 memcpy(page_data + page_ofs, buffer, page_cnt);
769
Ming Lei12446912012-08-04 12:01:20 +0800770 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700771 buffer += page_cnt;
772 offset += page_cnt;
773 count -= page_cnt;
774 }
775
Ming Lei12446912012-08-04 12:01:20 +0800776 buf->size = max_t(size_t, offset, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777out:
Laura Garciacad1e552006-05-23 23:22:38 +0200778 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return retval;
780}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800781
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700782static struct bin_attribute firmware_attr_data = {
783 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 .size = 0,
785 .read = firmware_data_read,
786 .write = firmware_data_write,
787};
788
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800789static void firmware_class_timeout_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800791 struct firmware_priv *fw_priv = container_of(work,
792 struct firmware_priv, timeout_work.work);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700793
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800794 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 fw_load_abort(fw_priv);
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800796 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700799static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200800fw_create_instance(struct firmware *firmware, const char *fw_name,
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700801 struct device *device, bool uevent, bool nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700803 struct firmware_priv *fw_priv;
804 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Ming Lei12446912012-08-04 12:01:20 +0800806 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700807 if (!fw_priv) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700808 dev_err(device, "%s: kmalloc failed\n", __func__);
Ming Lei12446912012-08-04 12:01:20 +0800809 fw_priv = ERR_PTR(-ENOMEM);
810 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700813 fw_priv->nowait = nowait;
Ming Lei1f2b7952012-08-04 12:01:21 +0800814 fw_priv->fw = firmware;
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800815 INIT_DELAYED_WORK(&fw_priv->timeout_work,
816 firmware_class_timeout_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700818 f_dev = &fw_priv->dev;
819
820 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800821 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700822 f_dev->parent = device;
823 f_dev->class = &firmware_class;
Ming Lei12446912012-08-04 12:01:20 +0800824exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700825 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100827
828/* load a firmware via user helper */
829static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
830 long timeout)
831{
832 int retval = 0;
833 struct device *f_dev = &fw_priv->dev;
834 struct firmware_buf *buf = fw_priv->buf;
835
836 /* fall back on userspace loading */
837 buf->is_paged_buf = true;
838
839 dev_set_uevent_suppress(f_dev, true);
840
841 /* Need to pin this module until class device is destroyed */
842 __module_get(THIS_MODULE);
843
844 retval = device_add(f_dev);
845 if (retval) {
846 dev_err(f_dev, "%s: device_register failed\n", __func__);
847 goto err_put_dev;
848 }
849
850 retval = device_create_bin_file(f_dev, &firmware_attr_data);
851 if (retval) {
852 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
853 goto err_del_dev;
854 }
855
856 retval = device_create_file(f_dev, &dev_attr_loading);
857 if (retval) {
858 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
859 goto err_del_bin_attr;
860 }
861
862 if (uevent) {
863 dev_set_uevent_suppress(f_dev, false);
864 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
865 if (timeout != MAX_SCHEDULE_TIMEOUT)
866 schedule_delayed_work(&fw_priv->timeout_work, timeout);
867
868 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
869 }
870
871 wait_for_completion(&buf->completion);
872
873 cancel_delayed_work_sync(&fw_priv->timeout_work);
874
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100875 device_remove_file(f_dev, &dev_attr_loading);
876err_del_bin_attr:
877 device_remove_bin_file(f_dev, &firmware_attr_data);
878err_del_dev:
879 device_del(f_dev);
880err_put_dev:
881 put_device(f_dev);
882 return retval;
883}
884
885static int fw_load_from_user_helper(struct firmware *firmware,
886 const char *name, struct device *device,
887 bool uevent, bool nowait, long timeout)
888{
889 struct firmware_priv *fw_priv;
890
891 fw_priv = fw_create_instance(firmware, name, device, uevent, nowait);
892 if (IS_ERR(fw_priv))
893 return PTR_ERR(fw_priv);
894
895 fw_priv->buf = firmware->priv;
896 return _request_firmware_load(fw_priv, uevent, timeout);
897}
898#else /* CONFIG_FW_LOADER_USER_HELPER */
899static inline int
900fw_load_from_user_helper(struct firmware *firmware, const char *name,
901 struct device *device, bool uevent, bool nowait,
902 long timeout)
903{
904 return -ENOENT;
905}
Takashi Iwai807be032013-01-31 11:13:57 +0100906
907/* No abort during direct loading */
908#define is_fw_load_aborted(buf) false
909
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100910#endif /* CONFIG_FW_LOADER_USER_HELPER */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Ming Leif531f05a2012-08-04 12:01:25 +0800912
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100913/* wait until the shared firmware_buf becomes ready (or error) */
914static int sync_cached_firmware_buf(struct firmware_buf *buf)
Ming Lei1f2b7952012-08-04 12:01:21 +0800915{
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100916 int ret = 0;
917
918 mutex_lock(&fw_lock);
919 while (!test_bit(FW_STATUS_DONE, &buf->status)) {
Takashi Iwai807be032013-01-31 11:13:57 +0100920 if (is_fw_load_aborted(buf)) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100921 ret = -ENOENT;
922 break;
923 }
924 mutex_unlock(&fw_lock);
925 wait_for_completion(&buf->completion);
926 mutex_lock(&fw_lock);
927 }
928 mutex_unlock(&fw_lock);
929 return ret;
Ming Lei1f2b7952012-08-04 12:01:21 +0800930}
931
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100932/* prepare firmware and firmware_buf structs;
933 * return 0 if a firmware is already assigned, 1 if need to load one,
934 * or a negative error code
935 */
936static int
937_request_firmware_prepare(struct firmware **firmware_p, const char *name,
938 struct device *device)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700939{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +0800941 struct firmware_buf *buf;
942 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Jiri Slaby4aed0642005-09-13 01:25:01 -0700944 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700946 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
947 __func__);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100948 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800951 if (fw_get_builtin_firmware(firmware, name)) {
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100952 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100953 return 0; /* assigned */
David Woodhouse5658c762008-05-23 13:52:42 +0100954 }
955
Ming Lei1f2b7952012-08-04 12:01:21 +0800956 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
Ming Lei1f2b7952012-08-04 12:01:21 +0800957
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100958 /*
959 * bind with 'buf' now to avoid warning in failure path
960 * of requesting firmware.
961 */
962 firmware->priv = buf;
Ming Lei1f2b7952012-08-04 12:01:21 +0800963
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100964 if (ret > 0) {
965 ret = sync_cached_firmware_buf(buf);
966 if (!ret) {
967 fw_set_page_data(buf, firmware);
968 return 0; /* assigned */
969 }
Stephen Boyddddb5542012-03-28 23:30:43 +0200970 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800971
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100972 if (ret < 0)
973 return ret;
974 return 1; /* need to load */
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200975}
976
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100977static int assign_firmware_buf(struct firmware *fw, struct device *device)
978{
979 struct firmware_buf *buf = fw->priv;
980
981 mutex_lock(&fw_lock);
Takashi Iwai807be032013-01-31 11:13:57 +0100982 if (!buf->size || is_fw_load_aborted(buf)) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100983 mutex_unlock(&fw_lock);
984 return -ENOENT;
985 }
986
987 /*
988 * add firmware name into devres list so that we can auto cache
989 * and uncache firmware for device.
990 *
991 * device may has been deleted already, but the problem
992 * should be fixed in devres or driver core.
993 */
994 if (device)
995 fw_add_devm_name(device, buf->fw_id);
996
997 /*
998 * After caching firmware image is started, let it piggyback
999 * on request firmware.
1000 */
1001 if (buf->fwc->state == FW_LOADER_START_CACHE) {
1002 if (fw_cache_piggyback_on_request(buf->fw_id))
1003 kref_get(&buf->ref);
1004 }
1005
1006 /* pass the pages buffer to driver at the last minute */
1007 fw_set_page_data(buf, fw);
1008 mutex_unlock(&fw_lock);
1009 return 0;
1010}
1011
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001012/* called from request_firmware() and request_firmware_work_func() */
1013static int
1014_request_firmware(const struct firmware **firmware_p, const char *name,
1015 struct device *device, bool uevent, bool nowait)
1016{
1017 struct firmware *fw;
1018 long timeout;
1019 int ret;
1020
1021 if (!firmware_p)
1022 return -EINVAL;
1023
1024 ret = _request_firmware_prepare(&fw, name, device);
1025 if (ret <= 0) /* error or already assigned */
1026 goto out;
1027
1028 ret = 0;
1029 timeout = firmware_loading_timeout();
1030 if (nowait) {
1031 timeout = usermodehelper_read_lock_wait(timeout);
1032 if (!timeout) {
1033 dev_dbg(device, "firmware: %s loading timed out\n",
1034 name);
1035 ret = -EBUSY;
1036 goto out;
1037 }
1038 } else {
1039 ret = usermodehelper_read_trylock();
1040 if (WARN_ON(ret)) {
1041 dev_err(device, "firmware: %s will not be loaded\n",
1042 name);
1043 goto out;
1044 }
1045 }
1046
1047 if (!fw_get_filesystem_firmware(device, fw->priv))
1048 ret = fw_load_from_user_helper(fw, name, device,
1049 uevent, nowait, timeout);
1050 if (!ret)
1051 ret = assign_firmware_buf(fw, device);
1052
1053 usermodehelper_read_unlock();
1054
1055 out:
1056 if (ret < 0) {
1057 release_firmware(fw);
1058 fw = NULL;
1059 }
1060
1061 *firmware_p = fw;
1062 return ret;
1063}
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065/**
Kay Sievers312c0042005-11-16 09:00:00 +01001066 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001067 * @firmware_p: pointer to firmware image
1068 * @name: name of firmware file
1069 * @device: device for which firmware is being loaded
1070 *
1071 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001072 * of @name for device @device.
1073 *
1074 * Should be called from user context where sleeping is allowed.
1075 *
Kay Sievers312c0042005-11-16 09:00:00 +01001076 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001077 * should be distinctive enough not to be confused with any other
1078 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +08001079 *
1080 * Caller must hold the reference count of @device.
Ming Lei6a927852012-11-03 17:48:16 +08001081 *
1082 * The function can be called safely inside device's suspend and
1083 * resume callback.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001084 **/
1085int
1086request_firmware(const struct firmware **firmware_p, const char *name,
1087 struct device *device)
1088{
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001089 return _request_firmware(firmware_p, name, device, true, false);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001090}
1091
1092/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001094 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001096void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
1098 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001099 if (!fw_is_builtin_firmware(fw))
1100 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 kfree(fw);
1102 }
1103}
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105/* Async support */
1106struct firmware_work {
1107 struct work_struct work;
1108 struct module *module;
1109 const char *name;
1110 struct device *device;
1111 void *context;
1112 void (*cont)(const struct firmware *fw, void *context);
Bob Liu072fc8f2011-01-26 18:33:32 +08001113 bool uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114};
1115
Stephen Boyda36cf842012-03-28 23:31:00 +02001116static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
Stephen Boyda36cf842012-03-28 23:31:00 +02001118 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 const struct firmware *fw;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001120
Stephen Boyda36cf842012-03-28 23:31:00 +02001121 fw_work = container_of(work, struct firmware_work, work);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001122
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001123 _request_firmware(&fw, fw_work->name, fw_work->device,
1124 fw_work->uevent, true);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001125 fw_work->cont(fw, fw_work->context);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001126 put_device(fw_work->device); /* taken in request_firmware_nowait() */
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 module_put(fw_work->module);
1129 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131
1132/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001133 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001134 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001135 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001136 * is non-zero else the firmware copy must be done manually.
1137 * @name: name of firmware file
1138 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001139 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001140 * @context: will be passed over to @cont, and
1141 * @fw may be %NULL if firmware request fails.
1142 * @cont: function will be called asynchronously when the firmware
1143 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001145 * Caller must hold the reference count of @device.
1146 *
Ming Lei6f21a622012-08-04 12:01:24 +08001147 * Asynchronous variant of request_firmware() for user contexts:
1148 * - sleep for as small periods as possible since it may
1149 * increase kernel boot time of built-in device drivers
1150 * requesting firmware in their ->probe() methods, if
1151 * @gfp is GFP_KERNEL.
1152 *
1153 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 **/
1155int
1156request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001157 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001158 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 void (*cont)(const struct firmware *fw, void *context))
1160{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001161 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001163 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (!fw_work)
1165 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001166
1167 fw_work->module = module;
1168 fw_work->name = name;
1169 fw_work->device = device;
1170 fw_work->context = context;
1171 fw_work->cont = cont;
1172 fw_work->uevent = uevent;
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (!try_module_get(module)) {
1175 kfree(fw_work);
1176 return -EFAULT;
1177 }
1178
Ming Lei0cfc1e12012-08-04 12:01:23 +08001179 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001180 INIT_WORK(&fw_work->work, request_firmware_work_func);
1181 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return 0;
1183}
1184
Ming Lei2887b392012-08-04 12:01:22 +08001185/**
1186 * cache_firmware - cache one firmware image in kernel memory space
1187 * @fw_name: the firmware image name
1188 *
1189 * Cache firmware in kernel memory so that drivers can use it when
1190 * system isn't ready for them to request firmware image from userspace.
1191 * Once it returns successfully, driver can use request_firmware or its
1192 * nowait version to get the cached firmware without any interacting
1193 * with userspace
1194 *
1195 * Return 0 if the firmware image has been cached successfully
1196 * Return !0 otherwise
1197 *
1198 */
1199int cache_firmware(const char *fw_name)
1200{
1201 int ret;
1202 const struct firmware *fw;
1203
1204 pr_debug("%s: %s\n", __func__, fw_name);
1205
1206 ret = request_firmware(&fw, fw_name, NULL);
1207 if (!ret)
1208 kfree(fw);
1209
1210 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1211
1212 return ret;
1213}
1214
1215/**
1216 * uncache_firmware - remove one cached firmware image
1217 * @fw_name: the firmware image name
1218 *
1219 * Uncache one firmware image which has been cached successfully
1220 * before.
1221 *
1222 * Return 0 if the firmware cache has been removed successfully
1223 * Return !0 otherwise
1224 *
1225 */
1226int uncache_firmware(const char *fw_name)
1227{
1228 struct firmware_buf *buf;
1229 struct firmware fw;
1230
1231 pr_debug("%s: %s\n", __func__, fw_name);
1232
1233 if (fw_get_builtin_firmware(&fw, fw_name))
1234 return 0;
1235
1236 buf = fw_lookup_buf(fw_name);
1237 if (buf) {
1238 fw_free_buf(buf);
1239 return 0;
1240 }
1241
1242 return -EINVAL;
1243}
1244
Ming Leicfe016b2012-09-08 17:32:30 +08001245#ifdef CONFIG_PM_SLEEP
Ming Leid28d3882012-10-09 12:01:04 +08001246static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1247
Ming Lei37276a52012-08-04 12:01:27 +08001248static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1249{
1250 struct fw_cache_entry *fce;
1251
1252 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1253 if (!fce)
1254 goto exit;
1255
1256 strcpy(fce->name, name);
1257exit:
1258 return fce;
1259}
1260
Ming Lei373304f2012-10-09 12:01:01 +08001261static int __fw_entry_found(const char *name)
1262{
1263 struct firmware_cache *fwc = &fw_cache;
1264 struct fw_cache_entry *fce;
1265
1266 list_for_each_entry(fce, &fwc->fw_names, list) {
1267 if (!strcmp(fce->name, name))
1268 return 1;
1269 }
1270 return 0;
1271}
1272
Ming Leiac39b3e2012-08-20 19:04:16 +08001273static int fw_cache_piggyback_on_request(const char *name)
1274{
1275 struct firmware_cache *fwc = &fw_cache;
1276 struct fw_cache_entry *fce;
1277 int ret = 0;
1278
1279 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001280 if (__fw_entry_found(name))
1281 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001282
1283 fce = alloc_fw_cache_entry(name);
1284 if (fce) {
1285 ret = 1;
1286 list_add(&fce->list, &fwc->fw_names);
1287 pr_debug("%s: fw: %s\n", __func__, name);
1288 }
1289found:
1290 spin_unlock(&fwc->name_lock);
1291 return ret;
1292}
1293
Ming Lei37276a52012-08-04 12:01:27 +08001294static void free_fw_cache_entry(struct fw_cache_entry *fce)
1295{
1296 kfree(fce);
1297}
1298
1299static void __async_dev_cache_fw_image(void *fw_entry,
1300 async_cookie_t cookie)
1301{
1302 struct fw_cache_entry *fce = fw_entry;
1303 struct firmware_cache *fwc = &fw_cache;
1304 int ret;
1305
1306 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001307 if (ret) {
1308 spin_lock(&fwc->name_lock);
1309 list_del(&fce->list);
1310 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001311
Ming Leiac39b3e2012-08-20 19:04:16 +08001312 free_fw_cache_entry(fce);
1313 }
Ming Lei37276a52012-08-04 12:01:27 +08001314}
1315
1316/* called with dev->devres_lock held */
1317static void dev_create_fw_entry(struct device *dev, void *res,
1318 void *data)
1319{
1320 struct fw_name_devm *fwn = res;
1321 const char *fw_name = fwn->name;
1322 struct list_head *head = data;
1323 struct fw_cache_entry *fce;
1324
1325 fce = alloc_fw_cache_entry(fw_name);
1326 if (fce)
1327 list_add(&fce->list, head);
1328}
1329
1330static int devm_name_match(struct device *dev, void *res,
1331 void *match_data)
1332{
1333 struct fw_name_devm *fwn = res;
1334 return (fwn->magic == (unsigned long)match_data);
1335}
1336
Ming Leiab6dd8e2012-08-17 22:07:00 +08001337static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001338{
1339 LIST_HEAD(todo);
1340 struct fw_cache_entry *fce;
1341 struct fw_cache_entry *fce_next;
1342 struct firmware_cache *fwc = &fw_cache;
1343
1344 devres_for_each_res(dev, fw_name_devm_release,
1345 devm_name_match, &fw_cache,
1346 dev_create_fw_entry, &todo);
1347
1348 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1349 list_del(&fce->list);
1350
1351 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001352 /* only one cache entry for one firmware */
1353 if (!__fw_entry_found(fce->name)) {
Ming Lei373304f2012-10-09 12:01:01 +08001354 list_add(&fce->list, &fwc->fw_names);
1355 } else {
1356 free_fw_cache_entry(fce);
1357 fce = NULL;
1358 }
Ming Lei37276a52012-08-04 12:01:27 +08001359 spin_unlock(&fwc->name_lock);
1360
Ming Lei373304f2012-10-09 12:01:01 +08001361 if (fce)
Ming Leid28d3882012-10-09 12:01:04 +08001362 async_schedule_domain(__async_dev_cache_fw_image,
1363 (void *)fce,
1364 &fw_cache_domain);
Ming Lei37276a52012-08-04 12:01:27 +08001365 }
1366}
1367
1368static void __device_uncache_fw_images(void)
1369{
1370 struct firmware_cache *fwc = &fw_cache;
1371 struct fw_cache_entry *fce;
1372
1373 spin_lock(&fwc->name_lock);
1374 while (!list_empty(&fwc->fw_names)) {
1375 fce = list_entry(fwc->fw_names.next,
1376 struct fw_cache_entry, list);
1377 list_del(&fce->list);
1378 spin_unlock(&fwc->name_lock);
1379
1380 uncache_firmware(fce->name);
1381 free_fw_cache_entry(fce);
1382
1383 spin_lock(&fwc->name_lock);
1384 }
1385 spin_unlock(&fwc->name_lock);
1386}
1387
1388/**
1389 * device_cache_fw_images - cache devices' firmware
1390 *
1391 * If one device called request_firmware or its nowait version
1392 * successfully before, the firmware names are recored into the
1393 * device's devres link list, so device_cache_fw_images can call
1394 * cache_firmware() to cache these firmwares for the device,
1395 * then the device driver can load its firmwares easily at
1396 * time when system is not ready to complete loading firmware.
1397 */
1398static void device_cache_fw_images(void)
1399{
1400 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001401 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001402 DEFINE_WAIT(wait);
1403
1404 pr_debug("%s\n", __func__);
1405
Ming Lei373304f2012-10-09 12:01:01 +08001406 /* cancel uncache work */
1407 cancel_delayed_work_sync(&fwc->work);
1408
Ming Leiffe53f62012-08-04 12:01:28 +08001409 /*
1410 * use small loading timeout for caching devices' firmware
1411 * because all these firmware images have been loaded
1412 * successfully at lease once, also system is ready for
1413 * completing firmware loading now. The maximum size of
1414 * firmware in current distributions is about 2M bytes,
1415 * so 10 secs should be enough.
1416 */
1417 old_timeout = loading_timeout;
1418 loading_timeout = 10;
1419
Ming Leiac39b3e2012-08-20 19:04:16 +08001420 mutex_lock(&fw_lock);
1421 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001422 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001423 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001424
1425 /* wait for completion of caching firmware for all devices */
Ming Leid28d3882012-10-09 12:01:04 +08001426 async_synchronize_full_domain(&fw_cache_domain);
Ming Leiffe53f62012-08-04 12:01:28 +08001427
1428 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001429}
1430
1431/**
1432 * device_uncache_fw_images - uncache devices' firmware
1433 *
1434 * uncache all firmwares which have been cached successfully
1435 * by device_uncache_fw_images earlier
1436 */
1437static void device_uncache_fw_images(void)
1438{
1439 pr_debug("%s\n", __func__);
1440 __device_uncache_fw_images();
1441}
1442
1443static void device_uncache_fw_images_work(struct work_struct *work)
1444{
1445 device_uncache_fw_images();
1446}
1447
1448/**
1449 * device_uncache_fw_images_delay - uncache devices firmwares
1450 * @delay: number of milliseconds to delay uncache device firmwares
1451 *
1452 * uncache all devices's firmwares which has been cached successfully
1453 * by device_cache_fw_images after @delay milliseconds.
1454 */
1455static void device_uncache_fw_images_delay(unsigned long delay)
1456{
1457 schedule_delayed_work(&fw_cache.work,
1458 msecs_to_jiffies(delay));
1459}
1460
Ming Lei07646d92012-08-04 12:01:29 +08001461static int fw_pm_notify(struct notifier_block *notify_block,
1462 unsigned long mode, void *unused)
1463{
1464 switch (mode) {
1465 case PM_HIBERNATION_PREPARE:
1466 case PM_SUSPEND_PREPARE:
1467 device_cache_fw_images();
1468 break;
1469
1470 case PM_POST_SUSPEND:
1471 case PM_POST_HIBERNATION:
1472 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001473 /*
1474 * In case that system sleep failed and syscore_suspend is
1475 * not called.
1476 */
1477 mutex_lock(&fw_lock);
1478 fw_cache.state = FW_LOADER_NO_CACHE;
1479 mutex_unlock(&fw_lock);
1480
Ming Lei07646d92012-08-04 12:01:29 +08001481 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1482 break;
1483 }
1484
1485 return 0;
1486}
Ming Lei07646d92012-08-04 12:01:29 +08001487
Ming Leiac39b3e2012-08-20 19:04:16 +08001488/* stop caching firmware once syscore_suspend is reached */
1489static int fw_suspend(void)
1490{
1491 fw_cache.state = FW_LOADER_NO_CACHE;
1492 return 0;
1493}
1494
1495static struct syscore_ops fw_syscore_ops = {
1496 .suspend = fw_suspend,
1497};
Ming Leicfe016b2012-09-08 17:32:30 +08001498#else
1499static int fw_cache_piggyback_on_request(const char *name)
1500{
1501 return 0;
1502}
1503#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001504
Ming Lei37276a52012-08-04 12:01:27 +08001505static void __init fw_cache_init(void)
1506{
1507 spin_lock_init(&fw_cache.lock);
1508 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001509 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001510
Ming Leicfe016b2012-09-08 17:32:30 +08001511#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +08001512 spin_lock_init(&fw_cache.name_lock);
1513 INIT_LIST_HEAD(&fw_cache.fw_names);
Ming Lei37276a52012-08-04 12:01:27 +08001514
Ming Lei37276a52012-08-04 12:01:27 +08001515 INIT_DELAYED_WORK(&fw_cache.work,
1516 device_uncache_fw_images_work);
Ming Lei07646d92012-08-04 12:01:29 +08001517
1518 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1519 register_pm_notifier(&fw_cache.pm_notify);
Ming Leiac39b3e2012-08-20 19:04:16 +08001520
1521 register_syscore_ops(&fw_syscore_ops);
Ming Leicfe016b2012-09-08 17:32:30 +08001522#endif
Ming Lei37276a52012-08-04 12:01:27 +08001523}
1524
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001525static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
Ming Lei1f2b7952012-08-04 12:01:21 +08001527 fw_cache_init();
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001528#ifdef CONFIG_FW_LOADER_USER_HELPER
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001529 return class_register(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001530#else
1531 return 0;
1532#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001534
1535static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
Ming Leicfe016b2012-09-08 17:32:30 +08001537#ifdef CONFIG_PM_SLEEP
Ming Leiac39b3e2012-08-20 19:04:16 +08001538 unregister_syscore_ops(&fw_syscore_ops);
Ming Lei07646d92012-08-04 12:01:29 +08001539 unregister_pm_notifier(&fw_cache.pm_notify);
Ming Leicfe016b2012-09-08 17:32:30 +08001540#endif
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001541#ifdef CONFIG_FW_LOADER_USER_HELPER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 class_unregister(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001543#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}
1545
Shaohua Lia30a6a22006-09-27 01:50:52 -07001546fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547module_exit(firmware_class_exit);
1548
1549EXPORT_SYMBOL(release_firmware);
1550EXPORT_SYMBOL(request_firmware);
1551EXPORT_SYMBOL(request_firmware_nowait);
Ming Lei2887b392012-08-04 12:01:22 +08001552EXPORT_SYMBOL_GPL(cache_firmware);
1553EXPORT_SYMBOL_GPL(uncache_firmware);