blob: 28bc6db9fbf4f5044308a662c30f2ae677ffd7a5 [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>
Takashi Iwaife304142013-05-22 18:28:38 +020030#include <linux/reboot.h>
Ming Lei37276a52012-08-04 12:01:27 +080031
Linus Torvaldsabb139e2012-10-03 15:58:32 -070032#include <generated/utsrelease.h>
33
Ming Lei37276a52012-08-04 12:01:27 +080034#include "base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Markus Rechberger87d37a42007-06-04 18:45:44 +020036MODULE_AUTHOR("Manuel Estrada Sainz");
Linus Torvalds1da177e2005-04-16 15:20:36 -070037MODULE_DESCRIPTION("Multi purpose firmware loading support");
38MODULE_LICENSE("GPL");
39
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080040/* Builtin firmware support */
41
42#ifdef CONFIG_FW_LOADER
43
44extern struct builtin_fw __start_builtin_fw[];
45extern struct builtin_fw __end_builtin_fw[];
46
47static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
48{
49 struct builtin_fw *b_fw;
50
51 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
52 if (strcmp(name, b_fw->name) == 0) {
53 fw->size = b_fw->size;
54 fw->data = b_fw->data;
55 return true;
56 }
57 }
58
59 return false;
60}
61
62static bool fw_is_builtin_firmware(const struct firmware *fw)
63{
64 struct builtin_fw *b_fw;
65
66 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
67 if (fw->data == b_fw->data)
68 return true;
69
70 return false;
71}
72
73#else /* Module case - no builtin firmware support */
74
75static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
76{
77 return false;
78}
79
80static inline bool fw_is_builtin_firmware(const struct firmware *fw)
81{
82 return false;
83}
84#endif
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086enum {
87 FW_STATUS_LOADING,
88 FW_STATUS_DONE,
89 FW_STATUS_ABORT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090};
91
Dave Jones2f651682007-01-25 15:56:15 -050092static int loading_timeout = 60; /* In seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +020094static inline long firmware_loading_timeout(void)
95{
96 return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
97}
98
Takashi Iwai14c4bae2013-12-02 15:38:18 +010099/* firmware behavior options */
100#define FW_OPT_UEVENT (1U << 0)
101#define FW_OPT_NOWAIT (1U << 1)
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100102#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwai5a1379e2014-06-04 17:48:15 +0200103#define FW_OPT_USERHELPER (1U << 2)
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100104#else
Takashi Iwai5a1379e2014-06-04 17:48:15 +0200105#define FW_OPT_USERHELPER 0
106#endif
107#ifdef CONFIG_FW_LOADER_USER_HELPER_FALLBACK
108#define FW_OPT_FALLBACK FW_OPT_USERHELPER
109#else
110#define FW_OPT_FALLBACK 0
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100111#endif
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100112
Ming Lei1f2b7952012-08-04 12:01:21 +0800113struct firmware_cache {
114 /* firmware_buf instance will be added into the below list */
115 spinlock_t lock;
116 struct list_head head;
Ming Leicfe016b2012-09-08 17:32:30 +0800117 int state;
Ming Lei37276a52012-08-04 12:01:27 +0800118
Ming Leicfe016b2012-09-08 17:32:30 +0800119#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +0800120 /*
121 * Names of firmware images which have been cached successfully
122 * will be added into the below list so that device uncache
123 * helper can trace which firmware images have been cached
124 * before.
125 */
126 spinlock_t name_lock;
127 struct list_head fw_names;
128
Ming Lei37276a52012-08-04 12:01:27 +0800129 struct delayed_work work;
Ming Lei07646d92012-08-04 12:01:29 +0800130
131 struct notifier_block pm_notify;
Ming Leicfe016b2012-09-08 17:32:30 +0800132#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800133};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Ming Lei12446912012-08-04 12:01:20 +0800135struct firmware_buf {
Ming Lei1f2b7952012-08-04 12:01:21 +0800136 struct kref ref;
137 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 struct completion completion;
Ming Lei1f2b7952012-08-04 12:01:21 +0800139 struct firmware_cache *fwc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 unsigned long status;
Ming Lei65710cb2012-08-04 12:01:16 +0800141 void *data;
142 size_t size;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100143#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100144 bool is_paged_buf;
Ming Leiaf5bc112013-05-27 18:30:04 +0800145 bool need_uevent;
David Woodhouse6e03a202009-04-09 22:04:07 -0700146 struct page **pages;
147 int nr_pages;
148 int page_array_size;
Takashi Iwaife304142013-05-22 18:28:38 +0200149 struct list_head pending_list;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100150#endif
Dmitry Torokhove1771232010-03-13 23:49:23 -0800151 char fw_id[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152};
153
Ming Lei37276a52012-08-04 12:01:27 +0800154struct fw_cache_entry {
155 struct list_head list;
156 char name[];
157};
158
Ming Leif531f05a2012-08-04 12:01:25 +0800159struct fw_name_devm {
160 unsigned long magic;
161 char name[];
162};
163
Ming Lei1f2b7952012-08-04 12:01:21 +0800164#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
165
Ming Leiac39b3e2012-08-20 19:04:16 +0800166#define FW_LOADER_NO_CACHE 0
167#define FW_LOADER_START_CACHE 1
168
169static int fw_cache_piggyback_on_request(const char *name);
170
Ming Lei1f2b7952012-08-04 12:01:21 +0800171/* fw_lock could be moved to 'struct firmware_priv' but since it is just
172 * guarding for corner cases a global lock should be OK */
173static DEFINE_MUTEX(fw_lock);
174
175static struct firmware_cache fw_cache;
176
177static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
178 struct firmware_cache *fwc)
179{
180 struct firmware_buf *buf;
181
182 buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
183
184 if (!buf)
185 return buf;
186
187 kref_init(&buf->ref);
188 strcpy(buf->fw_id, fw_name);
189 buf->fwc = fwc;
190 init_completion(&buf->completion);
Takashi Iwaife304142013-05-22 18:28:38 +0200191#ifdef CONFIG_FW_LOADER_USER_HELPER
192 INIT_LIST_HEAD(&buf->pending_list);
193#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800194
195 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
196
197 return buf;
198}
199
Ming Lei2887b392012-08-04 12:01:22 +0800200static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
201{
202 struct firmware_buf *tmp;
203 struct firmware_cache *fwc = &fw_cache;
204
205 list_for_each_entry(tmp, &fwc->head, list)
206 if (!strcmp(tmp->fw_id, fw_name))
207 return tmp;
208 return NULL;
209}
210
Ming Lei1f2b7952012-08-04 12:01:21 +0800211static int fw_lookup_and_allocate_buf(const char *fw_name,
212 struct firmware_cache *fwc,
213 struct firmware_buf **buf)
214{
215 struct firmware_buf *tmp;
216
217 spin_lock(&fwc->lock);
Ming Lei2887b392012-08-04 12:01:22 +0800218 tmp = __fw_lookup_buf(fw_name);
219 if (tmp) {
220 kref_get(&tmp->ref);
221 spin_unlock(&fwc->lock);
222 *buf = tmp;
223 return 1;
224 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800225 tmp = __allocate_fw_buf(fw_name, fwc);
226 if (tmp)
227 list_add(&tmp->list, &fwc->head);
228 spin_unlock(&fwc->lock);
229
230 *buf = tmp;
231
232 return tmp ? 0 : -ENOMEM;
233}
234
235static void __fw_free_buf(struct kref *ref)
Bart Van Assche98233b22014-01-04 14:20:36 +0100236 __releases(&fwc->lock)
Ming Lei1f2b7952012-08-04 12:01:21 +0800237{
238 struct firmware_buf *buf = to_fwbuf(ref);
239 struct firmware_cache *fwc = buf->fwc;
Ming Lei1f2b7952012-08-04 12:01:21 +0800240
241 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
242 __func__, buf->fw_id, buf, buf->data,
243 (unsigned int)buf->size);
244
Ming Lei1f2b7952012-08-04 12:01:21 +0800245 list_del(&buf->list);
246 spin_unlock(&fwc->lock);
247
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100248#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100249 if (buf->is_paged_buf) {
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100250 int i;
Ming Lei746058f2012-10-09 12:01:03 +0800251 vunmap(buf->data);
252 for (i = 0; i < buf->nr_pages; i++)
253 __free_page(buf->pages[i]);
254 kfree(buf->pages);
255 } else
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100256#endif
Ming Lei746058f2012-10-09 12:01:03 +0800257 vfree(buf->data);
Ming Lei1f2b7952012-08-04 12:01:21 +0800258 kfree(buf);
259}
260
261static void fw_free_buf(struct firmware_buf *buf)
262{
Chuansheng Liubd9eb7f2012-11-10 01:27:22 +0800263 struct firmware_cache *fwc = buf->fwc;
264 spin_lock(&fwc->lock);
265 if (!kref_put(&buf->ref, __fw_free_buf))
266 spin_unlock(&fwc->lock);
Ming Lei1f2b7952012-08-04 12:01:21 +0800267}
268
Ming Lei746058f2012-10-09 12:01:03 +0800269/* direct firmware loading support */
Ming Lei27602842012-11-03 17:47:58 +0800270static char fw_path_para[256];
271static const char * const fw_path[] = {
272 fw_path_para,
Ming Lei746058f2012-10-09 12:01:03 +0800273 "/lib/firmware/updates/" UTS_RELEASE,
274 "/lib/firmware/updates",
275 "/lib/firmware/" UTS_RELEASE,
276 "/lib/firmware"
277};
278
Ming Lei27602842012-11-03 17:47:58 +0800279/*
280 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
281 * from kernel command line because firmware_class is generally built in
282 * kernel instead of module.
283 */
284module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
285MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
286
Neil Horman3e358ac2013-09-06 15:36:08 -0400287static int fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf)
Ming Lei746058f2012-10-09 12:01:03 +0800288{
Ben Hutchings08da2012013-12-28 16:53:59 +0100289 int size;
Ming Lei746058f2012-10-09 12:01:03 +0800290 char *buf;
Neil Horman3e358ac2013-09-06 15:36:08 -0400291 int rc;
Ming Lei746058f2012-10-09 12:01:03 +0800292
Dmitry Kasatkin6af6b162014-06-13 18:09:54 +0300293 if (!S_ISREG(file_inode(file)->i_mode))
294 return -EINVAL;
295 size = i_size_read(file_inode(file));
Luciano Coelho4adf07f2013-01-15 10:43:43 +0200296 if (size <= 0)
Neil Horman3e358ac2013-09-06 15:36:08 -0400297 return -EINVAL;
Ming Lei746058f2012-10-09 12:01:03 +0800298 buf = vmalloc(size);
299 if (!buf)
Neil Horman3e358ac2013-09-06 15:36:08 -0400300 return -ENOMEM;
301 rc = kernel_read(file, 0, buf, size);
302 if (rc != size) {
303 if (rc > 0)
304 rc = -EIO;
Ming Lei746058f2012-10-09 12:01:03 +0800305 vfree(buf);
Neil Horman3e358ac2013-09-06 15:36:08 -0400306 return rc;
Ming Lei746058f2012-10-09 12:01:03 +0800307 }
308 fw_buf->data = buf;
309 fw_buf->size = size;
Neil Horman3e358ac2013-09-06 15:36:08 -0400310 return 0;
Ming Lei746058f2012-10-09 12:01:03 +0800311}
312
Neil Horman3e358ac2013-09-06 15:36:08 -0400313static int fw_get_filesystem_firmware(struct device *device,
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100314 struct firmware_buf *buf)
Ming Lei746058f2012-10-09 12:01:03 +0800315{
316 int i;
Neil Horman3e358ac2013-09-06 15:36:08 -0400317 int rc = -ENOENT;
Ming Lei746058f2012-10-09 12:01:03 +0800318 char *path = __getname();
319
320 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
321 struct file *file;
Ming Lei27602842012-11-03 17:47:58 +0800322
323 /* skip the unset customized path */
324 if (!fw_path[i][0])
325 continue;
326
Ming Lei746058f2012-10-09 12:01:03 +0800327 snprintf(path, PATH_MAX, "%s/%s", fw_path[i], buf->fw_id);
328
329 file = filp_open(path, O_RDONLY, 0);
330 if (IS_ERR(file))
331 continue;
Neil Horman3e358ac2013-09-06 15:36:08 -0400332 rc = fw_read_file_contents(file, buf);
Ming Lei746058f2012-10-09 12:01:03 +0800333 fput(file);
Neil Horman3e358ac2013-09-06 15:36:08 -0400334 if (rc)
335 dev_warn(device, "firmware, attempted to load %s, but failed with error %d\n",
336 path, rc);
337 else
Ming Lei746058f2012-10-09 12:01:03 +0800338 break;
339 }
340 __putname(path);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100341
Neil Horman3e358ac2013-09-06 15:36:08 -0400342 if (!rc) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100343 dev_dbg(device, "firmware: direct-loading firmware %s\n",
344 buf->fw_id);
345 mutex_lock(&fw_lock);
346 set_bit(FW_STATUS_DONE, &buf->status);
347 complete_all(&buf->completion);
348 mutex_unlock(&fw_lock);
349 }
350
Neil Horman3e358ac2013-09-06 15:36:08 -0400351 return rc;
Ming Lei746058f2012-10-09 12:01:03 +0800352}
353
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100354/* firmware holds the ownership of pages */
355static void firmware_free_data(const struct firmware *fw)
356{
357 /* Loaded directly? */
358 if (!fw->priv) {
359 vfree(fw->data);
360 return;
361 }
362 fw_free_buf(fw->priv);
363}
364
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100365/* store the pages buffer info firmware from buf */
366static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
367{
368 fw->priv = buf;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100369#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100370 fw->pages = buf->pages;
371#endif
372 fw->size = buf->size;
373 fw->data = buf->data;
374
375 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
376 __func__, buf->fw_id, buf, buf->data,
377 (unsigned int)buf->size);
378}
379
380#ifdef CONFIG_PM_SLEEP
381static void fw_name_devm_release(struct device *dev, void *res)
382{
383 struct fw_name_devm *fwn = res;
384
385 if (fwn->magic == (unsigned long)&fw_cache)
386 pr_debug("%s: fw_name-%s devm-%p released\n",
387 __func__, fwn->name, res);
388}
389
390static int fw_devm_match(struct device *dev, void *res,
391 void *match_data)
392{
393 struct fw_name_devm *fwn = res;
394
395 return (fwn->magic == (unsigned long)&fw_cache) &&
396 !strcmp(fwn->name, match_data);
397}
398
399static struct fw_name_devm *fw_find_devm_name(struct device *dev,
400 const char *name)
401{
402 struct fw_name_devm *fwn;
403
404 fwn = devres_find(dev, fw_name_devm_release,
405 fw_devm_match, (void *)name);
406 return fwn;
407}
408
409/* add firmware name into devres list */
410static int fw_add_devm_name(struct device *dev, const char *name)
411{
412 struct fw_name_devm *fwn;
413
414 fwn = fw_find_devm_name(dev, name);
415 if (fwn)
416 return 1;
417
418 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
419 strlen(name) + 1, GFP_KERNEL);
420 if (!fwn)
421 return -ENOMEM;
422
423 fwn->magic = (unsigned long)&fw_cache;
424 strcpy(fwn->name, name);
425 devres_add(dev, fwn);
426
427 return 0;
428}
429#else
430static int fw_add_devm_name(struct device *dev, const char *name)
431{
432 return 0;
433}
434#endif
435
436
437/*
438 * user-mode helper code
439 */
440#ifdef CONFIG_FW_LOADER_USER_HELPER
441struct firmware_priv {
442 struct delayed_work timeout_work;
443 bool nowait;
444 struct device dev;
445 struct firmware_buf *buf;
446 struct firmware *fw;
447};
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100448
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700449static struct firmware_priv *to_firmware_priv(struct device *dev)
450{
451 return container_of(dev, struct firmware_priv, dev);
452}
453
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700454static void __fw_load_abort(struct firmware_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Ming Lei87597932013-06-15 16:36:38 +0800456 /*
457 * There is a small window in which user can write to 'loading'
458 * between loading done and disappearance of 'loading'
459 */
460 if (test_bit(FW_STATUS_DONE, &buf->status))
461 return;
462
Takashi Iwaife304142013-05-22 18:28:38 +0200463 list_del_init(&buf->pending_list);
Ming Lei12446912012-08-04 12:01:20 +0800464 set_bit(FW_STATUS_ABORT, &buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800465 complete_all(&buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700468static void fw_load_abort(struct firmware_priv *fw_priv)
469{
470 struct firmware_buf *buf = fw_priv->buf;
471
472 __fw_load_abort(buf);
Ming Lei87597932013-06-15 16:36:38 +0800473
474 /* avoid user action after loading abort */
475 fw_priv->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Takashi Iwai807be032013-01-31 11:13:57 +0100478#define is_fw_load_aborted(buf) \
479 test_bit(FW_STATUS_ABORT, &(buf)->status)
480
Takashi Iwaife304142013-05-22 18:28:38 +0200481static LIST_HEAD(pending_fw_head);
482
483/* reboot notifier for avoid deadlock with usermode_lock */
484static int fw_shutdown_notify(struct notifier_block *unused1,
485 unsigned long unused2, void *unused3)
486{
487 mutex_lock(&fw_lock);
488 while (!list_empty(&pending_fw_head))
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700489 __fw_load_abort(list_first_entry(&pending_fw_head,
Takashi Iwaife304142013-05-22 18:28:38 +0200490 struct firmware_buf,
491 pending_list));
492 mutex_unlock(&fw_lock);
493 return NOTIFY_DONE;
494}
495
496static struct notifier_block fw_shutdown_nb = {
497 .notifier_call = fw_shutdown_notify,
498};
499
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700500static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
501 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 return sprintf(buf, "%d\n", loading_timeout);
504}
505
506/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800507 * firmware_timeout_store - set number of seconds to wait for firmware
508 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800509 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800510 * @buf: buffer to scan for timeout value
511 * @count: number of bytes in @buf
512 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800514 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 * firmware will be provided.
516 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800517 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 **/
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700519static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
520 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700523 if (loading_timeout < 0)
524 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return count;
527}
528
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800529static struct class_attribute firmware_class_attrs[] = {
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700530 __ATTR_RW(timeout),
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800531 __ATTR_NULL
532};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800534static void fw_dev_release(struct device *dev)
535{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700536 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800537
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800538 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800539}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Kay Sievers7eff2e72007-08-14 15:15:12 +0200541static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700543 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Ming Lei12446912012-08-04 12:01:20 +0800545 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200547 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700548 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200549 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
550 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 return 0;
553}
554
Adrian Bunk1b81d662006-05-20 15:00:16 -0700555static struct class firmware_class = {
556 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800557 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700558 .dev_uevent = firmware_uevent,
559 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700560};
561
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700562static ssize_t firmware_loading_show(struct device *dev,
563 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700565 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800566 int loading = 0;
567
568 mutex_lock(&fw_lock);
569 if (fw_priv->buf)
570 loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
571 mutex_unlock(&fw_lock);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return sprintf(buf, "%d\n", loading);
574}
575
David Woodhouse6e03a202009-04-09 22:04:07 -0700576/* Some architectures don't have PAGE_KERNEL_RO */
577#ifndef PAGE_KERNEL_RO
578#define PAGE_KERNEL_RO PAGE_KERNEL
579#endif
Ming Lei253c9242012-10-09 12:01:02 +0800580
581/* one pages buffer should be mapped/unmapped only once */
582static int fw_map_pages_buf(struct firmware_buf *buf)
583{
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100584 if (!buf->is_paged_buf)
Ming Lei746058f2012-10-09 12:01:03 +0800585 return 0;
586
Ming Lei253c9242012-10-09 12:01:02 +0800587 if (buf->data)
588 vunmap(buf->data);
589 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
590 if (!buf->data)
591 return -ENOMEM;
592 return 0;
593}
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800596 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700597 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800598 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800599 * @buf: buffer to scan for loading control value
600 * @count: number of bytes in @buf
601 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 * The relevant values are:
603 *
604 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800605 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 * -1: Conclude the load with an error and discard any written data.
607 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700608static ssize_t firmware_loading_store(struct device *dev,
609 struct device_attribute *attr,
610 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700612 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800613 struct firmware_buf *fw_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700615 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Neil Hormaneea915b2012-01-02 15:31:23 -0500617 mutex_lock(&fw_lock);
Ming Lei87597932013-06-15 16:36:38 +0800618 fw_buf = fw_priv->buf;
Ming Lei12446912012-08-04 12:01:20 +0800619 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500620 goto out;
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 switch (loading) {
623 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800624 /* discarding any previous partial load */
Ming Lei12446912012-08-04 12:01:20 +0800625 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
626 for (i = 0; i < fw_buf->nr_pages; i++)
627 __free_page(fw_buf->pages[i]);
628 kfree(fw_buf->pages);
629 fw_buf->pages = NULL;
630 fw_buf->page_array_size = 0;
631 fw_buf->nr_pages = 0;
632 set_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei28eefa72012-08-04 12:01:17 +0800633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 break;
635 case 0:
Ming Lei12446912012-08-04 12:01:20 +0800636 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
637 set_bit(FW_STATUS_DONE, &fw_buf->status);
638 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei253c9242012-10-09 12:01:02 +0800639
640 /*
641 * Several loading requests may be pending on
642 * one same firmware buf, so let all requests
643 * see the mapped 'buf->data' once the loading
644 * is completed.
645 * */
zhang jun2b1278c2014-02-13 15:18:47 +0800646 if (fw_map_pages_buf(fw_buf))
647 dev_err(dev, "%s: map pages failed\n",
648 __func__);
Takashi Iwaife304142013-05-22 18:28:38 +0200649 list_del_init(&fw_buf->pending_list);
Ming Lei1f2b7952012-08-04 12:01:21 +0800650 complete_all(&fw_buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 break;
652 }
653 /* fallthrough */
654 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700655 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /* fallthrough */
657 case -1:
658 fw_load_abort(fw_priv);
659 break;
660 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500661out:
662 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return count;
664}
665
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700666static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700668static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
669 struct bin_attribute *bin_attr,
670 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200672 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700673 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800674 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700675 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Laura Garciacad1e552006-05-23 23:22:38 +0200677 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800678 buf = fw_priv->buf;
679 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 ret_count = -ENODEV;
681 goto out;
682 }
Ming Lei12446912012-08-04 12:01:20 +0800683 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200684 ret_count = 0;
685 goto out;
686 }
Ming Lei12446912012-08-04 12:01:20 +0800687 if (count > buf->size - offset)
688 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700689
690 ret_count = count;
691
692 while (count) {
693 void *page_data;
694 int page_nr = offset >> PAGE_SHIFT;
695 int page_ofs = offset & (PAGE_SIZE-1);
696 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
697
Ming Lei12446912012-08-04 12:01:20 +0800698 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700699
700 memcpy(buffer, page_data + page_ofs, page_cnt);
701
Ming Lei12446912012-08-04 12:01:20 +0800702 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700703 buffer += page_cnt;
704 offset += page_cnt;
705 count -= page_cnt;
706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707out:
Laura Garciacad1e552006-05-23 23:22:38 +0200708 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return ret_count;
710}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800711
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700712static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Ming Lei12446912012-08-04 12:01:20 +0800714 struct firmware_buf *buf = fw_priv->buf;
Fabian Fredericka76040d2014-07-01 21:13:30 +0200715 int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
David Woodhouse6e03a202009-04-09 22:04:07 -0700717 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800718 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700719 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800720 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700721 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
David Woodhouse6e03a202009-04-09 22:04:07 -0700723 new_pages = kmalloc(new_array_size * sizeof(void *),
724 GFP_KERNEL);
725 if (!new_pages) {
726 fw_load_abort(fw_priv);
727 return -ENOMEM;
728 }
Ming Lei12446912012-08-04 12:01:20 +0800729 memcpy(new_pages, buf->pages,
730 buf->page_array_size * sizeof(void *));
731 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
732 (new_array_size - buf->page_array_size));
733 kfree(buf->pages);
734 buf->pages = new_pages;
735 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700737
Ming Lei12446912012-08-04 12:01:20 +0800738 while (buf->nr_pages < pages_needed) {
739 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700740 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
741
Ming Lei12446912012-08-04 12:01:20 +0800742 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700743 fw_load_abort(fw_priv);
744 return -ENOMEM;
745 }
Ming Lei12446912012-08-04 12:01:20 +0800746 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return 0;
749}
750
751/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800752 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700753 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700754 * @kobj: kobject for the device
Randy Dunlap42e61f4a2007-07-23 21:42:11 -0700755 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800756 * @buffer: buffer being written
757 * @offset: buffer offset for write in total data store area
758 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800760 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 * the driver as a firmware image.
762 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700763static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
764 struct bin_attribute *bin_attr,
765 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200767 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700768 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800769 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 ssize_t retval;
771
772 if (!capable(CAP_SYS_RAWIO))
773 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700774
Laura Garciacad1e552006-05-23 23:22:38 +0200775 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800776 buf = fw_priv->buf;
777 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 retval = -ENODEV;
779 goto out;
780 }
Ming Lei65710cb2012-08-04 12:01:16 +0800781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 retval = fw_realloc_buffer(fw_priv, offset + count);
783 if (retval)
784 goto out;
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700787
788 while (count) {
789 void *page_data;
790 int page_nr = offset >> PAGE_SHIFT;
791 int page_ofs = offset & (PAGE_SIZE - 1);
792 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
793
Ming Lei12446912012-08-04 12:01:20 +0800794 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700795
796 memcpy(page_data + page_ofs, buffer, page_cnt);
797
Ming Lei12446912012-08-04 12:01:20 +0800798 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700799 buffer += page_cnt;
800 offset += page_cnt;
801 count -= page_cnt;
802 }
803
Ming Lei12446912012-08-04 12:01:20 +0800804 buf->size = max_t(size_t, offset, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805out:
Laura Garciacad1e552006-05-23 23:22:38 +0200806 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return retval;
808}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800809
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700810static struct bin_attribute firmware_attr_data = {
811 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 .size = 0,
813 .read = firmware_data_read,
814 .write = firmware_data_write,
815};
816
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800817static void firmware_class_timeout_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800819 struct firmware_priv *fw_priv = container_of(work,
820 struct firmware_priv, timeout_work.work);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700821
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800822 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 fw_load_abort(fw_priv);
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800824 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700827static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200828fw_create_instance(struct firmware *firmware, const char *fw_name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100829 struct device *device, unsigned int opt_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700831 struct firmware_priv *fw_priv;
832 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Ming Lei12446912012-08-04 12:01:20 +0800834 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700835 if (!fw_priv) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700836 dev_err(device, "%s: kmalloc failed\n", __func__);
Ming Lei12446912012-08-04 12:01:20 +0800837 fw_priv = ERR_PTR(-ENOMEM);
838 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100841 fw_priv->nowait = !!(opt_flags & FW_OPT_NOWAIT);
Ming Lei1f2b7952012-08-04 12:01:21 +0800842 fw_priv->fw = firmware;
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800843 INIT_DELAYED_WORK(&fw_priv->timeout_work,
844 firmware_class_timeout_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700846 f_dev = &fw_priv->dev;
847
848 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800849 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700850 f_dev->parent = device;
851 f_dev->class = &firmware_class;
Ming Lei12446912012-08-04 12:01:20 +0800852exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700853 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100855
856/* load a firmware via user helper */
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100857static int _request_firmware_load(struct firmware_priv *fw_priv,
858 unsigned int opt_flags, long timeout)
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100859{
860 int retval = 0;
861 struct device *f_dev = &fw_priv->dev;
862 struct firmware_buf *buf = fw_priv->buf;
863
864 /* fall back on userspace loading */
865 buf->is_paged_buf = true;
866
867 dev_set_uevent_suppress(f_dev, true);
868
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100869 retval = device_add(f_dev);
870 if (retval) {
871 dev_err(f_dev, "%s: device_register failed\n", __func__);
872 goto err_put_dev;
873 }
874
875 retval = device_create_bin_file(f_dev, &firmware_attr_data);
876 if (retval) {
877 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
878 goto err_del_dev;
879 }
880
Maxime Bizon1eeeef12013-08-29 20:28:13 +0200881 mutex_lock(&fw_lock);
882 list_add(&buf->pending_list, &pending_fw_head);
883 mutex_unlock(&fw_lock);
884
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100885 retval = device_create_file(f_dev, &dev_attr_loading);
886 if (retval) {
Maxime Bizon1eeeef12013-08-29 20:28:13 +0200887 mutex_lock(&fw_lock);
888 list_del_init(&buf->pending_list);
889 mutex_unlock(&fw_lock);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100890 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
891 goto err_del_bin_attr;
892 }
893
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100894 if (opt_flags & FW_OPT_UEVENT) {
Ming Leiaf5bc112013-05-27 18:30:04 +0800895 buf->need_uevent = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100896 dev_set_uevent_suppress(f_dev, false);
897 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
898 if (timeout != MAX_SCHEDULE_TIMEOUT)
Shaibal Duttabce66182014-01-31 15:44:58 -0800899 queue_delayed_work(system_power_efficient_wq,
900 &fw_priv->timeout_work, timeout);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100901
902 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
903 }
904
905 wait_for_completion(&buf->completion);
906
907 cancel_delayed_work_sync(&fw_priv->timeout_work);
zhang jun2b1278c2014-02-13 15:18:47 +0800908 if (!buf->data)
909 retval = -ENOMEM;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100910
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100911 device_remove_file(f_dev, &dev_attr_loading);
912err_del_bin_attr:
913 device_remove_bin_file(f_dev, &firmware_attr_data);
914err_del_dev:
915 device_del(f_dev);
916err_put_dev:
917 put_device(f_dev);
918 return retval;
919}
920
921static int fw_load_from_user_helper(struct firmware *firmware,
922 const char *name, struct device *device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100923 unsigned int opt_flags, long timeout)
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100924{
925 struct firmware_priv *fw_priv;
926
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100927 fw_priv = fw_create_instance(firmware, name, device, opt_flags);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100928 if (IS_ERR(fw_priv))
929 return PTR_ERR(fw_priv);
930
931 fw_priv->buf = firmware->priv;
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100932 return _request_firmware_load(fw_priv, opt_flags, timeout);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100933}
Ming Leiddf1f062013-06-04 10:01:13 +0800934
Ming Lei5b7cb7a2013-06-06 19:52:54 +0800935#ifdef CONFIG_PM_SLEEP
Ming Leiddf1f062013-06-04 10:01:13 +0800936/* kill pending requests without uevent to avoid blocking suspend */
937static void kill_requests_without_uevent(void)
938{
939 struct firmware_buf *buf;
940 struct firmware_buf *next;
941
942 mutex_lock(&fw_lock);
943 list_for_each_entry_safe(buf, next, &pending_fw_head, pending_list) {
944 if (!buf->need_uevent)
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700945 __fw_load_abort(buf);
Ming Leiddf1f062013-06-04 10:01:13 +0800946 }
947 mutex_unlock(&fw_lock);
948}
Ming Lei5b7cb7a2013-06-06 19:52:54 +0800949#endif
Ming Leiddf1f062013-06-04 10:01:13 +0800950
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100951#else /* CONFIG_FW_LOADER_USER_HELPER */
952static inline int
953fw_load_from_user_helper(struct firmware *firmware, const char *name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100954 struct device *device, unsigned int opt_flags,
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100955 long timeout)
956{
957 return -ENOENT;
958}
Takashi Iwai807be032013-01-31 11:13:57 +0100959
960/* No abort during direct loading */
961#define is_fw_load_aborted(buf) false
962
Ming Lei5b7cb7a2013-06-06 19:52:54 +0800963#ifdef CONFIG_PM_SLEEP
Ming Leiddf1f062013-06-04 10:01:13 +0800964static inline void kill_requests_without_uevent(void) { }
Ming Lei5b7cb7a2013-06-06 19:52:54 +0800965#endif
Ming Leiddf1f062013-06-04 10:01:13 +0800966
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100967#endif /* CONFIG_FW_LOADER_USER_HELPER */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Ming Leif531f05a2012-08-04 12:01:25 +0800969
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100970/* wait until the shared firmware_buf becomes ready (or error) */
971static int sync_cached_firmware_buf(struct firmware_buf *buf)
Ming Lei1f2b7952012-08-04 12:01:21 +0800972{
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100973 int ret = 0;
974
975 mutex_lock(&fw_lock);
976 while (!test_bit(FW_STATUS_DONE, &buf->status)) {
Takashi Iwai807be032013-01-31 11:13:57 +0100977 if (is_fw_load_aborted(buf)) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100978 ret = -ENOENT;
979 break;
980 }
981 mutex_unlock(&fw_lock);
982 wait_for_completion(&buf->completion);
983 mutex_lock(&fw_lock);
984 }
985 mutex_unlock(&fw_lock);
986 return ret;
Ming Lei1f2b7952012-08-04 12:01:21 +0800987}
988
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100989/* prepare firmware and firmware_buf structs;
990 * return 0 if a firmware is already assigned, 1 if need to load one,
991 * or a negative error code
992 */
993static int
994_request_firmware_prepare(struct firmware **firmware_p, const char *name,
995 struct device *device)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700996{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +0800998 struct firmware_buf *buf;
999 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Jiri Slaby4aed0642005-09-13 01:25:01 -07001001 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -07001003 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
1004 __func__);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001005 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001008 if (fw_get_builtin_firmware(firmware, name)) {
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +01001009 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001010 return 0; /* assigned */
David Woodhouse5658c762008-05-23 13:52:42 +01001011 }
1012
Ming Lei1f2b7952012-08-04 12:01:21 +08001013 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
Ming Lei1f2b7952012-08-04 12:01:21 +08001014
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001015 /*
1016 * bind with 'buf' now to avoid warning in failure path
1017 * of requesting firmware.
1018 */
1019 firmware->priv = buf;
Ming Lei1f2b7952012-08-04 12:01:21 +08001020
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001021 if (ret > 0) {
1022 ret = sync_cached_firmware_buf(buf);
1023 if (!ret) {
1024 fw_set_page_data(buf, firmware);
1025 return 0; /* assigned */
1026 }
Stephen Boyddddb5542012-03-28 23:30:43 +02001027 }
Ming Lei1f2b7952012-08-04 12:01:21 +08001028
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001029 if (ret < 0)
1030 return ret;
1031 return 1; /* need to load */
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001032}
1033
Ming Leie771d1a2013-05-27 18:30:03 +08001034static int assign_firmware_buf(struct firmware *fw, struct device *device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001035 unsigned int opt_flags)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001036{
1037 struct firmware_buf *buf = fw->priv;
1038
1039 mutex_lock(&fw_lock);
Takashi Iwai807be032013-01-31 11:13:57 +01001040 if (!buf->size || is_fw_load_aborted(buf)) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001041 mutex_unlock(&fw_lock);
1042 return -ENOENT;
1043 }
1044
1045 /*
1046 * add firmware name into devres list so that we can auto cache
1047 * and uncache firmware for device.
1048 *
1049 * device may has been deleted already, but the problem
1050 * should be fixed in devres or driver core.
1051 */
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001052 /* don't cache firmware handled without uevent */
1053 if (device && (opt_flags & FW_OPT_UEVENT))
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001054 fw_add_devm_name(device, buf->fw_id);
1055
1056 /*
1057 * After caching firmware image is started, let it piggyback
1058 * on request firmware.
1059 */
1060 if (buf->fwc->state == FW_LOADER_START_CACHE) {
1061 if (fw_cache_piggyback_on_request(buf->fw_id))
1062 kref_get(&buf->ref);
1063 }
1064
1065 /* pass the pages buffer to driver at the last minute */
1066 fw_set_page_data(buf, fw);
1067 mutex_unlock(&fw_lock);
1068 return 0;
1069}
1070
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001071/* called from request_firmware() and request_firmware_work_func() */
1072static int
1073_request_firmware(const struct firmware **firmware_p, const char *name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001074 struct device *device, unsigned int opt_flags)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001075{
1076 struct firmware *fw;
1077 long timeout;
1078 int ret;
1079
1080 if (!firmware_p)
1081 return -EINVAL;
1082
1083 ret = _request_firmware_prepare(&fw, name, device);
1084 if (ret <= 0) /* error or already assigned */
1085 goto out;
1086
1087 ret = 0;
1088 timeout = firmware_loading_timeout();
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001089 if (opt_flags & FW_OPT_NOWAIT) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001090 timeout = usermodehelper_read_lock_wait(timeout);
1091 if (!timeout) {
1092 dev_dbg(device, "firmware: %s loading timed out\n",
1093 name);
1094 ret = -EBUSY;
1095 goto out;
1096 }
1097 } else {
1098 ret = usermodehelper_read_trylock();
1099 if (WARN_ON(ret)) {
1100 dev_err(device, "firmware: %s will not be loaded\n",
1101 name);
1102 goto out;
1103 }
1104 }
1105
Neil Horman3e358ac2013-09-06 15:36:08 -04001106 ret = fw_get_filesystem_firmware(device, fw->priv);
1107 if (ret) {
Takashi Iwai5a1379e2014-06-04 17:48:15 +02001108 if (opt_flags & FW_OPT_USERHELPER) {
Takashi Iwaibba3a872013-12-02 15:38:16 +01001109 dev_warn(device,
1110 "Direct firmware load failed with error %d\n",
1111 ret);
1112 dev_warn(device, "Falling back to user helper\n");
1113 ret = fw_load_from_user_helper(fw, name, device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001114 opt_flags, timeout);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001115 }
Neil Horman3e358ac2013-09-06 15:36:08 -04001116 }
Ming Leie771d1a2013-05-27 18:30:03 +08001117
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001118 if (!ret)
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001119 ret = assign_firmware_buf(fw, device, opt_flags);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001120
1121 usermodehelper_read_unlock();
1122
1123 out:
1124 if (ret < 0) {
1125 release_firmware(fw);
1126 fw = NULL;
1127 }
1128
1129 *firmware_p = fw;
1130 return ret;
1131}
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133/**
Kay Sievers312c0042005-11-16 09:00:00 +01001134 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001135 * @firmware_p: pointer to firmware image
1136 * @name: name of firmware file
1137 * @device: device for which firmware is being loaded
1138 *
1139 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab02005-09-06 15:17:13 -07001140 * of @name for device @device.
1141 *
1142 * Should be called from user context where sleeping is allowed.
1143 *
Kay Sievers312c0042005-11-16 09:00:00 +01001144 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab02005-09-06 15:17:13 -07001145 * should be distinctive enough not to be confused with any other
1146 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +08001147 *
1148 * Caller must hold the reference count of @device.
Ming Lei6a927852012-11-03 17:48:16 +08001149 *
1150 * The function can be called safely inside device's suspend and
1151 * resume callback.
Abhay Salunke6e3eaab02005-09-06 15:17:13 -07001152 **/
1153int
1154request_firmware(const struct firmware **firmware_p, const char *name,
1155 struct device *device)
1156{
Ming Leid6c8aa32013-06-06 20:01:48 +08001157 int ret;
1158
1159 /* Need to pin this module until return */
1160 __module_get(THIS_MODULE);
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001161 ret = _request_firmware(firmware_p, name, device,
1162 FW_OPT_UEVENT | FW_OPT_FALLBACK);
Ming Leid6c8aa32013-06-06 20:01:48 +08001163 module_put(THIS_MODULE);
1164 return ret;
Abhay Salunke6e3eaab02005-09-06 15:17:13 -07001165}
Daniel Mackf4945132013-05-23 22:17:18 +02001166EXPORT_SYMBOL(request_firmware);
Abhay Salunke6e3eaab02005-09-06 15:17:13 -07001167
Takashi Iwai5a1379e2014-06-04 17:48:15 +02001168#ifdef CONFIG_FW_LOADER_USER_HELPER_FALLBACK
Takashi Iwaibba3a872013-12-02 15:38:16 +01001169/**
1170 * request_firmware: - load firmware directly without usermode helper
1171 * @firmware_p: pointer to firmware image
1172 * @name: name of firmware file
1173 * @device: device for which firmware is being loaded
1174 *
1175 * This function works pretty much like request_firmware(), but this doesn't
1176 * fall back to usermode helper even if the firmware couldn't be loaded
1177 * directly from fs. Hence it's useful for loading optional firmwares, which
1178 * aren't always present, without extra long timeouts of udev.
1179 **/
1180int request_firmware_direct(const struct firmware **firmware_p,
1181 const char *name, struct device *device)
1182{
1183 int ret;
1184 __module_get(THIS_MODULE);
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001185 ret = _request_firmware(firmware_p, name, device, FW_OPT_UEVENT);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001186 module_put(THIS_MODULE);
1187 return ret;
1188}
1189EXPORT_SYMBOL_GPL(request_firmware_direct);
1190#endif
1191
Abhay Salunke6e3eaab02005-09-06 15:17:13 -07001192/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001194 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001196void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
1198 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001199 if (!fw_is_builtin_firmware(fw))
1200 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 kfree(fw);
1202 }
1203}
Daniel Mackf4945132013-05-23 22:17:18 +02001204EXPORT_SYMBOL(release_firmware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206/* Async support */
1207struct firmware_work {
1208 struct work_struct work;
1209 struct module *module;
1210 const char *name;
1211 struct device *device;
1212 void *context;
1213 void (*cont)(const struct firmware *fw, void *context);
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001214 unsigned int opt_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215};
1216
Stephen Boyda36cf842012-03-28 23:31:00 +02001217static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Stephen Boyda36cf842012-03-28 23:31:00 +02001219 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 const struct firmware *fw;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001221
Stephen Boyda36cf842012-03-28 23:31:00 +02001222 fw_work = container_of(work, struct firmware_work, work);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001223
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001224 _request_firmware(&fw, fw_work->name, fw_work->device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001225 fw_work->opt_flags);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001226 fw_work->cont(fw, fw_work->context);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001227 put_device(fw_work->device); /* taken in request_firmware_nowait() */
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 module_put(fw_work->module);
1230 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
1233/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001234 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001235 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001236 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001237 * is non-zero else the firmware copy must be done manually.
1238 * @name: name of firmware file
1239 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001240 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001241 * @context: will be passed over to @cont, and
1242 * @fw may be %NULL if firmware request fails.
1243 * @cont: function will be called asynchronously when the firmware
1244 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001246 * Caller must hold the reference count of @device.
1247 *
Ming Lei6f21a622012-08-04 12:01:24 +08001248 * Asynchronous variant of request_firmware() for user contexts:
1249 * - sleep for as small periods as possible since it may
1250 * increase kernel boot time of built-in device drivers
1251 * requesting firmware in their ->probe() methods, if
1252 * @gfp is GFP_KERNEL.
1253 *
1254 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 **/
1256int
1257request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001258 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001259 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 void (*cont)(const struct firmware *fw, void *context))
1261{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001262 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001264 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (!fw_work)
1266 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001267
1268 fw_work->module = module;
1269 fw_work->name = name;
1270 fw_work->device = device;
1271 fw_work->context = context;
1272 fw_work->cont = cont;
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001273 fw_work->opt_flags = FW_OPT_NOWAIT | FW_OPT_FALLBACK |
Takashi Iwai5a1379e2014-06-04 17:48:15 +02001274 (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if (!try_module_get(module)) {
1277 kfree(fw_work);
1278 return -EFAULT;
1279 }
1280
Ming Lei0cfc1e12012-08-04 12:01:23 +08001281 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001282 INIT_WORK(&fw_work->work, request_firmware_work_func);
1283 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return 0;
1285}
Daniel Mackf4945132013-05-23 22:17:18 +02001286EXPORT_SYMBOL(request_firmware_nowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Ming Lei90f890812013-06-20 12:30:16 +08001288#ifdef CONFIG_PM_SLEEP
1289static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1290
Ming Lei2887b392012-08-04 12:01:22 +08001291/**
1292 * cache_firmware - cache one firmware image in kernel memory space
1293 * @fw_name: the firmware image name
1294 *
1295 * Cache firmware in kernel memory so that drivers can use it when
1296 * system isn't ready for them to request firmware image from userspace.
1297 * Once it returns successfully, driver can use request_firmware or its
1298 * nowait version to get the cached firmware without any interacting
1299 * with userspace
1300 *
1301 * Return 0 if the firmware image has been cached successfully
1302 * Return !0 otherwise
1303 *
1304 */
Ming Lei93232e42013-06-06 20:01:47 +08001305static int cache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001306{
1307 int ret;
1308 const struct firmware *fw;
1309
1310 pr_debug("%s: %s\n", __func__, fw_name);
1311
1312 ret = request_firmware(&fw, fw_name, NULL);
1313 if (!ret)
1314 kfree(fw);
1315
1316 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1317
1318 return ret;
1319}
1320
Ming Lei6a2c1232013-06-26 09:28:17 +08001321static struct firmware_buf *fw_lookup_buf(const char *fw_name)
1322{
1323 struct firmware_buf *tmp;
1324 struct firmware_cache *fwc = &fw_cache;
1325
1326 spin_lock(&fwc->lock);
1327 tmp = __fw_lookup_buf(fw_name);
1328 spin_unlock(&fwc->lock);
1329
1330 return tmp;
1331}
1332
Ming Lei2887b392012-08-04 12:01:22 +08001333/**
1334 * uncache_firmware - remove one cached firmware image
1335 * @fw_name: the firmware image name
1336 *
1337 * Uncache one firmware image which has been cached successfully
1338 * before.
1339 *
1340 * Return 0 if the firmware cache has been removed successfully
1341 * Return !0 otherwise
1342 *
1343 */
Ming Lei93232e42013-06-06 20:01:47 +08001344static int uncache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001345{
1346 struct firmware_buf *buf;
1347 struct firmware fw;
1348
1349 pr_debug("%s: %s\n", __func__, fw_name);
1350
1351 if (fw_get_builtin_firmware(&fw, fw_name))
1352 return 0;
1353
1354 buf = fw_lookup_buf(fw_name);
1355 if (buf) {
1356 fw_free_buf(buf);
1357 return 0;
1358 }
1359
1360 return -EINVAL;
1361}
1362
Ming Lei37276a52012-08-04 12:01:27 +08001363static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1364{
1365 struct fw_cache_entry *fce;
1366
1367 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1368 if (!fce)
1369 goto exit;
1370
1371 strcpy(fce->name, name);
1372exit:
1373 return fce;
1374}
1375
Ming Lei373304f2012-10-09 12:01:01 +08001376static int __fw_entry_found(const char *name)
1377{
1378 struct firmware_cache *fwc = &fw_cache;
1379 struct fw_cache_entry *fce;
1380
1381 list_for_each_entry(fce, &fwc->fw_names, list) {
1382 if (!strcmp(fce->name, name))
1383 return 1;
1384 }
1385 return 0;
1386}
1387
Ming Leiac39b3e2012-08-20 19:04:16 +08001388static int fw_cache_piggyback_on_request(const char *name)
1389{
1390 struct firmware_cache *fwc = &fw_cache;
1391 struct fw_cache_entry *fce;
1392 int ret = 0;
1393
1394 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001395 if (__fw_entry_found(name))
1396 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001397
1398 fce = alloc_fw_cache_entry(name);
1399 if (fce) {
1400 ret = 1;
1401 list_add(&fce->list, &fwc->fw_names);
1402 pr_debug("%s: fw: %s\n", __func__, name);
1403 }
1404found:
1405 spin_unlock(&fwc->name_lock);
1406 return ret;
1407}
1408
Ming Lei37276a52012-08-04 12:01:27 +08001409static void free_fw_cache_entry(struct fw_cache_entry *fce)
1410{
1411 kfree(fce);
1412}
1413
1414static void __async_dev_cache_fw_image(void *fw_entry,
1415 async_cookie_t cookie)
1416{
1417 struct fw_cache_entry *fce = fw_entry;
1418 struct firmware_cache *fwc = &fw_cache;
1419 int ret;
1420
1421 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001422 if (ret) {
1423 spin_lock(&fwc->name_lock);
1424 list_del(&fce->list);
1425 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001426
Ming Leiac39b3e2012-08-20 19:04:16 +08001427 free_fw_cache_entry(fce);
1428 }
Ming Lei37276a52012-08-04 12:01:27 +08001429}
1430
1431/* called with dev->devres_lock held */
1432static void dev_create_fw_entry(struct device *dev, void *res,
1433 void *data)
1434{
1435 struct fw_name_devm *fwn = res;
1436 const char *fw_name = fwn->name;
1437 struct list_head *head = data;
1438 struct fw_cache_entry *fce;
1439
1440 fce = alloc_fw_cache_entry(fw_name);
1441 if (fce)
1442 list_add(&fce->list, head);
1443}
1444
1445static int devm_name_match(struct device *dev, void *res,
1446 void *match_data)
1447{
1448 struct fw_name_devm *fwn = res;
1449 return (fwn->magic == (unsigned long)match_data);
1450}
1451
Ming Leiab6dd8e2012-08-17 22:07:00 +08001452static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001453{
1454 LIST_HEAD(todo);
1455 struct fw_cache_entry *fce;
1456 struct fw_cache_entry *fce_next;
1457 struct firmware_cache *fwc = &fw_cache;
1458
1459 devres_for_each_res(dev, fw_name_devm_release,
1460 devm_name_match, &fw_cache,
1461 dev_create_fw_entry, &todo);
1462
1463 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1464 list_del(&fce->list);
1465
1466 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001467 /* only one cache entry for one firmware */
1468 if (!__fw_entry_found(fce->name)) {
Ming Lei373304f2012-10-09 12:01:01 +08001469 list_add(&fce->list, &fwc->fw_names);
1470 } else {
1471 free_fw_cache_entry(fce);
1472 fce = NULL;
1473 }
Ming Lei37276a52012-08-04 12:01:27 +08001474 spin_unlock(&fwc->name_lock);
1475
Ming Lei373304f2012-10-09 12:01:01 +08001476 if (fce)
Ming Leid28d3882012-10-09 12:01:04 +08001477 async_schedule_domain(__async_dev_cache_fw_image,
1478 (void *)fce,
1479 &fw_cache_domain);
Ming Lei37276a52012-08-04 12:01:27 +08001480 }
1481}
1482
1483static void __device_uncache_fw_images(void)
1484{
1485 struct firmware_cache *fwc = &fw_cache;
1486 struct fw_cache_entry *fce;
1487
1488 spin_lock(&fwc->name_lock);
1489 while (!list_empty(&fwc->fw_names)) {
1490 fce = list_entry(fwc->fw_names.next,
1491 struct fw_cache_entry, list);
1492 list_del(&fce->list);
1493 spin_unlock(&fwc->name_lock);
1494
1495 uncache_firmware(fce->name);
1496 free_fw_cache_entry(fce);
1497
1498 spin_lock(&fwc->name_lock);
1499 }
1500 spin_unlock(&fwc->name_lock);
1501}
1502
1503/**
1504 * device_cache_fw_images - cache devices' firmware
1505 *
1506 * If one device called request_firmware or its nowait version
1507 * successfully before, the firmware names are recored into the
1508 * device's devres link list, so device_cache_fw_images can call
1509 * cache_firmware() to cache these firmwares for the device,
1510 * then the device driver can load its firmwares easily at
1511 * time when system is not ready to complete loading firmware.
1512 */
1513static void device_cache_fw_images(void)
1514{
1515 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001516 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001517 DEFINE_WAIT(wait);
1518
1519 pr_debug("%s\n", __func__);
1520
Ming Lei373304f2012-10-09 12:01:01 +08001521 /* cancel uncache work */
1522 cancel_delayed_work_sync(&fwc->work);
1523
Ming Leiffe53f62012-08-04 12:01:28 +08001524 /*
1525 * use small loading timeout for caching devices' firmware
1526 * because all these firmware images have been loaded
1527 * successfully at lease once, also system is ready for
1528 * completing firmware loading now. The maximum size of
1529 * firmware in current distributions is about 2M bytes,
1530 * so 10 secs should be enough.
1531 */
1532 old_timeout = loading_timeout;
1533 loading_timeout = 10;
1534
Ming Leiac39b3e2012-08-20 19:04:16 +08001535 mutex_lock(&fw_lock);
1536 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001537 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001538 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001539
1540 /* wait for completion of caching firmware for all devices */
Ming Leid28d3882012-10-09 12:01:04 +08001541 async_synchronize_full_domain(&fw_cache_domain);
Ming Leiffe53f62012-08-04 12:01:28 +08001542
1543 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001544}
1545
1546/**
1547 * device_uncache_fw_images - uncache devices' firmware
1548 *
1549 * uncache all firmwares which have been cached successfully
1550 * by device_uncache_fw_images earlier
1551 */
1552static void device_uncache_fw_images(void)
1553{
1554 pr_debug("%s\n", __func__);
1555 __device_uncache_fw_images();
1556}
1557
1558static void device_uncache_fw_images_work(struct work_struct *work)
1559{
1560 device_uncache_fw_images();
1561}
1562
1563/**
1564 * device_uncache_fw_images_delay - uncache devices firmwares
1565 * @delay: number of milliseconds to delay uncache device firmwares
1566 *
1567 * uncache all devices's firmwares which has been cached successfully
1568 * by device_cache_fw_images after @delay milliseconds.
1569 */
1570static void device_uncache_fw_images_delay(unsigned long delay)
1571{
Shaibal Duttabce66182014-01-31 15:44:58 -08001572 queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
1573 msecs_to_jiffies(delay));
Ming Lei37276a52012-08-04 12:01:27 +08001574}
1575
Ming Lei07646d92012-08-04 12:01:29 +08001576static int fw_pm_notify(struct notifier_block *notify_block,
1577 unsigned long mode, void *unused)
1578{
1579 switch (mode) {
1580 case PM_HIBERNATION_PREPARE:
1581 case PM_SUSPEND_PREPARE:
Sebastian Capellaf8d5b9e2014-02-18 17:52:08 -08001582 case PM_RESTORE_PREPARE:
Ming Leiaf5bc112013-05-27 18:30:04 +08001583 kill_requests_without_uevent();
Ming Lei07646d92012-08-04 12:01:29 +08001584 device_cache_fw_images();
1585 break;
1586
1587 case PM_POST_SUSPEND:
1588 case PM_POST_HIBERNATION:
1589 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001590 /*
1591 * In case that system sleep failed and syscore_suspend is
1592 * not called.
1593 */
1594 mutex_lock(&fw_lock);
1595 fw_cache.state = FW_LOADER_NO_CACHE;
1596 mutex_unlock(&fw_lock);
1597
Ming Lei07646d92012-08-04 12:01:29 +08001598 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1599 break;
1600 }
1601
1602 return 0;
1603}
Ming Lei07646d92012-08-04 12:01:29 +08001604
Ming Leiac39b3e2012-08-20 19:04:16 +08001605/* stop caching firmware once syscore_suspend is reached */
1606static int fw_suspend(void)
1607{
1608 fw_cache.state = FW_LOADER_NO_CACHE;
1609 return 0;
1610}
1611
1612static struct syscore_ops fw_syscore_ops = {
1613 .suspend = fw_suspend,
1614};
Ming Leicfe016b2012-09-08 17:32:30 +08001615#else
1616static int fw_cache_piggyback_on_request(const char *name)
1617{
1618 return 0;
1619}
1620#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001621
Ming Lei37276a52012-08-04 12:01:27 +08001622static void __init fw_cache_init(void)
1623{
1624 spin_lock_init(&fw_cache.lock);
1625 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001626 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001627
Ming Leicfe016b2012-09-08 17:32:30 +08001628#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +08001629 spin_lock_init(&fw_cache.name_lock);
1630 INIT_LIST_HEAD(&fw_cache.fw_names);
Ming Lei37276a52012-08-04 12:01:27 +08001631
Ming Lei37276a52012-08-04 12:01:27 +08001632 INIT_DELAYED_WORK(&fw_cache.work,
1633 device_uncache_fw_images_work);
Ming Lei07646d92012-08-04 12:01:29 +08001634
1635 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1636 register_pm_notifier(&fw_cache.pm_notify);
Ming Leiac39b3e2012-08-20 19:04:16 +08001637
1638 register_syscore_ops(&fw_syscore_ops);
Ming Leicfe016b2012-09-08 17:32:30 +08001639#endif
Ming Lei37276a52012-08-04 12:01:27 +08001640}
1641
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001642static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
Ming Lei1f2b7952012-08-04 12:01:21 +08001644 fw_cache_init();
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001645#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaife304142013-05-22 18:28:38 +02001646 register_reboot_notifier(&fw_shutdown_nb);
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001647 return class_register(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001648#else
1649 return 0;
1650#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001652
1653static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
Ming Leicfe016b2012-09-08 17:32:30 +08001655#ifdef CONFIG_PM_SLEEP
Ming Leiac39b3e2012-08-20 19:04:16 +08001656 unregister_syscore_ops(&fw_syscore_ops);
Ming Lei07646d92012-08-04 12:01:29 +08001657 unregister_pm_notifier(&fw_cache.pm_notify);
Ming Leicfe016b2012-09-08 17:32:30 +08001658#endif
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001659#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaife304142013-05-22 18:28:38 +02001660 unregister_reboot_notifier(&fw_shutdown_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 class_unregister(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001662#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
1664
Shaohua Lia30a6a22006-09-27 01:50:52 -07001665fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666module_exit(firmware_class_exit);