blob: 01d55723d82cfae3cfbfd518befea0352c1e873a [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>
Mimi Zohare40ba6d2015-11-19 12:39:22 -050026#include <linux/fs.h>
Ming Lei37276a52012-08-04 12:01:27 +080027#include <linux/async.h>
28#include <linux/pm.h>
Ming Lei07646d92012-08-04 12:01:29 +080029#include <linux/suspend.h>
Ming Leiac39b3e2012-08-20 19:04:16 +080030#include <linux/syscore_ops.h>
Takashi Iwaife304142013-05-22 18:28:38 +020031#include <linux/reboot.h>
Kees Cook6593d922014-02-25 13:06:00 -080032#include <linux/security.h>
Ming Lei37276a52012-08-04 12:01:27 +080033
Linus Torvaldsabb139e2012-10-03 15:58:32 -070034#include <generated/utsrelease.h>
35
Ming Lei37276a52012-08-04 12:01:27 +080036#include "base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Markus Rechberger87d37a42007-06-04 18:45:44 +020038MODULE_AUTHOR("Manuel Estrada Sainz");
Linus Torvalds1da177e2005-04-16 15:20:36 -070039MODULE_DESCRIPTION("Multi purpose firmware loading support");
40MODULE_LICENSE("GPL");
41
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080042/* Builtin firmware support */
43
44#ifdef CONFIG_FW_LOADER
45
46extern struct builtin_fw __start_builtin_fw[];
47extern struct builtin_fw __end_builtin_fw[];
48
49static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
50{
51 struct builtin_fw *b_fw;
52
53 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
54 if (strcmp(name, b_fw->name) == 0) {
55 fw->size = b_fw->size;
56 fw->data = b_fw->data;
57 return true;
58 }
59 }
60
61 return false;
62}
63
64static bool fw_is_builtin_firmware(const struct firmware *fw)
65{
66 struct builtin_fw *b_fw;
67
68 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
69 if (fw->data == b_fw->data)
70 return true;
71
72 return false;
73}
74
75#else /* Module case - no builtin firmware support */
76
77static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
78{
79 return false;
80}
81
82static inline bool fw_is_builtin_firmware(const struct firmware *fw)
83{
84 return false;
85}
86#endif
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088enum {
89 FW_STATUS_LOADING,
90 FW_STATUS_DONE,
91 FW_STATUS_ABORT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070092};
93
Dave Jones2f651682007-01-25 15:56:15 -050094static int loading_timeout = 60; /* In seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +020096static inline long firmware_loading_timeout(void)
97{
Ming Lei68ff2a02015-01-13 00:02:01 +080098 return loading_timeout > 0 ? loading_timeout * HZ : MAX_JIFFY_OFFSET;
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +020099}
100
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100101/* firmware behavior options */
102#define FW_OPT_UEVENT (1U << 0)
103#define FW_OPT_NOWAIT (1U << 1)
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100104#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwai5a1379e2014-06-04 17:48:15 +0200105#define FW_OPT_USERHELPER (1U << 2)
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100106#else
Takashi Iwai5a1379e2014-06-04 17:48:15 +0200107#define FW_OPT_USERHELPER 0
108#endif
109#ifdef CONFIG_FW_LOADER_USER_HELPER_FALLBACK
110#define FW_OPT_FALLBACK FW_OPT_USERHELPER
111#else
112#define FW_OPT_FALLBACK 0
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100113#endif
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -0700114#define FW_OPT_NO_WARN (1U << 3)
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100115
Ming Lei1f2b7952012-08-04 12:01:21 +0800116struct firmware_cache {
117 /* firmware_buf instance will be added into the below list */
118 spinlock_t lock;
119 struct list_head head;
Ming Leicfe016b2012-09-08 17:32:30 +0800120 int state;
Ming Lei37276a52012-08-04 12:01:27 +0800121
Ming Leicfe016b2012-09-08 17:32:30 +0800122#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +0800123 /*
124 * Names of firmware images which have been cached successfully
125 * will be added into the below list so that device uncache
126 * helper can trace which firmware images have been cached
127 * before.
128 */
129 spinlock_t name_lock;
130 struct list_head fw_names;
131
Ming Lei37276a52012-08-04 12:01:27 +0800132 struct delayed_work work;
Ming Lei07646d92012-08-04 12:01:29 +0800133
134 struct notifier_block pm_notify;
Ming Leicfe016b2012-09-08 17:32:30 +0800135#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800136};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Ming Lei12446912012-08-04 12:01:20 +0800138struct firmware_buf {
Ming Lei1f2b7952012-08-04 12:01:21 +0800139 struct kref ref;
140 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 struct completion completion;
Ming Lei1f2b7952012-08-04 12:01:21 +0800142 struct firmware_cache *fwc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 unsigned long status;
Ming Lei65710cb2012-08-04 12:01:16 +0800144 void *data;
145 size_t size;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100146#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100147 bool is_paged_buf;
Ming Leiaf5bc112013-05-27 18:30:04 +0800148 bool need_uevent;
David Woodhouse6e03a202009-04-09 22:04:07 -0700149 struct page **pages;
150 int nr_pages;
151 int page_array_size;
Takashi Iwaife304142013-05-22 18:28:38 +0200152 struct list_head pending_list;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100153#endif
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700154 const char *fw_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155};
156
Ming Lei37276a52012-08-04 12:01:27 +0800157struct fw_cache_entry {
158 struct list_head list;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700159 const char *name;
Ming Lei37276a52012-08-04 12:01:27 +0800160};
161
Ming Leif531f05a2012-08-04 12:01:25 +0800162struct fw_name_devm {
163 unsigned long magic;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700164 const char *name;
Ming Leif531f05a2012-08-04 12:01:25 +0800165};
166
Ming Lei1f2b7952012-08-04 12:01:21 +0800167#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
168
Ming Leiac39b3e2012-08-20 19:04:16 +0800169#define FW_LOADER_NO_CACHE 0
170#define FW_LOADER_START_CACHE 1
171
172static int fw_cache_piggyback_on_request(const char *name);
173
Ming Lei1f2b7952012-08-04 12:01:21 +0800174/* fw_lock could be moved to 'struct firmware_priv' but since it is just
175 * guarding for corner cases a global lock should be OK */
176static DEFINE_MUTEX(fw_lock);
177
178static struct firmware_cache fw_cache;
179
180static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
181 struct firmware_cache *fwc)
182{
183 struct firmware_buf *buf;
184
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700185 buf = kzalloc(sizeof(*buf), GFP_ATOMIC);
Ming Lei1f2b7952012-08-04 12:01:21 +0800186 if (!buf)
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700187 return NULL;
188
189 buf->fw_id = kstrdup_const(fw_name, GFP_ATOMIC);
190 if (!buf->fw_id) {
191 kfree(buf);
192 return NULL;
193 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800194
195 kref_init(&buf->ref);
Ming Lei1f2b7952012-08-04 12:01:21 +0800196 buf->fwc = fwc;
197 init_completion(&buf->completion);
Takashi Iwaife304142013-05-22 18:28:38 +0200198#ifdef CONFIG_FW_LOADER_USER_HELPER
199 INIT_LIST_HEAD(&buf->pending_list);
200#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800201
202 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
203
204 return buf;
205}
206
Ming Lei2887b392012-08-04 12:01:22 +0800207static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
208{
209 struct firmware_buf *tmp;
210 struct firmware_cache *fwc = &fw_cache;
211
212 list_for_each_entry(tmp, &fwc->head, list)
213 if (!strcmp(tmp->fw_id, fw_name))
214 return tmp;
215 return NULL;
216}
217
Ming Lei1f2b7952012-08-04 12:01:21 +0800218static int fw_lookup_and_allocate_buf(const char *fw_name,
219 struct firmware_cache *fwc,
220 struct firmware_buf **buf)
221{
222 struct firmware_buf *tmp;
223
224 spin_lock(&fwc->lock);
Ming Lei2887b392012-08-04 12:01:22 +0800225 tmp = __fw_lookup_buf(fw_name);
226 if (tmp) {
227 kref_get(&tmp->ref);
228 spin_unlock(&fwc->lock);
229 *buf = tmp;
230 return 1;
231 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800232 tmp = __allocate_fw_buf(fw_name, fwc);
233 if (tmp)
234 list_add(&tmp->list, &fwc->head);
235 spin_unlock(&fwc->lock);
236
237 *buf = tmp;
238
239 return tmp ? 0 : -ENOMEM;
240}
241
242static void __fw_free_buf(struct kref *ref)
Bart Van Assche98233b22014-01-04 14:20:36 +0100243 __releases(&fwc->lock)
Ming Lei1f2b7952012-08-04 12:01:21 +0800244{
245 struct firmware_buf *buf = to_fwbuf(ref);
246 struct firmware_cache *fwc = buf->fwc;
Ming Lei1f2b7952012-08-04 12:01:21 +0800247
248 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
249 __func__, buf->fw_id, buf, buf->data,
250 (unsigned int)buf->size);
251
Ming Lei1f2b7952012-08-04 12:01:21 +0800252 list_del(&buf->list);
253 spin_unlock(&fwc->lock);
254
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100255#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100256 if (buf->is_paged_buf) {
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100257 int i;
Ming Lei746058f2012-10-09 12:01:03 +0800258 vunmap(buf->data);
259 for (i = 0; i < buf->nr_pages; i++)
260 __free_page(buf->pages[i]);
Chen Feng10a3fbf2015-12-16 17:03:15 +0800261 vfree(buf->pages);
Ming Lei746058f2012-10-09 12:01:03 +0800262 } else
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100263#endif
Ming Lei746058f2012-10-09 12:01:03 +0800264 vfree(buf->data);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700265 kfree_const(buf->fw_id);
Ming Lei1f2b7952012-08-04 12:01:21 +0800266 kfree(buf);
267}
268
269static void fw_free_buf(struct firmware_buf *buf)
270{
Chuansheng Liubd9eb7f2012-11-10 01:27:22 +0800271 struct firmware_cache *fwc = buf->fwc;
272 spin_lock(&fwc->lock);
273 if (!kref_put(&buf->ref, __fw_free_buf))
274 spin_unlock(&fwc->lock);
Ming Lei1f2b7952012-08-04 12:01:21 +0800275}
276
Ming Lei746058f2012-10-09 12:01:03 +0800277/* direct firmware loading support */
Ming Lei27602842012-11-03 17:47:58 +0800278static char fw_path_para[256];
279static const char * const fw_path[] = {
280 fw_path_para,
Ming Lei746058f2012-10-09 12:01:03 +0800281 "/lib/firmware/updates/" UTS_RELEASE,
282 "/lib/firmware/updates",
283 "/lib/firmware/" UTS_RELEASE,
284 "/lib/firmware"
285};
286
Ming Lei27602842012-11-03 17:47:58 +0800287/*
288 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
289 * from kernel command line because firmware_class is generally built in
290 * kernel instead of module.
291 */
292module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
293MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
294
Luis R. Rodriguez5275d192015-07-30 15:48:57 -0700295static void fw_finish_direct_load(struct device *device,
296 struct firmware_buf *buf)
297{
298 mutex_lock(&fw_lock);
299 set_bit(FW_STATUS_DONE, &buf->status);
300 complete_all(&buf->completion);
301 mutex_unlock(&fw_lock);
302}
303
Neil Horman3e358ac2013-09-06 15:36:08 -0400304static int fw_get_filesystem_firmware(struct device *device,
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100305 struct firmware_buf *buf)
Ming Lei746058f2012-10-09 12:01:03 +0800306{
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500307 loff_t size;
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700308 int i, len;
Neil Horman3e358ac2013-09-06 15:36:08 -0400309 int rc = -ENOENT;
Luis R. Rodriguezf5727b02015-05-12 14:49:40 -0700310 char *path;
311
312 path = __getname();
313 if (!path)
314 return -ENOMEM;
Ming Lei746058f2012-10-09 12:01:03 +0800315
316 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
Ming Lei27602842012-11-03 17:47:58 +0800317 /* skip the unset customized path */
318 if (!fw_path[i][0])
319 continue;
320
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700321 len = snprintf(path, PATH_MAX, "%s/%s",
322 fw_path[i], buf->fw_id);
323 if (len >= PATH_MAX) {
324 rc = -ENAMETOOLONG;
325 break;
326 }
Ming Lei746058f2012-10-09 12:01:03 +0800327
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500328 buf->size = 0;
329 rc = kernel_read_file_from_path(path, &buf->data, &size,
330 INT_MAX, READING_FIRMWARE);
Kees Cook4b2530d2016-02-04 13:15:02 -0800331 if (rc) {
Luis R. Rodriguez8e516aa2016-02-28 21:57:55 +0100332 if (rc == -ENOENT)
333 dev_dbg(device, "loading %s failed with error %d\n",
334 path, rc);
335 else
336 dev_warn(device, "loading %s failed with error %d\n",
337 path, rc);
Kees Cook4b2530d2016-02-04 13:15:02 -0800338 continue;
339 }
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500340 dev_dbg(device, "direct-loading %s\n", buf->fw_id);
341 buf->size = size;
Luis R. Rodriguez5275d192015-07-30 15:48:57 -0700342 fw_finish_direct_load(device, buf);
Kees Cook4b2530d2016-02-04 13:15:02 -0800343 break;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100344 }
Kees Cook4b2530d2016-02-04 13:15:02 -0800345 __putname(path);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100346
Neil Horman3e358ac2013-09-06 15:36:08 -0400347 return rc;
Ming Lei746058f2012-10-09 12:01:03 +0800348}
349
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100350/* firmware holds the ownership of pages */
351static void firmware_free_data(const struct firmware *fw)
352{
353 /* Loaded directly? */
354 if (!fw->priv) {
355 vfree(fw->data);
356 return;
357 }
358 fw_free_buf(fw->priv);
359}
360
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100361/* store the pages buffer info firmware from buf */
362static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
363{
364 fw->priv = buf;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100365#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100366 fw->pages = buf->pages;
367#endif
368 fw->size = buf->size;
369 fw->data = buf->data;
370
371 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
372 __func__, buf->fw_id, buf, buf->data,
373 (unsigned int)buf->size);
374}
375
376#ifdef CONFIG_PM_SLEEP
377static void fw_name_devm_release(struct device *dev, void *res)
378{
379 struct fw_name_devm *fwn = res;
380
381 if (fwn->magic == (unsigned long)&fw_cache)
382 pr_debug("%s: fw_name-%s devm-%p released\n",
383 __func__, fwn->name, res);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700384 kfree_const(fwn->name);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100385}
386
387static int fw_devm_match(struct device *dev, void *res,
388 void *match_data)
389{
390 struct fw_name_devm *fwn = res;
391
392 return (fwn->magic == (unsigned long)&fw_cache) &&
393 !strcmp(fwn->name, match_data);
394}
395
396static struct fw_name_devm *fw_find_devm_name(struct device *dev,
397 const char *name)
398{
399 struct fw_name_devm *fwn;
400
401 fwn = devres_find(dev, fw_name_devm_release,
402 fw_devm_match, (void *)name);
403 return fwn;
404}
405
406/* add firmware name into devres list */
407static int fw_add_devm_name(struct device *dev, const char *name)
408{
409 struct fw_name_devm *fwn;
410
411 fwn = fw_find_devm_name(dev, name);
412 if (fwn)
413 return 1;
414
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700415 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
416 GFP_KERNEL);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100417 if (!fwn)
418 return -ENOMEM;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700419 fwn->name = kstrdup_const(name, GFP_KERNEL);
420 if (!fwn->name) {
Vladimir Zapolskiya885de62015-07-29 23:26:28 +0300421 devres_free(fwn);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700422 return -ENOMEM;
423 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100424
425 fwn->magic = (unsigned long)&fw_cache;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100426 devres_add(dev, fwn);
427
428 return 0;
429}
430#else
431static int fw_add_devm_name(struct device *dev, const char *name)
432{
433 return 0;
434}
435#endif
436
437
438/*
439 * user-mode helper code
440 */
441#ifdef CONFIG_FW_LOADER_USER_HELPER
442struct firmware_priv {
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100443 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
Linus Torvalds6f957722015-07-09 11:20:01 -0700541static int do_firmware_uevent(struct firmware_priv *fw_priv, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Ming Lei12446912012-08-04 12:01:20 +0800543 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200545 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700546 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200547 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
548 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 return 0;
551}
552
Linus Torvalds6f957722015-07-09 11:20:01 -0700553static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
554{
555 struct firmware_priv *fw_priv = to_firmware_priv(dev);
556 int err = 0;
557
558 mutex_lock(&fw_lock);
559 if (fw_priv->buf)
560 err = do_firmware_uevent(fw_priv, env);
561 mutex_unlock(&fw_lock);
562 return err;
563}
564
Adrian Bunk1b81d662006-05-20 15:00:16 -0700565static struct class firmware_class = {
566 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800567 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700568 .dev_uevent = firmware_uevent,
569 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700570};
571
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700572static ssize_t firmware_loading_show(struct device *dev,
573 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700575 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800576 int loading = 0;
577
578 mutex_lock(&fw_lock);
579 if (fw_priv->buf)
580 loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
581 mutex_unlock(&fw_lock);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return sprintf(buf, "%d\n", loading);
584}
585
David Woodhouse6e03a202009-04-09 22:04:07 -0700586/* Some architectures don't have PAGE_KERNEL_RO */
587#ifndef PAGE_KERNEL_RO
588#define PAGE_KERNEL_RO PAGE_KERNEL
589#endif
Ming Lei253c9242012-10-09 12:01:02 +0800590
591/* one pages buffer should be mapped/unmapped only once */
592static int fw_map_pages_buf(struct firmware_buf *buf)
593{
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100594 if (!buf->is_paged_buf)
Ming Lei746058f2012-10-09 12:01:03 +0800595 return 0;
596
Markus Elfringdaa3d672014-11-19 11:38:38 +0100597 vunmap(buf->data);
Ming Lei253c9242012-10-09 12:01:02 +0800598 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
599 if (!buf->data)
600 return -ENOMEM;
601 return 0;
602}
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800605 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700606 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800607 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800608 * @buf: buffer to scan for loading control value
609 * @count: number of bytes in @buf
610 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 * The relevant values are:
612 *
613 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800614 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 * -1: Conclude the load with an error and discard any written data.
616 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700617static ssize_t firmware_loading_store(struct device *dev,
618 struct device_attribute *attr,
619 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700621 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800622 struct firmware_buf *fw_buf;
Kees Cook6593d922014-02-25 13:06:00 -0800623 ssize_t written = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700625 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Neil Hormaneea915b2012-01-02 15:31:23 -0500627 mutex_lock(&fw_lock);
Ming Lei87597932013-06-15 16:36:38 +0800628 fw_buf = fw_priv->buf;
Ming Lei12446912012-08-04 12:01:20 +0800629 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500630 goto out;
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 switch (loading) {
633 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800634 /* discarding any previous partial load */
Ming Lei12446912012-08-04 12:01:20 +0800635 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
636 for (i = 0; i < fw_buf->nr_pages; i++)
637 __free_page(fw_buf->pages[i]);
Chen Feng10a3fbf2015-12-16 17:03:15 +0800638 vfree(fw_buf->pages);
Ming Lei12446912012-08-04 12:01:20 +0800639 fw_buf->pages = NULL;
640 fw_buf->page_array_size = 0;
641 fw_buf->nr_pages = 0;
642 set_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei28eefa72012-08-04 12:01:17 +0800643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 break;
645 case 0:
Ming Lei12446912012-08-04 12:01:20 +0800646 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
Kees Cook6593d922014-02-25 13:06:00 -0800647 int rc;
648
Ming Lei12446912012-08-04 12:01:20 +0800649 set_bit(FW_STATUS_DONE, &fw_buf->status);
650 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei253c9242012-10-09 12:01:02 +0800651
652 /*
653 * Several loading requests may be pending on
654 * one same firmware buf, so let all requests
655 * see the mapped 'buf->data' once the loading
656 * is completed.
657 * */
Kees Cook6593d922014-02-25 13:06:00 -0800658 rc = fw_map_pages_buf(fw_buf);
659 if (rc)
zhang jun2b1278c2014-02-13 15:18:47 +0800660 dev_err(dev, "%s: map pages failed\n",
661 __func__);
Kees Cook6593d922014-02-25 13:06:00 -0800662 else
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500663 rc = security_kernel_post_read_file(NULL,
664 fw_buf->data, fw_buf->size,
665 READING_FIRMWARE);
Kees Cook6593d922014-02-25 13:06:00 -0800666
667 /*
668 * Same logic as fw_load_abort, only the DONE bit
669 * is ignored and we set ABORT only on failure.
670 */
Takashi Iwaife304142013-05-22 18:28:38 +0200671 list_del_init(&fw_buf->pending_list);
Kees Cook6593d922014-02-25 13:06:00 -0800672 if (rc) {
673 set_bit(FW_STATUS_ABORT, &fw_buf->status);
674 written = rc;
675 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800676 complete_all(&fw_buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 break;
678 }
679 /* fallthrough */
680 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700681 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 /* fallthrough */
683 case -1:
684 fw_load_abort(fw_priv);
685 break;
686 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500687out:
688 mutex_unlock(&fw_lock);
Kees Cook6593d922014-02-25 13:06:00 -0800689 return written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700692static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700694static void firmware_rw(struct firmware_buf *buf, char *buffer,
695 loff_t offset, size_t count, bool read)
696{
697 while (count) {
698 void *page_data;
699 int page_nr = offset >> PAGE_SHIFT;
700 int page_ofs = offset & (PAGE_SIZE-1);
701 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
702
703 page_data = kmap(buf->pages[page_nr]);
704
705 if (read)
706 memcpy(buffer, page_data + page_ofs, page_cnt);
707 else
708 memcpy(page_data + page_ofs, buffer, page_cnt);
709
710 kunmap(buf->pages[page_nr]);
711 buffer += page_cnt;
712 offset += page_cnt;
713 count -= page_cnt;
714 }
715}
716
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700717static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
718 struct bin_attribute *bin_attr,
719 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200721 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700722 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800723 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700724 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Laura Garciacad1e552006-05-23 23:22:38 +0200726 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800727 buf = fw_priv->buf;
728 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 ret_count = -ENODEV;
730 goto out;
731 }
Ming Lei12446912012-08-04 12:01:20 +0800732 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200733 ret_count = 0;
734 goto out;
735 }
Ming Lei12446912012-08-04 12:01:20 +0800736 if (count > buf->size - offset)
737 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700738
739 ret_count = count;
740
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700741 firmware_rw(buf, buffer, offset, count, true);
David Woodhouse6e03a202009-04-09 22:04:07 -0700742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743out:
Laura Garciacad1e552006-05-23 23:22:38 +0200744 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return ret_count;
746}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800747
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700748static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Ming Lei12446912012-08-04 12:01:20 +0800750 struct firmware_buf *buf = fw_priv->buf;
Fabian Fredericka76040d2014-07-01 21:13:30 +0200751 int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
David Woodhouse6e03a202009-04-09 22:04:07 -0700753 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800754 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700755 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800756 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700757 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Chen Feng10a3fbf2015-12-16 17:03:15 +0800759 new_pages = vmalloc(new_array_size * sizeof(void *));
David Woodhouse6e03a202009-04-09 22:04:07 -0700760 if (!new_pages) {
761 fw_load_abort(fw_priv);
762 return -ENOMEM;
763 }
Ming Lei12446912012-08-04 12:01:20 +0800764 memcpy(new_pages, buf->pages,
765 buf->page_array_size * sizeof(void *));
766 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
767 (new_array_size - buf->page_array_size));
Chen Feng10a3fbf2015-12-16 17:03:15 +0800768 vfree(buf->pages);
Ming Lei12446912012-08-04 12:01:20 +0800769 buf->pages = new_pages;
770 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700772
Ming Lei12446912012-08-04 12:01:20 +0800773 while (buf->nr_pages < pages_needed) {
774 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700775 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
776
Ming Lei12446912012-08-04 12:01:20 +0800777 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700778 fw_load_abort(fw_priv);
779 return -ENOMEM;
780 }
Ming Lei12446912012-08-04 12:01:20 +0800781 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return 0;
784}
785
786/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800787 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700788 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700789 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700790 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800791 * @buffer: buffer being written
792 * @offset: buffer offset for write in total data store area
793 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800795 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 * the driver as a firmware image.
797 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700798static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
799 struct bin_attribute *bin_attr,
800 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200802 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700803 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800804 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 ssize_t retval;
806
807 if (!capable(CAP_SYS_RAWIO))
808 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700809
Laura Garciacad1e552006-05-23 23:22:38 +0200810 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800811 buf = fw_priv->buf;
812 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 retval = -ENODEV;
814 goto out;
815 }
Ming Lei65710cb2012-08-04 12:01:16 +0800816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 retval = fw_realloc_buffer(fw_priv, offset + count);
818 if (retval)
819 goto out;
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 retval = count;
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700822 firmware_rw(buf, buffer, offset, count, false);
David Woodhouse6e03a202009-04-09 22:04:07 -0700823
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700824 buf->size = max_t(size_t, offset + count, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825out:
Laura Garciacad1e552006-05-23 23:22:38 +0200826 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return retval;
828}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800829
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700830static struct bin_attribute firmware_attr_data = {
831 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 .size = 0,
833 .read = firmware_data_read,
834 .write = firmware_data_write,
835};
836
Takashi Iwai46239902015-02-04 15:18:07 +0100837static struct attribute *fw_dev_attrs[] = {
838 &dev_attr_loading.attr,
839 NULL
840};
841
842static struct bin_attribute *fw_dev_bin_attrs[] = {
843 &firmware_attr_data,
844 NULL
845};
846
847static const struct attribute_group fw_dev_attr_group = {
848 .attrs = fw_dev_attrs,
849 .bin_attrs = fw_dev_bin_attrs,
850};
851
852static const struct attribute_group *fw_dev_attr_groups[] = {
853 &fw_dev_attr_group,
854 NULL
855};
856
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700857static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200858fw_create_instance(struct firmware *firmware, const char *fw_name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100859 struct device *device, unsigned int opt_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700861 struct firmware_priv *fw_priv;
862 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Ming Lei12446912012-08-04 12:01:20 +0800864 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700865 if (!fw_priv) {
Ming Lei12446912012-08-04 12:01:20 +0800866 fw_priv = ERR_PTR(-ENOMEM);
867 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100870 fw_priv->nowait = !!(opt_flags & FW_OPT_NOWAIT);
Ming Lei1f2b7952012-08-04 12:01:21 +0800871 fw_priv->fw = firmware;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700872 f_dev = &fw_priv->dev;
873
874 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800875 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700876 f_dev->parent = device;
877 f_dev->class = &firmware_class;
Takashi Iwai46239902015-02-04 15:18:07 +0100878 f_dev->groups = fw_dev_attr_groups;
Ming Lei12446912012-08-04 12:01:20 +0800879exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700880 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100882
883/* load a firmware via user helper */
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100884static int _request_firmware_load(struct firmware_priv *fw_priv,
885 unsigned int opt_flags, long timeout)
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100886{
887 int retval = 0;
888 struct device *f_dev = &fw_priv->dev;
889 struct firmware_buf *buf = fw_priv->buf;
890
891 /* fall back on userspace loading */
892 buf->is_paged_buf = true;
893
894 dev_set_uevent_suppress(f_dev, true);
895
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100896 retval = device_add(f_dev);
897 if (retval) {
898 dev_err(f_dev, "%s: device_register failed\n", __func__);
899 goto err_put_dev;
900 }
901
Maxime Bizon1eeeef12013-08-29 20:28:13 +0200902 mutex_lock(&fw_lock);
903 list_add(&buf->pending_list, &pending_fw_head);
904 mutex_unlock(&fw_lock);
905
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100906 if (opt_flags & FW_OPT_UEVENT) {
Ming Leiaf5bc112013-05-27 18:30:04 +0800907 buf->need_uevent = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100908 dev_set_uevent_suppress(f_dev, false);
909 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100910 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
Ming Lei68ff2a02015-01-13 00:02:01 +0800911 } else {
912 timeout = MAX_JIFFY_OFFSET;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100913 }
914
Ming Lei68ff2a02015-01-13 00:02:01 +0800915 retval = wait_for_completion_interruptible_timeout(&buf->completion,
916 timeout);
917 if (retval == -ERESTARTSYS || !retval) {
Ming Lei0cb64242015-01-13 00:01:35 +0800918 mutex_lock(&fw_lock);
919 fw_load_abort(fw_priv);
920 mutex_unlock(&fw_lock);
Zahari Doychevef518cc2015-03-10 10:45:40 +0100921 } else if (retval > 0) {
922 retval = 0;
Ming Lei0cb64242015-01-13 00:01:35 +0800923 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100924
Shuah Khan0542ad82014-07-22 18:10:38 -0600925 if (is_fw_load_aborted(buf))
926 retval = -EAGAIN;
927 else if (!buf->data)
zhang jun2b1278c2014-02-13 15:18:47 +0800928 retval = -ENOMEM;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100929
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100930 device_del(f_dev);
931err_put_dev:
932 put_device(f_dev);
933 return retval;
934}
935
936static int fw_load_from_user_helper(struct firmware *firmware,
937 const char *name, struct device *device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100938 unsigned int opt_flags, long timeout)
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100939{
940 struct firmware_priv *fw_priv;
941
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100942 fw_priv = fw_create_instance(firmware, name, device, opt_flags);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100943 if (IS_ERR(fw_priv))
944 return PTR_ERR(fw_priv);
945
946 fw_priv->buf = firmware->priv;
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100947 return _request_firmware_load(fw_priv, opt_flags, timeout);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100948}
Ming Leiddf1f062013-06-04 10:01:13 +0800949
Ming Lei5b7cb7a2013-06-06 19:52:54 +0800950#ifdef CONFIG_PM_SLEEP
Ming Leiddf1f062013-06-04 10:01:13 +0800951/* kill pending requests without uevent to avoid blocking suspend */
952static void kill_requests_without_uevent(void)
953{
954 struct firmware_buf *buf;
955 struct firmware_buf *next;
956
957 mutex_lock(&fw_lock);
958 list_for_each_entry_safe(buf, next, &pending_fw_head, pending_list) {
959 if (!buf->need_uevent)
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700960 __fw_load_abort(buf);
Ming Leiddf1f062013-06-04 10:01:13 +0800961 }
962 mutex_unlock(&fw_lock);
963}
Ming Lei5b7cb7a2013-06-06 19:52:54 +0800964#endif
Ming Leiddf1f062013-06-04 10:01:13 +0800965
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100966#else /* CONFIG_FW_LOADER_USER_HELPER */
967static inline int
968fw_load_from_user_helper(struct firmware *firmware, const char *name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100969 struct device *device, unsigned int opt_flags,
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100970 long timeout)
971{
972 return -ENOENT;
973}
Takashi Iwai807be032013-01-31 11:13:57 +0100974
975/* No abort during direct loading */
976#define is_fw_load_aborted(buf) false
977
Ming Lei5b7cb7a2013-06-06 19:52:54 +0800978#ifdef CONFIG_PM_SLEEP
Ming Leiddf1f062013-06-04 10:01:13 +0800979static inline void kill_requests_without_uevent(void) { }
Ming Lei5b7cb7a2013-06-06 19:52:54 +0800980#endif
Ming Leiddf1f062013-06-04 10:01:13 +0800981
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100982#endif /* CONFIG_FW_LOADER_USER_HELPER */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Ming Leif531f05a2012-08-04 12:01:25 +0800984
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100985/* wait until the shared firmware_buf becomes ready (or error) */
986static int sync_cached_firmware_buf(struct firmware_buf *buf)
Ming Lei1f2b7952012-08-04 12:01:21 +0800987{
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100988 int ret = 0;
989
990 mutex_lock(&fw_lock);
991 while (!test_bit(FW_STATUS_DONE, &buf->status)) {
Takashi Iwai807be032013-01-31 11:13:57 +0100992 if (is_fw_load_aborted(buf)) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100993 ret = -ENOENT;
994 break;
995 }
996 mutex_unlock(&fw_lock);
Kweh, Hock Leong000deba2014-11-18 10:56:59 +0800997 ret = wait_for_completion_interruptible(&buf->completion);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100998 mutex_lock(&fw_lock);
999 }
1000 mutex_unlock(&fw_lock);
1001 return ret;
Ming Lei1f2b7952012-08-04 12:01:21 +08001002}
1003
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001004/* prepare firmware and firmware_buf structs;
1005 * return 0 if a firmware is already assigned, 1 if need to load one,
1006 * or a negative error code
1007 */
1008static int
1009_request_firmware_prepare(struct firmware **firmware_p, const char *name,
1010 struct device *device)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001011{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +08001013 struct firmware_buf *buf;
1014 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Jiri Slaby4aed0642005-09-13 01:25:01 -07001016 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -07001018 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
1019 __func__);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001020 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001023 if (fw_get_builtin_firmware(firmware, name)) {
Luis R. Rodriguezed046302015-04-29 16:30:43 -07001024 dev_dbg(device, "using built-in %s\n", name);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001025 return 0; /* assigned */
David Woodhouse5658c762008-05-23 13:52:42 +01001026 }
1027
Ming Lei1f2b7952012-08-04 12:01:21 +08001028 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
Ming Lei1f2b7952012-08-04 12:01:21 +08001029
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001030 /*
1031 * bind with 'buf' now to avoid warning in failure path
1032 * of requesting firmware.
1033 */
1034 firmware->priv = buf;
Ming Lei1f2b7952012-08-04 12:01:21 +08001035
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001036 if (ret > 0) {
1037 ret = sync_cached_firmware_buf(buf);
1038 if (!ret) {
1039 fw_set_page_data(buf, firmware);
1040 return 0; /* assigned */
1041 }
Stephen Boyddddb5542012-03-28 23:30:43 +02001042 }
Ming Lei1f2b7952012-08-04 12:01:21 +08001043
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001044 if (ret < 0)
1045 return ret;
1046 return 1; /* need to load */
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001047}
1048
Ming Leie771d1a2013-05-27 18:30:03 +08001049static int assign_firmware_buf(struct firmware *fw, struct device *device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001050 unsigned int opt_flags)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001051{
1052 struct firmware_buf *buf = fw->priv;
1053
1054 mutex_lock(&fw_lock);
Takashi Iwai807be032013-01-31 11:13:57 +01001055 if (!buf->size || is_fw_load_aborted(buf)) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001056 mutex_unlock(&fw_lock);
1057 return -ENOENT;
1058 }
1059
1060 /*
1061 * add firmware name into devres list so that we can auto cache
1062 * and uncache firmware for device.
1063 *
1064 * device may has been deleted already, but the problem
1065 * should be fixed in devres or driver core.
1066 */
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001067 /* don't cache firmware handled without uevent */
1068 if (device && (opt_flags & FW_OPT_UEVENT))
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001069 fw_add_devm_name(device, buf->fw_id);
1070
1071 /*
1072 * After caching firmware image is started, let it piggyback
1073 * on request firmware.
1074 */
1075 if (buf->fwc->state == FW_LOADER_START_CACHE) {
1076 if (fw_cache_piggyback_on_request(buf->fw_id))
1077 kref_get(&buf->ref);
1078 }
1079
1080 /* pass the pages buffer to driver at the last minute */
1081 fw_set_page_data(buf, fw);
1082 mutex_unlock(&fw_lock);
1083 return 0;
1084}
1085
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001086/* called from request_firmware() and request_firmware_work_func() */
1087static int
1088_request_firmware(const struct firmware **firmware_p, const char *name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001089 struct device *device, unsigned int opt_flags)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001090{
Brian Norris715780a2015-12-09 14:50:28 -08001091 struct firmware *fw = NULL;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001092 long timeout;
1093 int ret;
1094
1095 if (!firmware_p)
1096 return -EINVAL;
1097
Brian Norris715780a2015-12-09 14:50:28 -08001098 if (!name || name[0] == '\0') {
1099 ret = -EINVAL;
1100 goto out;
1101 }
Kees Cook471b0952014-09-18 11:25:37 -07001102
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001103 ret = _request_firmware_prepare(&fw, name, device);
1104 if (ret <= 0) /* error or already assigned */
1105 goto out;
1106
1107 ret = 0;
1108 timeout = firmware_loading_timeout();
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001109 if (opt_flags & FW_OPT_NOWAIT) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001110 timeout = usermodehelper_read_lock_wait(timeout);
1111 if (!timeout) {
1112 dev_dbg(device, "firmware: %s loading timed out\n",
1113 name);
1114 ret = -EBUSY;
1115 goto out;
1116 }
1117 } else {
1118 ret = usermodehelper_read_trylock();
1119 if (WARN_ON(ret)) {
1120 dev_err(device, "firmware: %s will not be loaded\n",
1121 name);
1122 goto out;
1123 }
1124 }
1125
Neil Horman3e358ac2013-09-06 15:36:08 -04001126 ret = fw_get_filesystem_firmware(device, fw->priv);
1127 if (ret) {
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001128 if (!(opt_flags & FW_OPT_NO_WARN))
Takashi Iwaibba3a872013-12-02 15:38:16 +01001129 dev_warn(device,
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001130 "Direct firmware load for %s failed with error %d\n",
1131 name, ret);
1132 if (opt_flags & FW_OPT_USERHELPER) {
Takashi Iwaibba3a872013-12-02 15:38:16 +01001133 dev_warn(device, "Falling back to user helper\n");
1134 ret = fw_load_from_user_helper(fw, name, device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001135 opt_flags, timeout);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001136 }
Neil Horman3e358ac2013-09-06 15:36:08 -04001137 }
Ming Leie771d1a2013-05-27 18:30:03 +08001138
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001139 if (!ret)
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001140 ret = assign_firmware_buf(fw, device, opt_flags);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001141
1142 usermodehelper_read_unlock();
1143
1144 out:
1145 if (ret < 0) {
1146 release_firmware(fw);
1147 fw = NULL;
1148 }
1149
1150 *firmware_p = fw;
1151 return ret;
1152}
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154/**
Kay Sievers312c0042005-11-16 09:00:00 +01001155 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001156 * @firmware_p: pointer to firmware image
1157 * @name: name of firmware file
1158 * @device: device for which firmware is being loaded
1159 *
1160 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001161 * of @name for device @device.
1162 *
1163 * Should be called from user context where sleeping is allowed.
1164 *
Kay Sievers312c0042005-11-16 09:00:00 +01001165 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001166 * should be distinctive enough not to be confused with any other
1167 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +08001168 *
1169 * Caller must hold the reference count of @device.
Ming Lei6a927852012-11-03 17:48:16 +08001170 *
1171 * The function can be called safely inside device's suspend and
1172 * resume callback.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001173 **/
1174int
1175request_firmware(const struct firmware **firmware_p, const char *name,
Andrei Opreaea310032015-03-08 12:41:15 +02001176 struct device *device)
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001177{
Ming Leid6c8aa32013-06-06 20:01:48 +08001178 int ret;
1179
1180 /* Need to pin this module until return */
1181 __module_get(THIS_MODULE);
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001182 ret = _request_firmware(firmware_p, name, device,
1183 FW_OPT_UEVENT | FW_OPT_FALLBACK);
Ming Leid6c8aa32013-06-06 20:01:48 +08001184 module_put(THIS_MODULE);
1185 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001186}
Daniel Mackf4945132013-05-23 22:17:18 +02001187EXPORT_SYMBOL(request_firmware);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001188
Takashi Iwaibba3a872013-12-02 15:38:16 +01001189/**
Borislav Petkov3c1556b2014-12-03 22:46:37 +01001190 * request_firmware_direct: - load firmware directly without usermode helper
Takashi Iwaibba3a872013-12-02 15:38:16 +01001191 * @firmware_p: pointer to firmware image
1192 * @name: name of firmware file
1193 * @device: device for which firmware is being loaded
1194 *
1195 * This function works pretty much like request_firmware(), but this doesn't
1196 * fall back to usermode helper even if the firmware couldn't be loaded
1197 * directly from fs. Hence it's useful for loading optional firmwares, which
1198 * aren't always present, without extra long timeouts of udev.
1199 **/
1200int request_firmware_direct(const struct firmware **firmware_p,
1201 const char *name, struct device *device)
1202{
1203 int ret;
Andrei Opreaea310032015-03-08 12:41:15 +02001204
Takashi Iwaibba3a872013-12-02 15:38:16 +01001205 __module_get(THIS_MODULE);
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001206 ret = _request_firmware(firmware_p, name, device,
1207 FW_OPT_UEVENT | FW_OPT_NO_WARN);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001208 module_put(THIS_MODULE);
1209 return ret;
1210}
1211EXPORT_SYMBOL_GPL(request_firmware_direct);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001212
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001213/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001215 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001217void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
1219 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001220 if (!fw_is_builtin_firmware(fw))
1221 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 kfree(fw);
1223 }
1224}
Daniel Mackf4945132013-05-23 22:17:18 +02001225EXPORT_SYMBOL(release_firmware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227/* Async support */
1228struct firmware_work {
1229 struct work_struct work;
1230 struct module *module;
1231 const char *name;
1232 struct device *device;
1233 void *context;
1234 void (*cont)(const struct firmware *fw, void *context);
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001235 unsigned int opt_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236};
1237
Stephen Boyda36cf842012-03-28 23:31:00 +02001238static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Stephen Boyda36cf842012-03-28 23:31:00 +02001240 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 const struct firmware *fw;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001242
Stephen Boyda36cf842012-03-28 23:31:00 +02001243 fw_work = container_of(work, struct firmware_work, work);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001244
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001245 _request_firmware(&fw, fw_work->name, fw_work->device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001246 fw_work->opt_flags);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001247 fw_work->cont(fw, fw_work->context);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001248 put_device(fw_work->device); /* taken in request_firmware_nowait() */
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 module_put(fw_work->module);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001251 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
1255/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001256 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001257 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001258 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001259 * is non-zero else the firmware copy must be done manually.
1260 * @name: name of firmware file
1261 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001262 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001263 * @context: will be passed over to @cont, and
1264 * @fw may be %NULL if firmware request fails.
1265 * @cont: function will be called asynchronously when the firmware
1266 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001268 * Caller must hold the reference count of @device.
1269 *
Ming Lei6f21a622012-08-04 12:01:24 +08001270 * Asynchronous variant of request_firmware() for user contexts:
1271 * - sleep for as small periods as possible since it may
1272 * increase kernel boot time of built-in device drivers
1273 * requesting firmware in their ->probe() methods, if
1274 * @gfp is GFP_KERNEL.
1275 *
1276 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 **/
1278int
1279request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001280 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001281 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 void (*cont)(const struct firmware *fw, void *context))
1283{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001284 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Andrei Opreaea310032015-03-08 12:41:15 +02001286 fw_work = kzalloc(sizeof(struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 if (!fw_work)
1288 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001289
1290 fw_work->module = module;
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001291 fw_work->name = kstrdup_const(name, gfp);
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001292 if (!fw_work->name) {
1293 kfree(fw_work);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001294 return -ENOMEM;
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001295 }
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001296 fw_work->device = device;
1297 fw_work->context = context;
1298 fw_work->cont = cont;
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001299 fw_work->opt_flags = FW_OPT_NOWAIT | FW_OPT_FALLBACK |
Takashi Iwai5a1379e2014-06-04 17:48:15 +02001300 (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if (!try_module_get(module)) {
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001303 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 kfree(fw_work);
1305 return -EFAULT;
1306 }
1307
Ming Lei0cfc1e12012-08-04 12:01:23 +08001308 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001309 INIT_WORK(&fw_work->work, request_firmware_work_func);
1310 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return 0;
1312}
Daniel Mackf4945132013-05-23 22:17:18 +02001313EXPORT_SYMBOL(request_firmware_nowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Ming Lei90f890812013-06-20 12:30:16 +08001315#ifdef CONFIG_PM_SLEEP
1316static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1317
Ming Lei2887b392012-08-04 12:01:22 +08001318/**
1319 * cache_firmware - cache one firmware image in kernel memory space
1320 * @fw_name: the firmware image name
1321 *
1322 * Cache firmware in kernel memory so that drivers can use it when
1323 * system isn't ready for them to request firmware image from userspace.
1324 * Once it returns successfully, driver can use request_firmware or its
1325 * nowait version to get the cached firmware without any interacting
1326 * with userspace
1327 *
1328 * Return 0 if the firmware image has been cached successfully
1329 * Return !0 otherwise
1330 *
1331 */
Ming Lei93232e42013-06-06 20:01:47 +08001332static int cache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001333{
1334 int ret;
1335 const struct firmware *fw;
1336
1337 pr_debug("%s: %s\n", __func__, fw_name);
1338
1339 ret = request_firmware(&fw, fw_name, NULL);
1340 if (!ret)
1341 kfree(fw);
1342
1343 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1344
1345 return ret;
1346}
1347
Ming Lei6a2c1232013-06-26 09:28:17 +08001348static struct firmware_buf *fw_lookup_buf(const char *fw_name)
1349{
1350 struct firmware_buf *tmp;
1351 struct firmware_cache *fwc = &fw_cache;
1352
1353 spin_lock(&fwc->lock);
1354 tmp = __fw_lookup_buf(fw_name);
1355 spin_unlock(&fwc->lock);
1356
1357 return tmp;
1358}
1359
Ming Lei2887b392012-08-04 12:01:22 +08001360/**
1361 * uncache_firmware - remove one cached firmware image
1362 * @fw_name: the firmware image name
1363 *
1364 * Uncache one firmware image which has been cached successfully
1365 * before.
1366 *
1367 * Return 0 if the firmware cache has been removed successfully
1368 * Return !0 otherwise
1369 *
1370 */
Ming Lei93232e42013-06-06 20:01:47 +08001371static int uncache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001372{
1373 struct firmware_buf *buf;
1374 struct firmware fw;
1375
1376 pr_debug("%s: %s\n", __func__, fw_name);
1377
1378 if (fw_get_builtin_firmware(&fw, fw_name))
1379 return 0;
1380
1381 buf = fw_lookup_buf(fw_name);
1382 if (buf) {
1383 fw_free_buf(buf);
1384 return 0;
1385 }
1386
1387 return -EINVAL;
1388}
1389
Ming Lei37276a52012-08-04 12:01:27 +08001390static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1391{
1392 struct fw_cache_entry *fce;
1393
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001394 fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
Ming Lei37276a52012-08-04 12:01:27 +08001395 if (!fce)
1396 goto exit;
1397
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001398 fce->name = kstrdup_const(name, GFP_ATOMIC);
1399 if (!fce->name) {
1400 kfree(fce);
1401 fce = NULL;
1402 goto exit;
1403 }
Ming Lei37276a52012-08-04 12:01:27 +08001404exit:
1405 return fce;
1406}
1407
Ming Lei373304f2012-10-09 12:01:01 +08001408static int __fw_entry_found(const char *name)
1409{
1410 struct firmware_cache *fwc = &fw_cache;
1411 struct fw_cache_entry *fce;
1412
1413 list_for_each_entry(fce, &fwc->fw_names, list) {
1414 if (!strcmp(fce->name, name))
1415 return 1;
1416 }
1417 return 0;
1418}
1419
Ming Leiac39b3e2012-08-20 19:04:16 +08001420static int fw_cache_piggyback_on_request(const char *name)
1421{
1422 struct firmware_cache *fwc = &fw_cache;
1423 struct fw_cache_entry *fce;
1424 int ret = 0;
1425
1426 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001427 if (__fw_entry_found(name))
1428 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001429
1430 fce = alloc_fw_cache_entry(name);
1431 if (fce) {
1432 ret = 1;
1433 list_add(&fce->list, &fwc->fw_names);
1434 pr_debug("%s: fw: %s\n", __func__, name);
1435 }
1436found:
1437 spin_unlock(&fwc->name_lock);
1438 return ret;
1439}
1440
Ming Lei37276a52012-08-04 12:01:27 +08001441static void free_fw_cache_entry(struct fw_cache_entry *fce)
1442{
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001443 kfree_const(fce->name);
Ming Lei37276a52012-08-04 12:01:27 +08001444 kfree(fce);
1445}
1446
1447static void __async_dev_cache_fw_image(void *fw_entry,
1448 async_cookie_t cookie)
1449{
1450 struct fw_cache_entry *fce = fw_entry;
1451 struct firmware_cache *fwc = &fw_cache;
1452 int ret;
1453
1454 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001455 if (ret) {
1456 spin_lock(&fwc->name_lock);
1457 list_del(&fce->list);
1458 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001459
Ming Leiac39b3e2012-08-20 19:04:16 +08001460 free_fw_cache_entry(fce);
1461 }
Ming Lei37276a52012-08-04 12:01:27 +08001462}
1463
1464/* called with dev->devres_lock held */
1465static void dev_create_fw_entry(struct device *dev, void *res,
1466 void *data)
1467{
1468 struct fw_name_devm *fwn = res;
1469 const char *fw_name = fwn->name;
1470 struct list_head *head = data;
1471 struct fw_cache_entry *fce;
1472
1473 fce = alloc_fw_cache_entry(fw_name);
1474 if (fce)
1475 list_add(&fce->list, head);
1476}
1477
1478static int devm_name_match(struct device *dev, void *res,
1479 void *match_data)
1480{
1481 struct fw_name_devm *fwn = res;
1482 return (fwn->magic == (unsigned long)match_data);
1483}
1484
Ming Leiab6dd8e2012-08-17 22:07:00 +08001485static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001486{
1487 LIST_HEAD(todo);
1488 struct fw_cache_entry *fce;
1489 struct fw_cache_entry *fce_next;
1490 struct firmware_cache *fwc = &fw_cache;
1491
1492 devres_for_each_res(dev, fw_name_devm_release,
1493 devm_name_match, &fw_cache,
1494 dev_create_fw_entry, &todo);
1495
1496 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1497 list_del(&fce->list);
1498
1499 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001500 /* only one cache entry for one firmware */
1501 if (!__fw_entry_found(fce->name)) {
Ming Lei373304f2012-10-09 12:01:01 +08001502 list_add(&fce->list, &fwc->fw_names);
1503 } else {
1504 free_fw_cache_entry(fce);
1505 fce = NULL;
1506 }
Ming Lei37276a52012-08-04 12:01:27 +08001507 spin_unlock(&fwc->name_lock);
1508
Ming Lei373304f2012-10-09 12:01:01 +08001509 if (fce)
Ming Leid28d3882012-10-09 12:01:04 +08001510 async_schedule_domain(__async_dev_cache_fw_image,
1511 (void *)fce,
1512 &fw_cache_domain);
Ming Lei37276a52012-08-04 12:01:27 +08001513 }
1514}
1515
1516static void __device_uncache_fw_images(void)
1517{
1518 struct firmware_cache *fwc = &fw_cache;
1519 struct fw_cache_entry *fce;
1520
1521 spin_lock(&fwc->name_lock);
1522 while (!list_empty(&fwc->fw_names)) {
1523 fce = list_entry(fwc->fw_names.next,
1524 struct fw_cache_entry, list);
1525 list_del(&fce->list);
1526 spin_unlock(&fwc->name_lock);
1527
1528 uncache_firmware(fce->name);
1529 free_fw_cache_entry(fce);
1530
1531 spin_lock(&fwc->name_lock);
1532 }
1533 spin_unlock(&fwc->name_lock);
1534}
1535
1536/**
1537 * device_cache_fw_images - cache devices' firmware
1538 *
1539 * If one device called request_firmware or its nowait version
1540 * successfully before, the firmware names are recored into the
1541 * device's devres link list, so device_cache_fw_images can call
1542 * cache_firmware() to cache these firmwares for the device,
1543 * then the device driver can load its firmwares easily at
1544 * time when system is not ready to complete loading firmware.
1545 */
1546static void device_cache_fw_images(void)
1547{
1548 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001549 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001550 DEFINE_WAIT(wait);
1551
1552 pr_debug("%s\n", __func__);
1553
Ming Lei373304f2012-10-09 12:01:01 +08001554 /* cancel uncache work */
1555 cancel_delayed_work_sync(&fwc->work);
1556
Ming Leiffe53f62012-08-04 12:01:28 +08001557 /*
1558 * use small loading timeout for caching devices' firmware
1559 * because all these firmware images have been loaded
1560 * successfully at lease once, also system is ready for
1561 * completing firmware loading now. The maximum size of
1562 * firmware in current distributions is about 2M bytes,
1563 * so 10 secs should be enough.
1564 */
1565 old_timeout = loading_timeout;
1566 loading_timeout = 10;
1567
Ming Leiac39b3e2012-08-20 19:04:16 +08001568 mutex_lock(&fw_lock);
1569 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001570 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001571 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001572
1573 /* wait for completion of caching firmware for all devices */
Ming Leid28d3882012-10-09 12:01:04 +08001574 async_synchronize_full_domain(&fw_cache_domain);
Ming Leiffe53f62012-08-04 12:01:28 +08001575
1576 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001577}
1578
1579/**
1580 * device_uncache_fw_images - uncache devices' firmware
1581 *
1582 * uncache all firmwares which have been cached successfully
1583 * by device_uncache_fw_images earlier
1584 */
1585static void device_uncache_fw_images(void)
1586{
1587 pr_debug("%s\n", __func__);
1588 __device_uncache_fw_images();
1589}
1590
1591static void device_uncache_fw_images_work(struct work_struct *work)
1592{
1593 device_uncache_fw_images();
1594}
1595
1596/**
1597 * device_uncache_fw_images_delay - uncache devices firmwares
1598 * @delay: number of milliseconds to delay uncache device firmwares
1599 *
1600 * uncache all devices's firmwares which has been cached successfully
1601 * by device_cache_fw_images after @delay milliseconds.
1602 */
1603static void device_uncache_fw_images_delay(unsigned long delay)
1604{
Shaibal Duttabce66182014-01-31 15:44:58 -08001605 queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
1606 msecs_to_jiffies(delay));
Ming Lei37276a52012-08-04 12:01:27 +08001607}
1608
Ming Lei07646d92012-08-04 12:01:29 +08001609static int fw_pm_notify(struct notifier_block *notify_block,
1610 unsigned long mode, void *unused)
1611{
1612 switch (mode) {
1613 case PM_HIBERNATION_PREPARE:
1614 case PM_SUSPEND_PREPARE:
Sebastian Capellaf8d5b9e2014-02-18 17:52:08 -08001615 case PM_RESTORE_PREPARE:
Ming Leiaf5bc112013-05-27 18:30:04 +08001616 kill_requests_without_uevent();
Ming Lei07646d92012-08-04 12:01:29 +08001617 device_cache_fw_images();
1618 break;
1619
1620 case PM_POST_SUSPEND:
1621 case PM_POST_HIBERNATION:
1622 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001623 /*
1624 * In case that system sleep failed and syscore_suspend is
1625 * not called.
1626 */
1627 mutex_lock(&fw_lock);
1628 fw_cache.state = FW_LOADER_NO_CACHE;
1629 mutex_unlock(&fw_lock);
1630
Ming Lei07646d92012-08-04 12:01:29 +08001631 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1632 break;
1633 }
1634
1635 return 0;
1636}
Ming Lei07646d92012-08-04 12:01:29 +08001637
Ming Leiac39b3e2012-08-20 19:04:16 +08001638/* stop caching firmware once syscore_suspend is reached */
1639static int fw_suspend(void)
1640{
1641 fw_cache.state = FW_LOADER_NO_CACHE;
1642 return 0;
1643}
1644
1645static struct syscore_ops fw_syscore_ops = {
1646 .suspend = fw_suspend,
1647};
Ming Leicfe016b2012-09-08 17:32:30 +08001648#else
1649static int fw_cache_piggyback_on_request(const char *name)
1650{
1651 return 0;
1652}
1653#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001654
Ming Lei37276a52012-08-04 12:01:27 +08001655static void __init fw_cache_init(void)
1656{
1657 spin_lock_init(&fw_cache.lock);
1658 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001659 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001660
Ming Leicfe016b2012-09-08 17:32:30 +08001661#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +08001662 spin_lock_init(&fw_cache.name_lock);
1663 INIT_LIST_HEAD(&fw_cache.fw_names);
Ming Lei37276a52012-08-04 12:01:27 +08001664
Ming Lei37276a52012-08-04 12:01:27 +08001665 INIT_DELAYED_WORK(&fw_cache.work,
1666 device_uncache_fw_images_work);
Ming Lei07646d92012-08-04 12:01:29 +08001667
1668 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1669 register_pm_notifier(&fw_cache.pm_notify);
Ming Leiac39b3e2012-08-20 19:04:16 +08001670
1671 register_syscore_ops(&fw_syscore_ops);
Ming Leicfe016b2012-09-08 17:32:30 +08001672#endif
Ming Lei37276a52012-08-04 12:01:27 +08001673}
1674
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001675static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
Ming Lei1f2b7952012-08-04 12:01:21 +08001677 fw_cache_init();
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001678#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaife304142013-05-22 18:28:38 +02001679 register_reboot_notifier(&fw_shutdown_nb);
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001680 return class_register(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001681#else
1682 return 0;
1683#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001685
1686static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
Ming Leicfe016b2012-09-08 17:32:30 +08001688#ifdef CONFIG_PM_SLEEP
Ming Leiac39b3e2012-08-20 19:04:16 +08001689 unregister_syscore_ops(&fw_syscore_ops);
Ming Lei07646d92012-08-04 12:01:29 +08001690 unregister_pm_notifier(&fw_cache.pm_notify);
Ming Leicfe016b2012-09-08 17:32:30 +08001691#endif
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001692#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaife304142013-05-22 18:28:38 +02001693 unregister_reboot_notifier(&fw_shutdown_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 class_unregister(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001695#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
1697
Shaohua Lia30a6a22006-09-27 01:50:52 -07001698fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699module_exit(firmware_class_exit);