blob: a414008ea64c73280278201c6bd7bff016b16c60 [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]);
261 kfree(buf->pages);
262 } 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. Rodriguezed046302015-04-29 16:30:43 -0700332 dev_warn(device, "loading %s failed with error %d\n",
333 path, rc);
Kees Cook4b2530d2016-02-04 13:15:02 -0800334 continue;
335 }
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500336 dev_dbg(device, "direct-loading %s\n", buf->fw_id);
337 buf->size = size;
Luis R. Rodriguez5275d192015-07-30 15:48:57 -0700338 fw_finish_direct_load(device, buf);
Kees Cook4b2530d2016-02-04 13:15:02 -0800339 break;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100340 }
Kees Cook4b2530d2016-02-04 13:15:02 -0800341 __putname(path);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100342
Neil Horman3e358ac2013-09-06 15:36:08 -0400343 return rc;
Ming Lei746058f2012-10-09 12:01:03 +0800344}
345
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100346/* firmware holds the ownership of pages */
347static void firmware_free_data(const struct firmware *fw)
348{
349 /* Loaded directly? */
350 if (!fw->priv) {
351 vfree(fw->data);
352 return;
353 }
354 fw_free_buf(fw->priv);
355}
356
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100357/* store the pages buffer info firmware from buf */
358static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
359{
360 fw->priv = buf;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100361#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100362 fw->pages = buf->pages;
363#endif
364 fw->size = buf->size;
365 fw->data = buf->data;
366
367 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
368 __func__, buf->fw_id, buf, buf->data,
369 (unsigned int)buf->size);
370}
371
372#ifdef CONFIG_PM_SLEEP
373static void fw_name_devm_release(struct device *dev, void *res)
374{
375 struct fw_name_devm *fwn = res;
376
377 if (fwn->magic == (unsigned long)&fw_cache)
378 pr_debug("%s: fw_name-%s devm-%p released\n",
379 __func__, fwn->name, res);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700380 kfree_const(fwn->name);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100381}
382
383static int fw_devm_match(struct device *dev, void *res,
384 void *match_data)
385{
386 struct fw_name_devm *fwn = res;
387
388 return (fwn->magic == (unsigned long)&fw_cache) &&
389 !strcmp(fwn->name, match_data);
390}
391
392static struct fw_name_devm *fw_find_devm_name(struct device *dev,
393 const char *name)
394{
395 struct fw_name_devm *fwn;
396
397 fwn = devres_find(dev, fw_name_devm_release,
398 fw_devm_match, (void *)name);
399 return fwn;
400}
401
402/* add firmware name into devres list */
403static int fw_add_devm_name(struct device *dev, const char *name)
404{
405 struct fw_name_devm *fwn;
406
407 fwn = fw_find_devm_name(dev, name);
408 if (fwn)
409 return 1;
410
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700411 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
412 GFP_KERNEL);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100413 if (!fwn)
414 return -ENOMEM;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700415 fwn->name = kstrdup_const(name, GFP_KERNEL);
416 if (!fwn->name) {
Vladimir Zapolskiya885de62015-07-29 23:26:28 +0300417 devres_free(fwn);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700418 return -ENOMEM;
419 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100420
421 fwn->magic = (unsigned long)&fw_cache;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100422 devres_add(dev, fwn);
423
424 return 0;
425}
426#else
427static int fw_add_devm_name(struct device *dev, const char *name)
428{
429 return 0;
430}
431#endif
432
433
434/*
435 * user-mode helper code
436 */
437#ifdef CONFIG_FW_LOADER_USER_HELPER
438struct firmware_priv {
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100439 bool nowait;
440 struct device dev;
441 struct firmware_buf *buf;
442 struct firmware *fw;
443};
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100444
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700445static struct firmware_priv *to_firmware_priv(struct device *dev)
446{
447 return container_of(dev, struct firmware_priv, dev);
448}
449
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700450static void __fw_load_abort(struct firmware_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Ming Lei87597932013-06-15 16:36:38 +0800452 /*
453 * There is a small window in which user can write to 'loading'
454 * between loading done and disappearance of 'loading'
455 */
456 if (test_bit(FW_STATUS_DONE, &buf->status))
457 return;
458
Takashi Iwaife304142013-05-22 18:28:38 +0200459 list_del_init(&buf->pending_list);
Ming Lei12446912012-08-04 12:01:20 +0800460 set_bit(FW_STATUS_ABORT, &buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800461 complete_all(&buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700464static void fw_load_abort(struct firmware_priv *fw_priv)
465{
466 struct firmware_buf *buf = fw_priv->buf;
467
468 __fw_load_abort(buf);
Ming Lei87597932013-06-15 16:36:38 +0800469
470 /* avoid user action after loading abort */
471 fw_priv->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
Takashi Iwai807be032013-01-31 11:13:57 +0100474#define is_fw_load_aborted(buf) \
475 test_bit(FW_STATUS_ABORT, &(buf)->status)
476
Takashi Iwaife304142013-05-22 18:28:38 +0200477static LIST_HEAD(pending_fw_head);
478
479/* reboot notifier for avoid deadlock with usermode_lock */
480static int fw_shutdown_notify(struct notifier_block *unused1,
481 unsigned long unused2, void *unused3)
482{
483 mutex_lock(&fw_lock);
484 while (!list_empty(&pending_fw_head))
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700485 __fw_load_abort(list_first_entry(&pending_fw_head,
Takashi Iwaife304142013-05-22 18:28:38 +0200486 struct firmware_buf,
487 pending_list));
488 mutex_unlock(&fw_lock);
489 return NOTIFY_DONE;
490}
491
492static struct notifier_block fw_shutdown_nb = {
493 .notifier_call = fw_shutdown_notify,
494};
495
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700496static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
497 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 return sprintf(buf, "%d\n", loading_timeout);
500}
501
502/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800503 * firmware_timeout_store - set number of seconds to wait for firmware
504 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800505 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800506 * @buf: buffer to scan for timeout value
507 * @count: number of bytes in @buf
508 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800510 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 * firmware will be provided.
512 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800513 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 **/
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700515static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
516 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700519 if (loading_timeout < 0)
520 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return count;
523}
524
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800525static struct class_attribute firmware_class_attrs[] = {
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700526 __ATTR_RW(timeout),
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800527 __ATTR_NULL
528};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800530static void fw_dev_release(struct device *dev)
531{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700532 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800533
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800534 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800535}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Linus Torvalds6f957722015-07-09 11:20:01 -0700537static int do_firmware_uevent(struct firmware_priv *fw_priv, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Ming Lei12446912012-08-04 12:01:20 +0800539 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200541 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700542 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200543 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
544 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 return 0;
547}
548
Linus Torvalds6f957722015-07-09 11:20:01 -0700549static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
550{
551 struct firmware_priv *fw_priv = to_firmware_priv(dev);
552 int err = 0;
553
554 mutex_lock(&fw_lock);
555 if (fw_priv->buf)
556 err = do_firmware_uevent(fw_priv, env);
557 mutex_unlock(&fw_lock);
558 return err;
559}
560
Adrian Bunk1b81d662006-05-20 15:00:16 -0700561static struct class firmware_class = {
562 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800563 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700564 .dev_uevent = firmware_uevent,
565 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700566};
567
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700568static ssize_t firmware_loading_show(struct device *dev,
569 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700571 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800572 int loading = 0;
573
574 mutex_lock(&fw_lock);
575 if (fw_priv->buf)
576 loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
577 mutex_unlock(&fw_lock);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return sprintf(buf, "%d\n", loading);
580}
581
David Woodhouse6e03a202009-04-09 22:04:07 -0700582/* Some architectures don't have PAGE_KERNEL_RO */
583#ifndef PAGE_KERNEL_RO
584#define PAGE_KERNEL_RO PAGE_KERNEL
585#endif
Ming Lei253c9242012-10-09 12:01:02 +0800586
587/* one pages buffer should be mapped/unmapped only once */
588static int fw_map_pages_buf(struct firmware_buf *buf)
589{
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100590 if (!buf->is_paged_buf)
Ming Lei746058f2012-10-09 12:01:03 +0800591 return 0;
592
Markus Elfringdaa3d672014-11-19 11:38:38 +0100593 vunmap(buf->data);
Ming Lei253c9242012-10-09 12:01:02 +0800594 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
595 if (!buf->data)
596 return -ENOMEM;
597 return 0;
598}
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800601 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700602 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800603 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800604 * @buf: buffer to scan for loading control value
605 * @count: number of bytes in @buf
606 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 * The relevant values are:
608 *
609 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800610 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 * -1: Conclude the load with an error and discard any written data.
612 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700613static ssize_t firmware_loading_store(struct device *dev,
614 struct device_attribute *attr,
615 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700617 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800618 struct firmware_buf *fw_buf;
Kees Cook6593d922014-02-25 13:06:00 -0800619 ssize_t written = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700621 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Neil Hormaneea915b2012-01-02 15:31:23 -0500623 mutex_lock(&fw_lock);
Ming Lei87597932013-06-15 16:36:38 +0800624 fw_buf = fw_priv->buf;
Ming Lei12446912012-08-04 12:01:20 +0800625 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500626 goto out;
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 switch (loading) {
629 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800630 /* discarding any previous partial load */
Ming Lei12446912012-08-04 12:01:20 +0800631 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
632 for (i = 0; i < fw_buf->nr_pages; i++)
633 __free_page(fw_buf->pages[i]);
634 kfree(fw_buf->pages);
635 fw_buf->pages = NULL;
636 fw_buf->page_array_size = 0;
637 fw_buf->nr_pages = 0;
638 set_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei28eefa72012-08-04 12:01:17 +0800639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 break;
641 case 0:
Ming Lei12446912012-08-04 12:01:20 +0800642 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
Kees Cook6593d922014-02-25 13:06:00 -0800643 int rc;
644
Ming Lei12446912012-08-04 12:01:20 +0800645 set_bit(FW_STATUS_DONE, &fw_buf->status);
646 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei253c9242012-10-09 12:01:02 +0800647
648 /*
649 * Several loading requests may be pending on
650 * one same firmware buf, so let all requests
651 * see the mapped 'buf->data' once the loading
652 * is completed.
653 * */
Kees Cook6593d922014-02-25 13:06:00 -0800654 rc = fw_map_pages_buf(fw_buf);
655 if (rc)
zhang jun2b1278c2014-02-13 15:18:47 +0800656 dev_err(dev, "%s: map pages failed\n",
657 __func__);
Kees Cook6593d922014-02-25 13:06:00 -0800658 else
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500659 rc = security_kernel_post_read_file(NULL,
660 fw_buf->data, fw_buf->size,
661 READING_FIRMWARE);
Kees Cook6593d922014-02-25 13:06:00 -0800662
663 /*
664 * Same logic as fw_load_abort, only the DONE bit
665 * is ignored and we set ABORT only on failure.
666 */
Takashi Iwaife304142013-05-22 18:28:38 +0200667 list_del_init(&fw_buf->pending_list);
Kees Cook6593d922014-02-25 13:06:00 -0800668 if (rc) {
669 set_bit(FW_STATUS_ABORT, &fw_buf->status);
670 written = rc;
671 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800672 complete_all(&fw_buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 break;
674 }
675 /* fallthrough */
676 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700677 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 /* fallthrough */
679 case -1:
680 fw_load_abort(fw_priv);
681 break;
682 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500683out:
684 mutex_unlock(&fw_lock);
Kees Cook6593d922014-02-25 13:06:00 -0800685 return written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700688static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700690static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
691 struct bin_attribute *bin_attr,
692 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200694 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700695 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800696 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700697 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Laura Garciacad1e552006-05-23 23:22:38 +0200699 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800700 buf = fw_priv->buf;
701 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 ret_count = -ENODEV;
703 goto out;
704 }
Ming Lei12446912012-08-04 12:01:20 +0800705 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200706 ret_count = 0;
707 goto out;
708 }
Ming Lei12446912012-08-04 12:01:20 +0800709 if (count > buf->size - offset)
710 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700711
712 ret_count = count;
713
714 while (count) {
715 void *page_data;
716 int page_nr = offset >> PAGE_SHIFT;
717 int page_ofs = offset & (PAGE_SIZE-1);
718 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
719
Ming Lei12446912012-08-04 12:01:20 +0800720 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700721
722 memcpy(buffer, page_data + page_ofs, page_cnt);
723
Ming Lei12446912012-08-04 12:01:20 +0800724 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700725 buffer += page_cnt;
726 offset += page_cnt;
727 count -= page_cnt;
728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729out:
Laura Garciacad1e552006-05-23 23:22:38 +0200730 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return ret_count;
732}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800733
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700734static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Ming Lei12446912012-08-04 12:01:20 +0800736 struct firmware_buf *buf = fw_priv->buf;
Fabian Fredericka76040d2014-07-01 21:13:30 +0200737 int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
David Woodhouse6e03a202009-04-09 22:04:07 -0700739 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800740 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700741 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800742 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700743 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
David Woodhouse6e03a202009-04-09 22:04:07 -0700745 new_pages = kmalloc(new_array_size * sizeof(void *),
746 GFP_KERNEL);
747 if (!new_pages) {
748 fw_load_abort(fw_priv);
749 return -ENOMEM;
750 }
Ming Lei12446912012-08-04 12:01:20 +0800751 memcpy(new_pages, buf->pages,
752 buf->page_array_size * sizeof(void *));
753 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
754 (new_array_size - buf->page_array_size));
755 kfree(buf->pages);
756 buf->pages = new_pages;
757 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700759
Ming Lei12446912012-08-04 12:01:20 +0800760 while (buf->nr_pages < pages_needed) {
761 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700762 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
763
Ming Lei12446912012-08-04 12:01:20 +0800764 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700765 fw_load_abort(fw_priv);
766 return -ENOMEM;
767 }
Ming Lei12446912012-08-04 12:01:20 +0800768 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return 0;
771}
772
773/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800774 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700775 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700776 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700777 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800778 * @buffer: buffer being written
779 * @offset: buffer offset for write in total data store area
780 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800782 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 * the driver as a firmware image.
784 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700785static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
786 struct bin_attribute *bin_attr,
787 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200789 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700790 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800791 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 ssize_t retval;
793
794 if (!capable(CAP_SYS_RAWIO))
795 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700796
Laura Garciacad1e552006-05-23 23:22:38 +0200797 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800798 buf = fw_priv->buf;
799 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 retval = -ENODEV;
801 goto out;
802 }
Ming Lei65710cb2012-08-04 12:01:16 +0800803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 retval = fw_realloc_buffer(fw_priv, offset + count);
805 if (retval)
806 goto out;
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700809
810 while (count) {
811 void *page_data;
812 int page_nr = offset >> PAGE_SHIFT;
813 int page_ofs = offset & (PAGE_SIZE - 1);
814 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
815
Ming Lei12446912012-08-04 12:01:20 +0800816 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700817
818 memcpy(page_data + page_ofs, buffer, page_cnt);
819
Ming Lei12446912012-08-04 12:01:20 +0800820 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700821 buffer += page_cnt;
822 offset += page_cnt;
823 count -= page_cnt;
824 }
825
Ming Lei12446912012-08-04 12:01:20 +0800826 buf->size = max_t(size_t, offset, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827out:
Laura Garciacad1e552006-05-23 23:22:38 +0200828 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return retval;
830}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800831
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700832static struct bin_attribute firmware_attr_data = {
833 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 .size = 0,
835 .read = firmware_data_read,
836 .write = firmware_data_write,
837};
838
Takashi Iwai46239902015-02-04 15:18:07 +0100839static struct attribute *fw_dev_attrs[] = {
840 &dev_attr_loading.attr,
841 NULL
842};
843
844static struct bin_attribute *fw_dev_bin_attrs[] = {
845 &firmware_attr_data,
846 NULL
847};
848
849static const struct attribute_group fw_dev_attr_group = {
850 .attrs = fw_dev_attrs,
851 .bin_attrs = fw_dev_bin_attrs,
852};
853
854static const struct attribute_group *fw_dev_attr_groups[] = {
855 &fw_dev_attr_group,
856 NULL
857};
858
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700859static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200860fw_create_instance(struct firmware *firmware, const char *fw_name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100861 struct device *device, unsigned int opt_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700863 struct firmware_priv *fw_priv;
864 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Ming Lei12446912012-08-04 12:01:20 +0800866 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700867 if (!fw_priv) {
Ming Lei12446912012-08-04 12:01:20 +0800868 fw_priv = ERR_PTR(-ENOMEM);
869 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100872 fw_priv->nowait = !!(opt_flags & FW_OPT_NOWAIT);
Ming Lei1f2b7952012-08-04 12:01:21 +0800873 fw_priv->fw = firmware;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700874 f_dev = &fw_priv->dev;
875
876 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800877 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700878 f_dev->parent = device;
879 f_dev->class = &firmware_class;
Takashi Iwai46239902015-02-04 15:18:07 +0100880 f_dev->groups = fw_dev_attr_groups;
Ming Lei12446912012-08-04 12:01:20 +0800881exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700882 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100884
885/* load a firmware via user helper */
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100886static int _request_firmware_load(struct firmware_priv *fw_priv,
887 unsigned int opt_flags, long timeout)
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100888{
889 int retval = 0;
890 struct device *f_dev = &fw_priv->dev;
891 struct firmware_buf *buf = fw_priv->buf;
892
893 /* fall back on userspace loading */
894 buf->is_paged_buf = true;
895
896 dev_set_uevent_suppress(f_dev, true);
897
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100898 retval = device_add(f_dev);
899 if (retval) {
900 dev_err(f_dev, "%s: device_register failed\n", __func__);
901 goto err_put_dev;
902 }
903
Maxime Bizon1eeeef12013-08-29 20:28:13 +0200904 mutex_lock(&fw_lock);
905 list_add(&buf->pending_list, &pending_fw_head);
906 mutex_unlock(&fw_lock);
907
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100908 if (opt_flags & FW_OPT_UEVENT) {
Ming Leiaf5bc112013-05-27 18:30:04 +0800909 buf->need_uevent = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100910 dev_set_uevent_suppress(f_dev, false);
911 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100912 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
Ming Lei68ff2a02015-01-13 00:02:01 +0800913 } else {
914 timeout = MAX_JIFFY_OFFSET;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100915 }
916
Ming Lei68ff2a02015-01-13 00:02:01 +0800917 retval = wait_for_completion_interruptible_timeout(&buf->completion,
918 timeout);
919 if (retval == -ERESTARTSYS || !retval) {
Ming Lei0cb64242015-01-13 00:01:35 +0800920 mutex_lock(&fw_lock);
921 fw_load_abort(fw_priv);
922 mutex_unlock(&fw_lock);
Zahari Doychevef518cc2015-03-10 10:45:40 +0100923 } else if (retval > 0) {
924 retval = 0;
Ming Lei0cb64242015-01-13 00:01:35 +0800925 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100926
Shuah Khan0542ad82014-07-22 18:10:38 -0600927 if (is_fw_load_aborted(buf))
928 retval = -EAGAIN;
929 else if (!buf->data)
zhang jun2b1278c2014-02-13 15:18:47 +0800930 retval = -ENOMEM;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100931
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100932 device_del(f_dev);
933err_put_dev:
934 put_device(f_dev);
935 return retval;
936}
937
938static int fw_load_from_user_helper(struct firmware *firmware,
939 const char *name, struct device *device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100940 unsigned int opt_flags, long timeout)
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100941{
942 struct firmware_priv *fw_priv;
943
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100944 fw_priv = fw_create_instance(firmware, name, device, opt_flags);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100945 if (IS_ERR(fw_priv))
946 return PTR_ERR(fw_priv);
947
948 fw_priv->buf = firmware->priv;
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100949 return _request_firmware_load(fw_priv, opt_flags, timeout);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100950}
Ming Leiddf1f062013-06-04 10:01:13 +0800951
Ming Lei5b7cb7a2013-06-06 19:52:54 +0800952#ifdef CONFIG_PM_SLEEP
Ming Leiddf1f062013-06-04 10:01:13 +0800953/* kill pending requests without uevent to avoid blocking suspend */
954static void kill_requests_without_uevent(void)
955{
956 struct firmware_buf *buf;
957 struct firmware_buf *next;
958
959 mutex_lock(&fw_lock);
960 list_for_each_entry_safe(buf, next, &pending_fw_head, pending_list) {
961 if (!buf->need_uevent)
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700962 __fw_load_abort(buf);
Ming Leiddf1f062013-06-04 10:01:13 +0800963 }
964 mutex_unlock(&fw_lock);
965}
Ming Lei5b7cb7a2013-06-06 19:52:54 +0800966#endif
Ming Leiddf1f062013-06-04 10:01:13 +0800967
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100968#else /* CONFIG_FW_LOADER_USER_HELPER */
969static inline int
970fw_load_from_user_helper(struct firmware *firmware, const char *name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100971 struct device *device, unsigned int opt_flags,
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100972 long timeout)
973{
974 return -ENOENT;
975}
Takashi Iwai807be032013-01-31 11:13:57 +0100976
977/* No abort during direct loading */
978#define is_fw_load_aborted(buf) false
979
Ming Lei5b7cb7a2013-06-06 19:52:54 +0800980#ifdef CONFIG_PM_SLEEP
Ming Leiddf1f062013-06-04 10:01:13 +0800981static inline void kill_requests_without_uevent(void) { }
Ming Lei5b7cb7a2013-06-06 19:52:54 +0800982#endif
Ming Leiddf1f062013-06-04 10:01:13 +0800983
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100984#endif /* CONFIG_FW_LOADER_USER_HELPER */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Ming Leif531f05a2012-08-04 12:01:25 +0800986
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100987/* wait until the shared firmware_buf becomes ready (or error) */
988static int sync_cached_firmware_buf(struct firmware_buf *buf)
Ming Lei1f2b7952012-08-04 12:01:21 +0800989{
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100990 int ret = 0;
991
992 mutex_lock(&fw_lock);
993 while (!test_bit(FW_STATUS_DONE, &buf->status)) {
Takashi Iwai807be032013-01-31 11:13:57 +0100994 if (is_fw_load_aborted(buf)) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100995 ret = -ENOENT;
996 break;
997 }
998 mutex_unlock(&fw_lock);
Kweh, Hock Leong000deba2014-11-18 10:56:59 +0800999 ret = wait_for_completion_interruptible(&buf->completion);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001000 mutex_lock(&fw_lock);
1001 }
1002 mutex_unlock(&fw_lock);
1003 return ret;
Ming Lei1f2b7952012-08-04 12:01:21 +08001004}
1005
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001006/* prepare firmware and firmware_buf structs;
1007 * return 0 if a firmware is already assigned, 1 if need to load one,
1008 * or a negative error code
1009 */
1010static int
1011_request_firmware_prepare(struct firmware **firmware_p, const char *name,
1012 struct device *device)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001013{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +08001015 struct firmware_buf *buf;
1016 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Jiri Slaby4aed0642005-09-13 01:25:01 -07001018 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -07001020 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
1021 __func__);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001022 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001025 if (fw_get_builtin_firmware(firmware, name)) {
Luis R. Rodriguezed046302015-04-29 16:30:43 -07001026 dev_dbg(device, "using built-in %s\n", name);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001027 return 0; /* assigned */
David Woodhouse5658c762008-05-23 13:52:42 +01001028 }
1029
Ming Lei1f2b7952012-08-04 12:01:21 +08001030 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
Ming Lei1f2b7952012-08-04 12:01:21 +08001031
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001032 /*
1033 * bind with 'buf' now to avoid warning in failure path
1034 * of requesting firmware.
1035 */
1036 firmware->priv = buf;
Ming Lei1f2b7952012-08-04 12:01:21 +08001037
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001038 if (ret > 0) {
1039 ret = sync_cached_firmware_buf(buf);
1040 if (!ret) {
1041 fw_set_page_data(buf, firmware);
1042 return 0; /* assigned */
1043 }
Stephen Boyddddb5542012-03-28 23:30:43 +02001044 }
Ming Lei1f2b7952012-08-04 12:01:21 +08001045
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001046 if (ret < 0)
1047 return ret;
1048 return 1; /* need to load */
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001049}
1050
Ming Leie771d1a2013-05-27 18:30:03 +08001051static int assign_firmware_buf(struct firmware *fw, struct device *device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001052 unsigned int opt_flags)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001053{
1054 struct firmware_buf *buf = fw->priv;
1055
1056 mutex_lock(&fw_lock);
Takashi Iwai807be032013-01-31 11:13:57 +01001057 if (!buf->size || is_fw_load_aborted(buf)) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001058 mutex_unlock(&fw_lock);
1059 return -ENOENT;
1060 }
1061
1062 /*
1063 * add firmware name into devres list so that we can auto cache
1064 * and uncache firmware for device.
1065 *
1066 * device may has been deleted already, but the problem
1067 * should be fixed in devres or driver core.
1068 */
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001069 /* don't cache firmware handled without uevent */
1070 if (device && (opt_flags & FW_OPT_UEVENT))
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001071 fw_add_devm_name(device, buf->fw_id);
1072
1073 /*
1074 * After caching firmware image is started, let it piggyback
1075 * on request firmware.
1076 */
1077 if (buf->fwc->state == FW_LOADER_START_CACHE) {
1078 if (fw_cache_piggyback_on_request(buf->fw_id))
1079 kref_get(&buf->ref);
1080 }
1081
1082 /* pass the pages buffer to driver at the last minute */
1083 fw_set_page_data(buf, fw);
1084 mutex_unlock(&fw_lock);
1085 return 0;
1086}
1087
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001088/* called from request_firmware() and request_firmware_work_func() */
1089static int
1090_request_firmware(const struct firmware **firmware_p, const char *name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001091 struct device *device, unsigned int opt_flags)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001092{
Brian Norris715780a2015-12-09 14:50:28 -08001093 struct firmware *fw = NULL;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001094 long timeout;
1095 int ret;
1096
1097 if (!firmware_p)
1098 return -EINVAL;
1099
Brian Norris715780a2015-12-09 14:50:28 -08001100 if (!name || name[0] == '\0') {
1101 ret = -EINVAL;
1102 goto out;
1103 }
Kees Cook471b0952014-09-18 11:25:37 -07001104
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001105 ret = _request_firmware_prepare(&fw, name, device);
1106 if (ret <= 0) /* error or already assigned */
1107 goto out;
1108
1109 ret = 0;
1110 timeout = firmware_loading_timeout();
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001111 if (opt_flags & FW_OPT_NOWAIT) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001112 timeout = usermodehelper_read_lock_wait(timeout);
1113 if (!timeout) {
1114 dev_dbg(device, "firmware: %s loading timed out\n",
1115 name);
1116 ret = -EBUSY;
1117 goto out;
1118 }
1119 } else {
1120 ret = usermodehelper_read_trylock();
1121 if (WARN_ON(ret)) {
1122 dev_err(device, "firmware: %s will not be loaded\n",
1123 name);
1124 goto out;
1125 }
1126 }
1127
Neil Horman3e358ac2013-09-06 15:36:08 -04001128 ret = fw_get_filesystem_firmware(device, fw->priv);
1129 if (ret) {
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001130 if (!(opt_flags & FW_OPT_NO_WARN))
Takashi Iwaibba3a872013-12-02 15:38:16 +01001131 dev_warn(device,
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001132 "Direct firmware load for %s failed with error %d\n",
1133 name, ret);
1134 if (opt_flags & FW_OPT_USERHELPER) {
Takashi Iwaibba3a872013-12-02 15:38:16 +01001135 dev_warn(device, "Falling back to user helper\n");
1136 ret = fw_load_from_user_helper(fw, name, device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001137 opt_flags, timeout);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001138 }
Neil Horman3e358ac2013-09-06 15:36:08 -04001139 }
Ming Leie771d1a2013-05-27 18:30:03 +08001140
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001141 if (!ret)
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001142 ret = assign_firmware_buf(fw, device, opt_flags);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001143
1144 usermodehelper_read_unlock();
1145
1146 out:
1147 if (ret < 0) {
1148 release_firmware(fw);
1149 fw = NULL;
1150 }
1151
1152 *firmware_p = fw;
1153 return ret;
1154}
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156/**
Kay Sievers312c0042005-11-16 09:00:00 +01001157 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001158 * @firmware_p: pointer to firmware image
1159 * @name: name of firmware file
1160 * @device: device for which firmware is being loaded
1161 *
1162 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001163 * of @name for device @device.
1164 *
1165 * Should be called from user context where sleeping is allowed.
1166 *
Kay Sievers312c0042005-11-16 09:00:00 +01001167 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001168 * should be distinctive enough not to be confused with any other
1169 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +08001170 *
1171 * Caller must hold the reference count of @device.
Ming Lei6a927852012-11-03 17:48:16 +08001172 *
1173 * The function can be called safely inside device's suspend and
1174 * resume callback.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001175 **/
1176int
1177request_firmware(const struct firmware **firmware_p, const char *name,
Andrei Opreaea310032015-03-08 12:41:15 +02001178 struct device *device)
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001179{
Ming Leid6c8aa32013-06-06 20:01:48 +08001180 int ret;
1181
1182 /* Need to pin this module until return */
1183 __module_get(THIS_MODULE);
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001184 ret = _request_firmware(firmware_p, name, device,
1185 FW_OPT_UEVENT | FW_OPT_FALLBACK);
Ming Leid6c8aa32013-06-06 20:01:48 +08001186 module_put(THIS_MODULE);
1187 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001188}
Daniel Mackf4945132013-05-23 22:17:18 +02001189EXPORT_SYMBOL(request_firmware);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001190
Takashi Iwaibba3a872013-12-02 15:38:16 +01001191/**
Borislav Petkov3c1556b2014-12-03 22:46:37 +01001192 * request_firmware_direct: - load firmware directly without usermode helper
Takashi Iwaibba3a872013-12-02 15:38:16 +01001193 * @firmware_p: pointer to firmware image
1194 * @name: name of firmware file
1195 * @device: device for which firmware is being loaded
1196 *
1197 * This function works pretty much like request_firmware(), but this doesn't
1198 * fall back to usermode helper even if the firmware couldn't be loaded
1199 * directly from fs. Hence it's useful for loading optional firmwares, which
1200 * aren't always present, without extra long timeouts of udev.
1201 **/
1202int request_firmware_direct(const struct firmware **firmware_p,
1203 const char *name, struct device *device)
1204{
1205 int ret;
Andrei Opreaea310032015-03-08 12:41:15 +02001206
Takashi Iwaibba3a872013-12-02 15:38:16 +01001207 __module_get(THIS_MODULE);
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001208 ret = _request_firmware(firmware_p, name, device,
1209 FW_OPT_UEVENT | FW_OPT_NO_WARN);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001210 module_put(THIS_MODULE);
1211 return ret;
1212}
1213EXPORT_SYMBOL_GPL(request_firmware_direct);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001214
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001215/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001217 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001219void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
1221 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001222 if (!fw_is_builtin_firmware(fw))
1223 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 kfree(fw);
1225 }
1226}
Daniel Mackf4945132013-05-23 22:17:18 +02001227EXPORT_SYMBOL(release_firmware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229/* Async support */
1230struct firmware_work {
1231 struct work_struct work;
1232 struct module *module;
1233 const char *name;
1234 struct device *device;
1235 void *context;
1236 void (*cont)(const struct firmware *fw, void *context);
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001237 unsigned int opt_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238};
1239
Stephen Boyda36cf842012-03-28 23:31:00 +02001240static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Stephen Boyda36cf842012-03-28 23:31:00 +02001242 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 const struct firmware *fw;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001244
Stephen Boyda36cf842012-03-28 23:31:00 +02001245 fw_work = container_of(work, struct firmware_work, work);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001246
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001247 _request_firmware(&fw, fw_work->name, fw_work->device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001248 fw_work->opt_flags);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001249 fw_work->cont(fw, fw_work->context);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001250 put_device(fw_work->device); /* taken in request_firmware_nowait() */
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 module_put(fw_work->module);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001253 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
1256
1257/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001258 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001259 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001260 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001261 * is non-zero else the firmware copy must be done manually.
1262 * @name: name of firmware file
1263 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001264 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001265 * @context: will be passed over to @cont, and
1266 * @fw may be %NULL if firmware request fails.
1267 * @cont: function will be called asynchronously when the firmware
1268 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001270 * Caller must hold the reference count of @device.
1271 *
Ming Lei6f21a622012-08-04 12:01:24 +08001272 * Asynchronous variant of request_firmware() for user contexts:
1273 * - sleep for as small periods as possible since it may
1274 * increase kernel boot time of built-in device drivers
1275 * requesting firmware in their ->probe() methods, if
1276 * @gfp is GFP_KERNEL.
1277 *
1278 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 **/
1280int
1281request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001282 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001283 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 void (*cont)(const struct firmware *fw, void *context))
1285{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001286 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Andrei Opreaea310032015-03-08 12:41:15 +02001288 fw_work = kzalloc(sizeof(struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (!fw_work)
1290 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001291
1292 fw_work->module = module;
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001293 fw_work->name = kstrdup_const(name, gfp);
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001294 if (!fw_work->name) {
1295 kfree(fw_work);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001296 return -ENOMEM;
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001297 }
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001298 fw_work->device = device;
1299 fw_work->context = context;
1300 fw_work->cont = cont;
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001301 fw_work->opt_flags = FW_OPT_NOWAIT | FW_OPT_FALLBACK |
Takashi Iwai5a1379e2014-06-04 17:48:15 +02001302 (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if (!try_module_get(module)) {
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001305 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 kfree(fw_work);
1307 return -EFAULT;
1308 }
1309
Ming Lei0cfc1e12012-08-04 12:01:23 +08001310 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001311 INIT_WORK(&fw_work->work, request_firmware_work_func);
1312 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 return 0;
1314}
Daniel Mackf4945132013-05-23 22:17:18 +02001315EXPORT_SYMBOL(request_firmware_nowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Ming Lei90f890812013-06-20 12:30:16 +08001317#ifdef CONFIG_PM_SLEEP
1318static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1319
Ming Lei2887b392012-08-04 12:01:22 +08001320/**
1321 * cache_firmware - cache one firmware image in kernel memory space
1322 * @fw_name: the firmware image name
1323 *
1324 * Cache firmware in kernel memory so that drivers can use it when
1325 * system isn't ready for them to request firmware image from userspace.
1326 * Once it returns successfully, driver can use request_firmware or its
1327 * nowait version to get the cached firmware without any interacting
1328 * with userspace
1329 *
1330 * Return 0 if the firmware image has been cached successfully
1331 * Return !0 otherwise
1332 *
1333 */
Ming Lei93232e42013-06-06 20:01:47 +08001334static int cache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001335{
1336 int ret;
1337 const struct firmware *fw;
1338
1339 pr_debug("%s: %s\n", __func__, fw_name);
1340
1341 ret = request_firmware(&fw, fw_name, NULL);
1342 if (!ret)
1343 kfree(fw);
1344
1345 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1346
1347 return ret;
1348}
1349
Ming Lei6a2c1232013-06-26 09:28:17 +08001350static struct firmware_buf *fw_lookup_buf(const char *fw_name)
1351{
1352 struct firmware_buf *tmp;
1353 struct firmware_cache *fwc = &fw_cache;
1354
1355 spin_lock(&fwc->lock);
1356 tmp = __fw_lookup_buf(fw_name);
1357 spin_unlock(&fwc->lock);
1358
1359 return tmp;
1360}
1361
Ming Lei2887b392012-08-04 12:01:22 +08001362/**
1363 * uncache_firmware - remove one cached firmware image
1364 * @fw_name: the firmware image name
1365 *
1366 * Uncache one firmware image which has been cached successfully
1367 * before.
1368 *
1369 * Return 0 if the firmware cache has been removed successfully
1370 * Return !0 otherwise
1371 *
1372 */
Ming Lei93232e42013-06-06 20:01:47 +08001373static int uncache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001374{
1375 struct firmware_buf *buf;
1376 struct firmware fw;
1377
1378 pr_debug("%s: %s\n", __func__, fw_name);
1379
1380 if (fw_get_builtin_firmware(&fw, fw_name))
1381 return 0;
1382
1383 buf = fw_lookup_buf(fw_name);
1384 if (buf) {
1385 fw_free_buf(buf);
1386 return 0;
1387 }
1388
1389 return -EINVAL;
1390}
1391
Ming Lei37276a52012-08-04 12:01:27 +08001392static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1393{
1394 struct fw_cache_entry *fce;
1395
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001396 fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
Ming Lei37276a52012-08-04 12:01:27 +08001397 if (!fce)
1398 goto exit;
1399
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001400 fce->name = kstrdup_const(name, GFP_ATOMIC);
1401 if (!fce->name) {
1402 kfree(fce);
1403 fce = NULL;
1404 goto exit;
1405 }
Ming Lei37276a52012-08-04 12:01:27 +08001406exit:
1407 return fce;
1408}
1409
Ming Lei373304f2012-10-09 12:01:01 +08001410static int __fw_entry_found(const char *name)
1411{
1412 struct firmware_cache *fwc = &fw_cache;
1413 struct fw_cache_entry *fce;
1414
1415 list_for_each_entry(fce, &fwc->fw_names, list) {
1416 if (!strcmp(fce->name, name))
1417 return 1;
1418 }
1419 return 0;
1420}
1421
Ming Leiac39b3e2012-08-20 19:04:16 +08001422static int fw_cache_piggyback_on_request(const char *name)
1423{
1424 struct firmware_cache *fwc = &fw_cache;
1425 struct fw_cache_entry *fce;
1426 int ret = 0;
1427
1428 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001429 if (__fw_entry_found(name))
1430 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001431
1432 fce = alloc_fw_cache_entry(name);
1433 if (fce) {
1434 ret = 1;
1435 list_add(&fce->list, &fwc->fw_names);
1436 pr_debug("%s: fw: %s\n", __func__, name);
1437 }
1438found:
1439 spin_unlock(&fwc->name_lock);
1440 return ret;
1441}
1442
Ming Lei37276a52012-08-04 12:01:27 +08001443static void free_fw_cache_entry(struct fw_cache_entry *fce)
1444{
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001445 kfree_const(fce->name);
Ming Lei37276a52012-08-04 12:01:27 +08001446 kfree(fce);
1447}
1448
1449static void __async_dev_cache_fw_image(void *fw_entry,
1450 async_cookie_t cookie)
1451{
1452 struct fw_cache_entry *fce = fw_entry;
1453 struct firmware_cache *fwc = &fw_cache;
1454 int ret;
1455
1456 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001457 if (ret) {
1458 spin_lock(&fwc->name_lock);
1459 list_del(&fce->list);
1460 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001461
Ming Leiac39b3e2012-08-20 19:04:16 +08001462 free_fw_cache_entry(fce);
1463 }
Ming Lei37276a52012-08-04 12:01:27 +08001464}
1465
1466/* called with dev->devres_lock held */
1467static void dev_create_fw_entry(struct device *dev, void *res,
1468 void *data)
1469{
1470 struct fw_name_devm *fwn = res;
1471 const char *fw_name = fwn->name;
1472 struct list_head *head = data;
1473 struct fw_cache_entry *fce;
1474
1475 fce = alloc_fw_cache_entry(fw_name);
1476 if (fce)
1477 list_add(&fce->list, head);
1478}
1479
1480static int devm_name_match(struct device *dev, void *res,
1481 void *match_data)
1482{
1483 struct fw_name_devm *fwn = res;
1484 return (fwn->magic == (unsigned long)match_data);
1485}
1486
Ming Leiab6dd8e2012-08-17 22:07:00 +08001487static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001488{
1489 LIST_HEAD(todo);
1490 struct fw_cache_entry *fce;
1491 struct fw_cache_entry *fce_next;
1492 struct firmware_cache *fwc = &fw_cache;
1493
1494 devres_for_each_res(dev, fw_name_devm_release,
1495 devm_name_match, &fw_cache,
1496 dev_create_fw_entry, &todo);
1497
1498 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1499 list_del(&fce->list);
1500
1501 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001502 /* only one cache entry for one firmware */
1503 if (!__fw_entry_found(fce->name)) {
Ming Lei373304f2012-10-09 12:01:01 +08001504 list_add(&fce->list, &fwc->fw_names);
1505 } else {
1506 free_fw_cache_entry(fce);
1507 fce = NULL;
1508 }
Ming Lei37276a52012-08-04 12:01:27 +08001509 spin_unlock(&fwc->name_lock);
1510
Ming Lei373304f2012-10-09 12:01:01 +08001511 if (fce)
Ming Leid28d3882012-10-09 12:01:04 +08001512 async_schedule_domain(__async_dev_cache_fw_image,
1513 (void *)fce,
1514 &fw_cache_domain);
Ming Lei37276a52012-08-04 12:01:27 +08001515 }
1516}
1517
1518static void __device_uncache_fw_images(void)
1519{
1520 struct firmware_cache *fwc = &fw_cache;
1521 struct fw_cache_entry *fce;
1522
1523 spin_lock(&fwc->name_lock);
1524 while (!list_empty(&fwc->fw_names)) {
1525 fce = list_entry(fwc->fw_names.next,
1526 struct fw_cache_entry, list);
1527 list_del(&fce->list);
1528 spin_unlock(&fwc->name_lock);
1529
1530 uncache_firmware(fce->name);
1531 free_fw_cache_entry(fce);
1532
1533 spin_lock(&fwc->name_lock);
1534 }
1535 spin_unlock(&fwc->name_lock);
1536}
1537
1538/**
1539 * device_cache_fw_images - cache devices' firmware
1540 *
1541 * If one device called request_firmware or its nowait version
1542 * successfully before, the firmware names are recored into the
1543 * device's devres link list, so device_cache_fw_images can call
1544 * cache_firmware() to cache these firmwares for the device,
1545 * then the device driver can load its firmwares easily at
1546 * time when system is not ready to complete loading firmware.
1547 */
1548static void device_cache_fw_images(void)
1549{
1550 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001551 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001552 DEFINE_WAIT(wait);
1553
1554 pr_debug("%s\n", __func__);
1555
Ming Lei373304f2012-10-09 12:01:01 +08001556 /* cancel uncache work */
1557 cancel_delayed_work_sync(&fwc->work);
1558
Ming Leiffe53f62012-08-04 12:01:28 +08001559 /*
1560 * use small loading timeout for caching devices' firmware
1561 * because all these firmware images have been loaded
1562 * successfully at lease once, also system is ready for
1563 * completing firmware loading now. The maximum size of
1564 * firmware in current distributions is about 2M bytes,
1565 * so 10 secs should be enough.
1566 */
1567 old_timeout = loading_timeout;
1568 loading_timeout = 10;
1569
Ming Leiac39b3e2012-08-20 19:04:16 +08001570 mutex_lock(&fw_lock);
1571 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001572 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001573 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001574
1575 /* wait for completion of caching firmware for all devices */
Ming Leid28d3882012-10-09 12:01:04 +08001576 async_synchronize_full_domain(&fw_cache_domain);
Ming Leiffe53f62012-08-04 12:01:28 +08001577
1578 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001579}
1580
1581/**
1582 * device_uncache_fw_images - uncache devices' firmware
1583 *
1584 * uncache all firmwares which have been cached successfully
1585 * by device_uncache_fw_images earlier
1586 */
1587static void device_uncache_fw_images(void)
1588{
1589 pr_debug("%s\n", __func__);
1590 __device_uncache_fw_images();
1591}
1592
1593static void device_uncache_fw_images_work(struct work_struct *work)
1594{
1595 device_uncache_fw_images();
1596}
1597
1598/**
1599 * device_uncache_fw_images_delay - uncache devices firmwares
1600 * @delay: number of milliseconds to delay uncache device firmwares
1601 *
1602 * uncache all devices's firmwares which has been cached successfully
1603 * by device_cache_fw_images after @delay milliseconds.
1604 */
1605static void device_uncache_fw_images_delay(unsigned long delay)
1606{
Shaibal Duttabce66182014-01-31 15:44:58 -08001607 queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
1608 msecs_to_jiffies(delay));
Ming Lei37276a52012-08-04 12:01:27 +08001609}
1610
Ming Lei07646d92012-08-04 12:01:29 +08001611static int fw_pm_notify(struct notifier_block *notify_block,
1612 unsigned long mode, void *unused)
1613{
1614 switch (mode) {
1615 case PM_HIBERNATION_PREPARE:
1616 case PM_SUSPEND_PREPARE:
Sebastian Capellaf8d5b9e2014-02-18 17:52:08 -08001617 case PM_RESTORE_PREPARE:
Ming Leiaf5bc112013-05-27 18:30:04 +08001618 kill_requests_without_uevent();
Ming Lei07646d92012-08-04 12:01:29 +08001619 device_cache_fw_images();
1620 break;
1621
1622 case PM_POST_SUSPEND:
1623 case PM_POST_HIBERNATION:
1624 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001625 /*
1626 * In case that system sleep failed and syscore_suspend is
1627 * not called.
1628 */
1629 mutex_lock(&fw_lock);
1630 fw_cache.state = FW_LOADER_NO_CACHE;
1631 mutex_unlock(&fw_lock);
1632
Ming Lei07646d92012-08-04 12:01:29 +08001633 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1634 break;
1635 }
1636
1637 return 0;
1638}
Ming Lei07646d92012-08-04 12:01:29 +08001639
Ming Leiac39b3e2012-08-20 19:04:16 +08001640/* stop caching firmware once syscore_suspend is reached */
1641static int fw_suspend(void)
1642{
1643 fw_cache.state = FW_LOADER_NO_CACHE;
1644 return 0;
1645}
1646
1647static struct syscore_ops fw_syscore_ops = {
1648 .suspend = fw_suspend,
1649};
Ming Leicfe016b2012-09-08 17:32:30 +08001650#else
1651static int fw_cache_piggyback_on_request(const char *name)
1652{
1653 return 0;
1654}
1655#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001656
Ming Lei37276a52012-08-04 12:01:27 +08001657static void __init fw_cache_init(void)
1658{
1659 spin_lock_init(&fw_cache.lock);
1660 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001661 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001662
Ming Leicfe016b2012-09-08 17:32:30 +08001663#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +08001664 spin_lock_init(&fw_cache.name_lock);
1665 INIT_LIST_HEAD(&fw_cache.fw_names);
Ming Lei37276a52012-08-04 12:01:27 +08001666
Ming Lei37276a52012-08-04 12:01:27 +08001667 INIT_DELAYED_WORK(&fw_cache.work,
1668 device_uncache_fw_images_work);
Ming Lei07646d92012-08-04 12:01:29 +08001669
1670 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1671 register_pm_notifier(&fw_cache.pm_notify);
Ming Leiac39b3e2012-08-20 19:04:16 +08001672
1673 register_syscore_ops(&fw_syscore_ops);
Ming Leicfe016b2012-09-08 17:32:30 +08001674#endif
Ming Lei37276a52012-08-04 12:01:27 +08001675}
1676
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001677static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
Ming Lei1f2b7952012-08-04 12:01:21 +08001679 fw_cache_init();
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001680#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaife304142013-05-22 18:28:38 +02001681 register_reboot_notifier(&fw_shutdown_nb);
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001682 return class_register(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001683#else
1684 return 0;
1685#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001687
1688static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
Ming Leicfe016b2012-09-08 17:32:30 +08001690#ifdef CONFIG_PM_SLEEP
Ming Leiac39b3e2012-08-20 19:04:16 +08001691 unregister_syscore_ops(&fw_syscore_ops);
Ming Lei07646d92012-08-04 12:01:29 +08001692 unregister_pm_notifier(&fw_cache.pm_notify);
Ming Leicfe016b2012-09-08 17:32:30 +08001693#endif
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001694#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaife304142013-05-22 18:28:38 +02001695 unregister_reboot_notifier(&fw_shutdown_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 class_unregister(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001697#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698}
1699
Shaohua Lia30a6a22006-09-27 01:50:52 -07001700fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701module_exit(firmware_class_exit);