blob: 566a7456da5ea07f1c63994c2e954f5425af3d9a [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
Stephen Boyda098ecd2016-08-02 14:04:28 -070049static bool fw_get_builtin_firmware(struct firmware *fw, const char *name,
50 void *buf, size_t size)
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080051{
52 struct builtin_fw *b_fw;
53
54 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
55 if (strcmp(name, b_fw->name) == 0) {
56 fw->size = b_fw->size;
57 fw->data = b_fw->data;
Stephen Boyda098ecd2016-08-02 14:04:28 -070058
59 if (buf && fw->size <= size)
60 memcpy(buf, fw->data, fw->size);
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080061 return true;
62 }
63 }
64
65 return false;
66}
67
68static bool fw_is_builtin_firmware(const struct firmware *fw)
69{
70 struct builtin_fw *b_fw;
71
72 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
73 if (fw->data == b_fw->data)
74 return true;
75
76 return false;
77}
78
79#else /* Module case - no builtin firmware support */
80
Stephen Boyda098ecd2016-08-02 14:04:28 -070081static inline bool fw_get_builtin_firmware(struct firmware *fw,
82 const char *name, void *buf,
83 size_t size)
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080084{
85 return false;
86}
87
88static inline bool fw_is_builtin_firmware(const struct firmware *fw)
89{
90 return false;
91}
92#endif
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094enum {
95 FW_STATUS_LOADING,
96 FW_STATUS_DONE,
97 FW_STATUS_ABORT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098};
99
Dave Jones2f651682007-01-25 15:56:15 -0500100static int loading_timeout = 60; /* In seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200102static inline long firmware_loading_timeout(void)
103{
Ming Lei68ff2a02015-01-13 00:02:01 +0800104 return loading_timeout > 0 ? loading_timeout * HZ : MAX_JIFFY_OFFSET;
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200105}
106
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100107/* firmware behavior options */
108#define FW_OPT_UEVENT (1U << 0)
109#define FW_OPT_NOWAIT (1U << 1)
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100110#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwai5a1379e2014-06-04 17:48:15 +0200111#define FW_OPT_USERHELPER (1U << 2)
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100112#else
Takashi Iwai5a1379e2014-06-04 17:48:15 +0200113#define FW_OPT_USERHELPER 0
114#endif
115#ifdef CONFIG_FW_LOADER_USER_HELPER_FALLBACK
116#define FW_OPT_FALLBACK FW_OPT_USERHELPER
117#else
118#define FW_OPT_FALLBACK 0
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100119#endif
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -0700120#define FW_OPT_NO_WARN (1U << 3)
Vikram Mulukutla0e742e92016-08-02 14:04:25 -0700121#define FW_OPT_NOCACHE (1U << 4)
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100122
Ming Lei1f2b7952012-08-04 12:01:21 +0800123struct firmware_cache {
124 /* firmware_buf instance will be added into the below list */
125 spinlock_t lock;
126 struct list_head head;
Ming Leicfe016b2012-09-08 17:32:30 +0800127 int state;
Ming Lei37276a52012-08-04 12:01:27 +0800128
Ming Leicfe016b2012-09-08 17:32:30 +0800129#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +0800130 /*
131 * Names of firmware images which have been cached successfully
132 * will be added into the below list so that device uncache
133 * helper can trace which firmware images have been cached
134 * before.
135 */
136 spinlock_t name_lock;
137 struct list_head fw_names;
138
Ming Lei37276a52012-08-04 12:01:27 +0800139 struct delayed_work work;
Ming Lei07646d92012-08-04 12:01:29 +0800140
141 struct notifier_block pm_notify;
Ming Leicfe016b2012-09-08 17:32:30 +0800142#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800143};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Ming Lei12446912012-08-04 12:01:20 +0800145struct firmware_buf {
Ming Lei1f2b7952012-08-04 12:01:21 +0800146 struct kref ref;
147 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 struct completion completion;
Ming Lei1f2b7952012-08-04 12:01:21 +0800149 struct firmware_cache *fwc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 unsigned long status;
Ming Lei65710cb2012-08-04 12:01:16 +0800151 void *data;
152 size_t size;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700153 size_t allocated_size;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100154#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100155 bool is_paged_buf;
Ming Leiaf5bc112013-05-27 18:30:04 +0800156 bool need_uevent;
David Woodhouse6e03a202009-04-09 22:04:07 -0700157 struct page **pages;
158 int nr_pages;
159 int page_array_size;
Takashi Iwaife304142013-05-22 18:28:38 +0200160 struct list_head pending_list;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100161#endif
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700162 const char *fw_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163};
164
Ming Lei37276a52012-08-04 12:01:27 +0800165struct fw_cache_entry {
166 struct list_head list;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700167 const char *name;
Ming Lei37276a52012-08-04 12:01:27 +0800168};
169
Ming Leif531f05a2012-08-04 12:01:25 +0800170struct fw_name_devm {
171 unsigned long magic;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700172 const char *name;
Ming Leif531f05a2012-08-04 12:01:25 +0800173};
174
Ming Lei1f2b7952012-08-04 12:01:21 +0800175#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
176
Ming Leiac39b3e2012-08-20 19:04:16 +0800177#define FW_LOADER_NO_CACHE 0
178#define FW_LOADER_START_CACHE 1
179
180static int fw_cache_piggyback_on_request(const char *name);
181
Ming Lei1f2b7952012-08-04 12:01:21 +0800182/* fw_lock could be moved to 'struct firmware_priv' but since it is just
183 * guarding for corner cases a global lock should be OK */
184static DEFINE_MUTEX(fw_lock);
185
186static struct firmware_cache fw_cache;
187
188static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
Stephen Boyda098ecd2016-08-02 14:04:28 -0700189 struct firmware_cache *fwc,
190 void *dbuf, size_t size)
Ming Lei1f2b7952012-08-04 12:01:21 +0800191{
192 struct firmware_buf *buf;
193
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700194 buf = kzalloc(sizeof(*buf), GFP_ATOMIC);
Ming Lei1f2b7952012-08-04 12:01:21 +0800195 if (!buf)
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700196 return NULL;
197
198 buf->fw_id = kstrdup_const(fw_name, GFP_ATOMIC);
199 if (!buf->fw_id) {
200 kfree(buf);
201 return NULL;
202 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800203
204 kref_init(&buf->ref);
Ming Lei1f2b7952012-08-04 12:01:21 +0800205 buf->fwc = fwc;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700206 buf->data = dbuf;
207 buf->allocated_size = size;
Ming Lei1f2b7952012-08-04 12:01:21 +0800208 init_completion(&buf->completion);
Kyle Yanc5336ef2017-05-24 17:18:15 -0700209 INIT_LIST_HEAD(&buf->list);
Takashi Iwaife304142013-05-22 18:28:38 +0200210#ifdef CONFIG_FW_LOADER_USER_HELPER
211 INIT_LIST_HEAD(&buf->pending_list);
212#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800213
214 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
215
216 return buf;
217}
218
Ming Lei2887b392012-08-04 12:01:22 +0800219static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
220{
221 struct firmware_buf *tmp;
222 struct firmware_cache *fwc = &fw_cache;
223
224 list_for_each_entry(tmp, &fwc->head, list)
225 if (!strcmp(tmp->fw_id, fw_name))
226 return tmp;
227 return NULL;
228}
229
Ming Lei1f2b7952012-08-04 12:01:21 +0800230static int fw_lookup_and_allocate_buf(const char *fw_name,
231 struct firmware_cache *fwc,
Stephen Boyda098ecd2016-08-02 14:04:28 -0700232 struct firmware_buf **buf, void *dbuf,
Vikram Mulukutlab19be882017-04-24 19:31:14 -0700233 size_t size, unsigned int opt_flags)
Ming Lei1f2b7952012-08-04 12:01:21 +0800234{
235 struct firmware_buf *tmp;
236
237 spin_lock(&fwc->lock);
Vikram Mulukutlab19be882017-04-24 19:31:14 -0700238 if (!(opt_flags & FW_OPT_NOCACHE)) {
239 tmp = __fw_lookup_buf(fw_name);
240 if (tmp) {
241 kref_get(&tmp->ref);
242 spin_unlock(&fwc->lock);
243 *buf = tmp;
244 return 1;
245 }
Ming Lei2887b392012-08-04 12:01:22 +0800246 }
Stephen Boyda098ecd2016-08-02 14:04:28 -0700247 tmp = __allocate_fw_buf(fw_name, fwc, dbuf, size);
Vikram Mulukutlab19be882017-04-24 19:31:14 -0700248 if (tmp && !(opt_flags & FW_OPT_NOCACHE))
Ming Lei1f2b7952012-08-04 12:01:21 +0800249 list_add(&tmp->list, &fwc->head);
250 spin_unlock(&fwc->lock);
251
252 *buf = tmp;
253
254 return tmp ? 0 : -ENOMEM;
255}
256
257static void __fw_free_buf(struct kref *ref)
Bart Van Assche98233b22014-01-04 14:20:36 +0100258 __releases(&fwc->lock)
Ming Lei1f2b7952012-08-04 12:01:21 +0800259{
260 struct firmware_buf *buf = to_fwbuf(ref);
261 struct firmware_cache *fwc = buf->fwc;
Ming Lei1f2b7952012-08-04 12:01:21 +0800262
263 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
264 __func__, buf->fw_id, buf, buf->data,
265 (unsigned int)buf->size);
266
Ming Lei1f2b7952012-08-04 12:01:21 +0800267 list_del(&buf->list);
268 spin_unlock(&fwc->lock);
269
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100270#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100271 if (buf->is_paged_buf) {
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100272 int i;
Ming Lei746058f2012-10-09 12:01:03 +0800273 vunmap(buf->data);
274 for (i = 0; i < buf->nr_pages; i++)
275 __free_page(buf->pages[i]);
Chen Feng10a3fbf2015-12-16 17:03:15 +0800276 vfree(buf->pages);
Ming Lei746058f2012-10-09 12:01:03 +0800277 } else
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100278#endif
Stephen Boyda098ecd2016-08-02 14:04:28 -0700279 if (!buf->allocated_size)
Ming Lei746058f2012-10-09 12:01:03 +0800280 vfree(buf->data);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700281 kfree_const(buf->fw_id);
Ming Lei1f2b7952012-08-04 12:01:21 +0800282 kfree(buf);
283}
284
285static void fw_free_buf(struct firmware_buf *buf)
286{
Chuansheng Liubd9eb7f2012-11-10 01:27:22 +0800287 struct firmware_cache *fwc = buf->fwc;
288 spin_lock(&fwc->lock);
289 if (!kref_put(&buf->ref, __fw_free_buf))
290 spin_unlock(&fwc->lock);
Ming Lei1f2b7952012-08-04 12:01:21 +0800291}
292
jinjiawu74d1d602020-05-14 18:02:17 +0800293//[FairPhone][Audio][jinjia]=2018.11.21=smart amp porting. -s
Ming Lei746058f2012-10-09 12:01:03 +0800294/* direct firmware loading support */
Ming Lei27602842012-11-03 17:47:58 +0800295static char fw_path_para[256];
296static const char * const fw_path[] = {
297 fw_path_para,
jinjiawu74d1d602020-05-14 18:02:17 +0800298 "/system/vendor/firmware",
Ming Lei746058f2012-10-09 12:01:03 +0800299 "/lib/firmware/updates/" UTS_RELEASE,
300 "/lib/firmware/updates",
301 "/lib/firmware/" UTS_RELEASE,
Satya Durga Srinivasu Prabhala036c9b22017-02-27 11:27:10 -0800302 "/lib/firmware"
Ming Lei746058f2012-10-09 12:01:03 +0800303};
jinjiawu74d1d602020-05-14 18:02:17 +0800304//[FairPhone][Audio][jinjia]=2018.11.21=smart amp porting. -e
Ming Lei746058f2012-10-09 12:01:03 +0800305
Ming Lei27602842012-11-03 17:47:58 +0800306/*
307 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
308 * from kernel command line because firmware_class is generally built in
309 * kernel instead of module.
310 */
311module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
312MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
313
Luis R. Rodriguez5275d192015-07-30 15:48:57 -0700314static void fw_finish_direct_load(struct device *device,
315 struct firmware_buf *buf)
316{
317 mutex_lock(&fw_lock);
318 set_bit(FW_STATUS_DONE, &buf->status);
319 complete_all(&buf->completion);
320 mutex_unlock(&fw_lock);
321}
322
Stephen Boyda098ecd2016-08-02 14:04:28 -0700323static int
324fw_get_filesystem_firmware(struct device *device, struct firmware_buf *buf)
Ming Lei746058f2012-10-09 12:01:03 +0800325{
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500326 loff_t size;
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700327 int i, len;
Neil Horman3e358ac2013-09-06 15:36:08 -0400328 int rc = -ENOENT;
Luis R. Rodriguezf5727b02015-05-12 14:49:40 -0700329 char *path;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700330 enum kernel_read_file_id id = READING_FIRMWARE;
331 size_t msize = INT_MAX;
332
333 /* Already populated data member means we're loading into a buffer */
334 if (buf->data) {
335 id = READING_FIRMWARE_PREALLOC_BUFFER;
336 msize = buf->allocated_size;
337 }
Luis R. Rodriguezf5727b02015-05-12 14:49:40 -0700338
339 path = __getname();
340 if (!path)
341 return -ENOMEM;
Ming Lei746058f2012-10-09 12:01:03 +0800342
343 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
Ming Lei27602842012-11-03 17:47:58 +0800344 /* skip the unset customized path */
345 if (!fw_path[i][0])
346 continue;
347
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700348 len = snprintf(path, PATH_MAX, "%s/%s",
349 fw_path[i], buf->fw_id);
350 if (len >= PATH_MAX) {
351 rc = -ENAMETOOLONG;
352 break;
353 }
Ming Lei746058f2012-10-09 12:01:03 +0800354
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500355 buf->size = 0;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700356 rc = kernel_read_file_from_path(path, &buf->data, &size, msize,
357 id);
Kees Cook4b2530d2016-02-04 13:15:02 -0800358 if (rc) {
Luis R. Rodriguez8e516aa2016-02-28 21:57:55 +0100359 if (rc == -ENOENT)
360 dev_dbg(device, "loading %s failed with error %d\n",
361 path, rc);
362 else
363 dev_warn(device, "loading %s failed with error %d\n",
364 path, rc);
Kees Cook4b2530d2016-02-04 13:15:02 -0800365 continue;
366 }
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500367 dev_dbg(device, "direct-loading %s\n", buf->fw_id);
368 buf->size = size;
Luis R. Rodriguez5275d192015-07-30 15:48:57 -0700369 fw_finish_direct_load(device, buf);
Kees Cook4b2530d2016-02-04 13:15:02 -0800370 break;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100371 }
Kees Cook4b2530d2016-02-04 13:15:02 -0800372 __putname(path);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100373
Neil Horman3e358ac2013-09-06 15:36:08 -0400374 return rc;
Ming Lei746058f2012-10-09 12:01:03 +0800375}
376
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100377/* firmware holds the ownership of pages */
378static void firmware_free_data(const struct firmware *fw)
379{
380 /* Loaded directly? */
381 if (!fw->priv) {
382 vfree(fw->data);
383 return;
384 }
385 fw_free_buf(fw->priv);
386}
387
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100388/* store the pages buffer info firmware from buf */
389static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
390{
391 fw->priv = buf;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100392#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100393 fw->pages = buf->pages;
394#endif
395 fw->size = buf->size;
396 fw->data = buf->data;
397
398 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
399 __func__, buf->fw_id, buf, buf->data,
400 (unsigned int)buf->size);
401}
402
403#ifdef CONFIG_PM_SLEEP
404static void fw_name_devm_release(struct device *dev, void *res)
405{
406 struct fw_name_devm *fwn = res;
407
408 if (fwn->magic == (unsigned long)&fw_cache)
409 pr_debug("%s: fw_name-%s devm-%p released\n",
410 __func__, fwn->name, res);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700411 kfree_const(fwn->name);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100412}
413
414static int fw_devm_match(struct device *dev, void *res,
415 void *match_data)
416{
417 struct fw_name_devm *fwn = res;
418
419 return (fwn->magic == (unsigned long)&fw_cache) &&
420 !strcmp(fwn->name, match_data);
421}
422
423static struct fw_name_devm *fw_find_devm_name(struct device *dev,
424 const char *name)
425{
426 struct fw_name_devm *fwn;
427
428 fwn = devres_find(dev, fw_name_devm_release,
429 fw_devm_match, (void *)name);
430 return fwn;
431}
432
433/* add firmware name into devres list */
434static int fw_add_devm_name(struct device *dev, const char *name)
435{
436 struct fw_name_devm *fwn;
437
438 fwn = fw_find_devm_name(dev, name);
439 if (fwn)
440 return 1;
441
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700442 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
443 GFP_KERNEL);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100444 if (!fwn)
445 return -ENOMEM;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700446 fwn->name = kstrdup_const(name, GFP_KERNEL);
447 if (!fwn->name) {
Vladimir Zapolskiya885de62015-07-29 23:26:28 +0300448 devres_free(fwn);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700449 return -ENOMEM;
450 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100451
452 fwn->magic = (unsigned long)&fw_cache;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100453 devres_add(dev, fwn);
454
455 return 0;
456}
457#else
458static int fw_add_devm_name(struct device *dev, const char *name)
459{
460 return 0;
461}
462#endif
463
464
465/*
466 * user-mode helper code
467 */
468#ifdef CONFIG_FW_LOADER_USER_HELPER
469struct firmware_priv {
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100470 bool nowait;
471 struct device dev;
472 struct firmware_buf *buf;
473 struct firmware *fw;
474};
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100475
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700476static struct firmware_priv *to_firmware_priv(struct device *dev)
477{
478 return container_of(dev, struct firmware_priv, dev);
479}
480
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700481static void __fw_load_abort(struct firmware_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Ming Lei87597932013-06-15 16:36:38 +0800483 /*
484 * There is a small window in which user can write to 'loading'
485 * between loading done and disappearance of 'loading'
486 */
487 if (test_bit(FW_STATUS_DONE, &buf->status))
488 return;
489
Takashi Iwaife304142013-05-22 18:28:38 +0200490 list_del_init(&buf->pending_list);
Ming Lei12446912012-08-04 12:01:20 +0800491 set_bit(FW_STATUS_ABORT, &buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800492 complete_all(&buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700495static void fw_load_abort(struct firmware_priv *fw_priv)
496{
497 struct firmware_buf *buf = fw_priv->buf;
498
499 __fw_load_abort(buf);
Ming Lei87597932013-06-15 16:36:38 +0800500
501 /* avoid user action after loading abort */
502 fw_priv->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
Takashi Iwai807be032013-01-31 11:13:57 +0100505#define is_fw_load_aborted(buf) \
506 test_bit(FW_STATUS_ABORT, &(buf)->status)
507
Takashi Iwaife304142013-05-22 18:28:38 +0200508static LIST_HEAD(pending_fw_head);
509
510/* reboot notifier for avoid deadlock with usermode_lock */
511static int fw_shutdown_notify(struct notifier_block *unused1,
512 unsigned long unused2, void *unused3)
513{
514 mutex_lock(&fw_lock);
515 while (!list_empty(&pending_fw_head))
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700516 __fw_load_abort(list_first_entry(&pending_fw_head,
Takashi Iwaife304142013-05-22 18:28:38 +0200517 struct firmware_buf,
518 pending_list));
519 mutex_unlock(&fw_lock);
520 return NOTIFY_DONE;
521}
522
523static struct notifier_block fw_shutdown_nb = {
524 .notifier_call = fw_shutdown_notify,
525};
526
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700527static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
528 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 return sprintf(buf, "%d\n", loading_timeout);
531}
532
533/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800534 * firmware_timeout_store - set number of seconds to wait for firmware
535 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800536 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800537 * @buf: buffer to scan for timeout value
538 * @count: number of bytes in @buf
539 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800541 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 * firmware will be provided.
543 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800544 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 **/
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700546static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
547 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700550 if (loading_timeout < 0)
551 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return count;
554}
555
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800556static struct class_attribute firmware_class_attrs[] = {
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700557 __ATTR_RW(timeout),
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800558 __ATTR_NULL
559};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800561static void fw_dev_release(struct device *dev)
562{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700563 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800564
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800565 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800566}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Linus Torvalds6f957722015-07-09 11:20:01 -0700568static int do_firmware_uevent(struct firmware_priv *fw_priv, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Ming Lei12446912012-08-04 12:01:20 +0800570 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200572 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700573 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200574 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
575 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 return 0;
578}
579
Linus Torvalds6f957722015-07-09 11:20:01 -0700580static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
581{
582 struct firmware_priv *fw_priv = to_firmware_priv(dev);
583 int err = 0;
584
585 mutex_lock(&fw_lock);
586 if (fw_priv->buf)
587 err = do_firmware_uevent(fw_priv, env);
588 mutex_unlock(&fw_lock);
589 return err;
590}
591
Adrian Bunk1b81d662006-05-20 15:00:16 -0700592static struct class firmware_class = {
593 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800594 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700595 .dev_uevent = firmware_uevent,
596 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700597};
598
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700599static ssize_t firmware_loading_show(struct device *dev,
600 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700602 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800603 int loading = 0;
604
605 mutex_lock(&fw_lock);
606 if (fw_priv->buf)
607 loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
608 mutex_unlock(&fw_lock);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return sprintf(buf, "%d\n", loading);
611}
612
David Woodhouse6e03a202009-04-09 22:04:07 -0700613/* Some architectures don't have PAGE_KERNEL_RO */
614#ifndef PAGE_KERNEL_RO
615#define PAGE_KERNEL_RO PAGE_KERNEL
616#endif
Ming Lei253c9242012-10-09 12:01:02 +0800617
618/* one pages buffer should be mapped/unmapped only once */
619static int fw_map_pages_buf(struct firmware_buf *buf)
620{
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100621 if (!buf->is_paged_buf)
Ming Lei746058f2012-10-09 12:01:03 +0800622 return 0;
623
Markus Elfringdaa3d672014-11-19 11:38:38 +0100624 vunmap(buf->data);
Ming Lei253c9242012-10-09 12:01:02 +0800625 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
626 if (!buf->data)
627 return -ENOMEM;
628 return 0;
629}
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800632 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700633 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800634 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800635 * @buf: buffer to scan for loading control value
636 * @count: number of bytes in @buf
637 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 * The relevant values are:
639 *
640 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800641 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 * -1: Conclude the load with an error and discard any written data.
643 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700644static ssize_t firmware_loading_store(struct device *dev,
645 struct device_attribute *attr,
646 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700648 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800649 struct firmware_buf *fw_buf;
Kees Cook6593d922014-02-25 13:06:00 -0800650 ssize_t written = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700652 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Neil Hormaneea915b2012-01-02 15:31:23 -0500654 mutex_lock(&fw_lock);
Ming Lei87597932013-06-15 16:36:38 +0800655 fw_buf = fw_priv->buf;
Ming Lei12446912012-08-04 12:01:20 +0800656 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500657 goto out;
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 switch (loading) {
660 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800661 /* discarding any previous partial load */
Ming Lei12446912012-08-04 12:01:20 +0800662 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
663 for (i = 0; i < fw_buf->nr_pages; i++)
664 __free_page(fw_buf->pages[i]);
Chen Feng10a3fbf2015-12-16 17:03:15 +0800665 vfree(fw_buf->pages);
Ming Lei12446912012-08-04 12:01:20 +0800666 fw_buf->pages = NULL;
667 fw_buf->page_array_size = 0;
668 fw_buf->nr_pages = 0;
669 set_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei28eefa72012-08-04 12:01:17 +0800670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 break;
672 case 0:
Ming Lei12446912012-08-04 12:01:20 +0800673 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
Kees Cook6593d922014-02-25 13:06:00 -0800674 int rc;
675
Ming Lei12446912012-08-04 12:01:20 +0800676 set_bit(FW_STATUS_DONE, &fw_buf->status);
677 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei253c9242012-10-09 12:01:02 +0800678
679 /*
680 * Several loading requests may be pending on
681 * one same firmware buf, so let all requests
682 * see the mapped 'buf->data' once the loading
683 * is completed.
684 * */
Kees Cook6593d922014-02-25 13:06:00 -0800685 rc = fw_map_pages_buf(fw_buf);
686 if (rc)
zhang jun2b1278c2014-02-13 15:18:47 +0800687 dev_err(dev, "%s: map pages failed\n",
688 __func__);
Kees Cook6593d922014-02-25 13:06:00 -0800689 else
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500690 rc = security_kernel_post_read_file(NULL,
691 fw_buf->data, fw_buf->size,
692 READING_FIRMWARE);
Kees Cook6593d922014-02-25 13:06:00 -0800693
694 /*
695 * Same logic as fw_load_abort, only the DONE bit
696 * is ignored and we set ABORT only on failure.
697 */
Takashi Iwaife304142013-05-22 18:28:38 +0200698 list_del_init(&fw_buf->pending_list);
Kees Cook6593d922014-02-25 13:06:00 -0800699 if (rc) {
700 set_bit(FW_STATUS_ABORT, &fw_buf->status);
701 written = rc;
702 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800703 complete_all(&fw_buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 break;
705 }
706 /* fallthrough */
707 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700708 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 /* fallthrough */
710 case -1:
711 fw_load_abort(fw_priv);
712 break;
713 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500714out:
715 mutex_unlock(&fw_lock);
Kees Cook6593d922014-02-25 13:06:00 -0800716 return written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700719static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Stephen Boyda098ecd2016-08-02 14:04:28 -0700721static void firmware_rw_buf(struct firmware_buf *buf, char *buffer,
722 loff_t offset, size_t count, bool read)
723{
724 if (read)
725 memcpy(buffer, buf->data + offset, count);
726 else
727 memcpy(buf->data + offset, buffer, count);
728}
729
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700730static void firmware_rw(struct firmware_buf *buf, char *buffer,
731 loff_t offset, size_t count, bool read)
732{
733 while (count) {
734 void *page_data;
735 int page_nr = offset >> PAGE_SHIFT;
736 int page_ofs = offset & (PAGE_SIZE-1);
737 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
738
739 page_data = kmap(buf->pages[page_nr]);
740
741 if (read)
742 memcpy(buffer, page_data + page_ofs, page_cnt);
743 else
744 memcpy(page_data + page_ofs, buffer, page_cnt);
745
746 kunmap(buf->pages[page_nr]);
747 buffer += page_cnt;
748 offset += page_cnt;
749 count -= page_cnt;
750 }
751}
752
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700753static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
754 struct bin_attribute *bin_attr,
755 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200757 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700758 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800759 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700760 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Laura Garciacad1e552006-05-23 23:22:38 +0200762 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800763 buf = fw_priv->buf;
764 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 ret_count = -ENODEV;
766 goto out;
767 }
Ming Lei12446912012-08-04 12:01:20 +0800768 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200769 ret_count = 0;
770 goto out;
771 }
Ming Lei12446912012-08-04 12:01:20 +0800772 if (count > buf->size - offset)
773 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700774
775 ret_count = count;
776
Stephen Boyda098ecd2016-08-02 14:04:28 -0700777 if (buf->data)
778 firmware_rw_buf(buf, buffer, offset, count, true);
779 else
780 firmware_rw(buf, buffer, offset, count, true);
David Woodhouse6e03a202009-04-09 22:04:07 -0700781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782out:
Laura Garciacad1e552006-05-23 23:22:38 +0200783 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return ret_count;
785}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800786
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700787static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Ming Lei12446912012-08-04 12:01:20 +0800789 struct firmware_buf *buf = fw_priv->buf;
Fabian Fredericka76040d2014-07-01 21:13:30 +0200790 int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
David Woodhouse6e03a202009-04-09 22:04:07 -0700792 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800793 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700794 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800795 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700796 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Chen Feng10a3fbf2015-12-16 17:03:15 +0800798 new_pages = vmalloc(new_array_size * sizeof(void *));
David Woodhouse6e03a202009-04-09 22:04:07 -0700799 if (!new_pages) {
800 fw_load_abort(fw_priv);
801 return -ENOMEM;
802 }
Ming Lei12446912012-08-04 12:01:20 +0800803 memcpy(new_pages, buf->pages,
804 buf->page_array_size * sizeof(void *));
805 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
806 (new_array_size - buf->page_array_size));
Chen Feng10a3fbf2015-12-16 17:03:15 +0800807 vfree(buf->pages);
Ming Lei12446912012-08-04 12:01:20 +0800808 buf->pages = new_pages;
809 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700811
Ming Lei12446912012-08-04 12:01:20 +0800812 while (buf->nr_pages < pages_needed) {
813 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700814 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
815
Ming Lei12446912012-08-04 12:01:20 +0800816 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700817 fw_load_abort(fw_priv);
818 return -ENOMEM;
819 }
Ming Lei12446912012-08-04 12:01:20 +0800820 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return 0;
823}
824
825/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800826 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700827 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700828 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700829 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800830 * @buffer: buffer being written
831 * @offset: buffer offset for write in total data store area
832 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800834 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 * the driver as a firmware image.
836 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700837static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
838 struct bin_attribute *bin_attr,
839 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200841 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700842 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800843 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 ssize_t retval;
845
846 if (!capable(CAP_SYS_RAWIO))
847 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700848
Laura Garciacad1e552006-05-23 23:22:38 +0200849 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800850 buf = fw_priv->buf;
851 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 retval = -ENODEV;
853 goto out;
854 }
Ming Lei65710cb2012-08-04 12:01:16 +0800855
Stephen Boyda098ecd2016-08-02 14:04:28 -0700856 if (buf->data) {
857 if (offset + count > buf->allocated_size) {
858 retval = -ENOMEM;
859 goto out;
860 }
861 firmware_rw_buf(buf, buffer, offset, count, false);
862 retval = count;
863 } else {
864 retval = fw_realloc_buffer(fw_priv, offset + count);
865 if (retval)
866 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Stephen Boyda098ecd2016-08-02 14:04:28 -0700868 retval = count;
869 firmware_rw(buf, buffer, offset, count, false);
870 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700871
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700872 buf->size = max_t(size_t, offset + count, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873out:
Laura Garciacad1e552006-05-23 23:22:38 +0200874 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return retval;
876}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800877
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700878static struct bin_attribute firmware_attr_data = {
879 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 .size = 0,
881 .read = firmware_data_read,
882 .write = firmware_data_write,
883};
884
Takashi Iwai46239902015-02-04 15:18:07 +0100885static struct attribute *fw_dev_attrs[] = {
886 &dev_attr_loading.attr,
887 NULL
888};
889
890static struct bin_attribute *fw_dev_bin_attrs[] = {
891 &firmware_attr_data,
892 NULL
893};
894
895static const struct attribute_group fw_dev_attr_group = {
896 .attrs = fw_dev_attrs,
897 .bin_attrs = fw_dev_bin_attrs,
898};
899
900static const struct attribute_group *fw_dev_attr_groups[] = {
901 &fw_dev_attr_group,
902 NULL
903};
904
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700905static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200906fw_create_instance(struct firmware *firmware, const char *fw_name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100907 struct device *device, unsigned int opt_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700909 struct firmware_priv *fw_priv;
910 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Ming Lei12446912012-08-04 12:01:20 +0800912 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700913 if (!fw_priv) {
Ming Lei12446912012-08-04 12:01:20 +0800914 fw_priv = ERR_PTR(-ENOMEM);
915 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100918 fw_priv->nowait = !!(opt_flags & FW_OPT_NOWAIT);
Ming Lei1f2b7952012-08-04 12:01:21 +0800919 fw_priv->fw = firmware;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700920 f_dev = &fw_priv->dev;
921
922 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800923 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700924 f_dev->parent = device;
925 f_dev->class = &firmware_class;
Takashi Iwai46239902015-02-04 15:18:07 +0100926 f_dev->groups = fw_dev_attr_groups;
Ming Lei12446912012-08-04 12:01:20 +0800927exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700928 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100930
931/* load a firmware via user helper */
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100932static int _request_firmware_load(struct firmware_priv *fw_priv,
933 unsigned int opt_flags, long timeout)
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100934{
935 int retval = 0;
936 struct device *f_dev = &fw_priv->dev;
937 struct firmware_buf *buf = fw_priv->buf;
938
939 /* fall back on userspace loading */
Stephen Boyda098ecd2016-08-02 14:04:28 -0700940 if (!buf->data)
941 buf->is_paged_buf = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100942
943 dev_set_uevent_suppress(f_dev, true);
944
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100945 retval = device_add(f_dev);
946 if (retval) {
947 dev_err(f_dev, "%s: device_register failed\n", __func__);
948 goto err_put_dev;
949 }
950
Maxime Bizon1eeeef12013-08-29 20:28:13 +0200951 mutex_lock(&fw_lock);
952 list_add(&buf->pending_list, &pending_fw_head);
953 mutex_unlock(&fw_lock);
954
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100955 if (opt_flags & FW_OPT_UEVENT) {
Ming Leiaf5bc112013-05-27 18:30:04 +0800956 buf->need_uevent = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100957 dev_set_uevent_suppress(f_dev, false);
958 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100959 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
Ming Lei68ff2a02015-01-13 00:02:01 +0800960 } else {
961 timeout = MAX_JIFFY_OFFSET;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100962 }
963
Kyle Yan5f4db9a2017-09-20 17:18:35 -0700964 timeout = wait_for_completion_killable_timeout(&buf->completion,
Ming Lei68ff2a02015-01-13 00:02:01 +0800965 timeout);
Yves-Alexis Perezb3854ce2016-11-11 11:28:40 -0800966 if (timeout == -ERESTARTSYS || !timeout) {
967 retval = timeout;
Ming Lei0cb64242015-01-13 00:01:35 +0800968 mutex_lock(&fw_lock);
969 fw_load_abort(fw_priv);
970 mutex_unlock(&fw_lock);
Yves-Alexis Perezb3854ce2016-11-11 11:28:40 -0800971 } else if (timeout > 0) {
Zahari Doychevef518cc2015-03-10 10:45:40 +0100972 retval = 0;
Ming Lei0cb64242015-01-13 00:01:35 +0800973 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100974
Shuah Khan0542ad82014-07-22 18:10:38 -0600975 if (is_fw_load_aborted(buf))
976 retval = -EAGAIN;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700977 else if (buf->is_paged_buf && !buf->data)
zhang jun2b1278c2014-02-13 15:18:47 +0800978 retval = -ENOMEM;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100979
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100980 device_del(f_dev);
981err_put_dev:
982 put_device(f_dev);
983 return retval;
984}
985
986static int fw_load_from_user_helper(struct firmware *firmware,
987 const char *name, struct device *device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100988 unsigned int opt_flags, long timeout)
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100989{
990 struct firmware_priv *fw_priv;
991
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100992 fw_priv = fw_create_instance(firmware, name, device, opt_flags);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100993 if (IS_ERR(fw_priv))
994 return PTR_ERR(fw_priv);
995
996 fw_priv->buf = firmware->priv;
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100997 return _request_firmware_load(fw_priv, opt_flags, timeout);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100998}
Ming Leiddf1f062013-06-04 10:01:13 +0800999
Tim Murray57c7ee12016-10-20 13:53:58 -07001000#ifdef CONFIG_FW_CACHE
Ming Leiddf1f062013-06-04 10:01:13 +08001001/* kill pending requests without uevent to avoid blocking suspend */
1002static void kill_requests_without_uevent(void)
1003{
1004 struct firmware_buf *buf;
1005 struct firmware_buf *next;
1006
1007 mutex_lock(&fw_lock);
1008 list_for_each_entry_safe(buf, next, &pending_fw_head, pending_list) {
1009 if (!buf->need_uevent)
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -07001010 __fw_load_abort(buf);
Ming Leiddf1f062013-06-04 10:01:13 +08001011 }
1012 mutex_unlock(&fw_lock);
1013}
Ming Lei5b7cb7a2013-06-06 19:52:54 +08001014#endif
Ming Leiddf1f062013-06-04 10:01:13 +08001015
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001016#else /* CONFIG_FW_LOADER_USER_HELPER */
1017static inline int
1018fw_load_from_user_helper(struct firmware *firmware, const char *name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001019 struct device *device, unsigned int opt_flags,
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001020 long timeout)
1021{
1022 return -ENOENT;
1023}
Takashi Iwai807be032013-01-31 11:13:57 +01001024
1025/* No abort during direct loading */
1026#define is_fw_load_aborted(buf) false
1027
Ming Lei5b7cb7a2013-06-06 19:52:54 +08001028#ifdef CONFIG_PM_SLEEP
Ming Leiddf1f062013-06-04 10:01:13 +08001029static inline void kill_requests_without_uevent(void) { }
Ming Lei5b7cb7a2013-06-06 19:52:54 +08001030#endif
Ming Leiddf1f062013-06-04 10:01:13 +08001031
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001032#endif /* CONFIG_FW_LOADER_USER_HELPER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Ming Leif531f05a2012-08-04 12:01:25 +08001034
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001035/* wait until the shared firmware_buf becomes ready (or error) */
1036static int sync_cached_firmware_buf(struct firmware_buf *buf)
Ming Lei1f2b7952012-08-04 12:01:21 +08001037{
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001038 int ret = 0;
1039
1040 mutex_lock(&fw_lock);
1041 while (!test_bit(FW_STATUS_DONE, &buf->status)) {
Takashi Iwai807be032013-01-31 11:13:57 +01001042 if (is_fw_load_aborted(buf)) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001043 ret = -ENOENT;
1044 break;
1045 }
1046 mutex_unlock(&fw_lock);
Kweh, Hock Leong000deba2014-11-18 10:56:59 +08001047 ret = wait_for_completion_interruptible(&buf->completion);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001048 mutex_lock(&fw_lock);
1049 }
1050 mutex_unlock(&fw_lock);
1051 return ret;
Ming Lei1f2b7952012-08-04 12:01:21 +08001052}
1053
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001054/* prepare firmware and firmware_buf structs;
1055 * return 0 if a firmware is already assigned, 1 if need to load one,
1056 * or a negative error code
1057 */
1058static int
1059_request_firmware_prepare(struct firmware **firmware_p, const char *name,
Vikram Mulukutlab19be882017-04-24 19:31:14 -07001060 struct device *device, void *dbuf, size_t size,
1061 unsigned int opt_flags)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001062{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +08001064 struct firmware_buf *buf;
1065 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Jiri Slaby4aed0642005-09-13 01:25:01 -07001067 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -07001069 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
1070 __func__);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001071 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Stephen Boyda098ecd2016-08-02 14:04:28 -07001074 if (fw_get_builtin_firmware(firmware, name, dbuf, size)) {
Luis R. Rodriguezed046302015-04-29 16:30:43 -07001075 dev_dbg(device, "using built-in %s\n", name);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001076 return 0; /* assigned */
David Woodhouse5658c762008-05-23 13:52:42 +01001077 }
1078
Vikram Mulukutlab19be882017-04-24 19:31:14 -07001079 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf, dbuf, size,
1080 opt_flags);
Ming Lei1f2b7952012-08-04 12:01:21 +08001081
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001082 /*
1083 * bind with 'buf' now to avoid warning in failure path
1084 * of requesting firmware.
1085 */
1086 firmware->priv = buf;
Ming Lei1f2b7952012-08-04 12:01:21 +08001087
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001088 if (ret > 0) {
1089 ret = sync_cached_firmware_buf(buf);
1090 if (!ret) {
1091 fw_set_page_data(buf, firmware);
1092 return 0; /* assigned */
1093 }
Stephen Boyddddb5542012-03-28 23:30:43 +02001094 }
Ming Lei1f2b7952012-08-04 12:01:21 +08001095
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001096 if (ret < 0)
1097 return ret;
1098 return 1; /* need to load */
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001099}
1100
Ming Leie771d1a2013-05-27 18:30:03 +08001101static int assign_firmware_buf(struct firmware *fw, struct device *device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001102 unsigned int opt_flags)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001103{
1104 struct firmware_buf *buf = fw->priv;
1105
1106 mutex_lock(&fw_lock);
Takashi Iwai807be032013-01-31 11:13:57 +01001107 if (!buf->size || is_fw_load_aborted(buf)) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001108 mutex_unlock(&fw_lock);
1109 return -ENOENT;
1110 }
1111
1112 /*
1113 * add firmware name into devres list so that we can auto cache
1114 * and uncache firmware for device.
1115 *
1116 * device may has been deleted already, but the problem
1117 * should be fixed in devres or driver core.
1118 */
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001119 /* don't cache firmware handled without uevent */
Vikram Mulukutla0e742e92016-08-02 14:04:25 -07001120 if (device && (opt_flags & FW_OPT_UEVENT) &&
1121 !(opt_flags & FW_OPT_NOCACHE))
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001122 fw_add_devm_name(device, buf->fw_id);
1123
1124 /*
1125 * After caching firmware image is started, let it piggyback
1126 * on request firmware.
1127 */
Vikram Mulukutla0e742e92016-08-02 14:04:25 -07001128 if (!(opt_flags & FW_OPT_NOCACHE) &&
1129 buf->fwc->state == FW_LOADER_START_CACHE) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001130 if (fw_cache_piggyback_on_request(buf->fw_id))
1131 kref_get(&buf->ref);
1132 }
1133
1134 /* pass the pages buffer to driver at the last minute */
1135 fw_set_page_data(buf, fw);
1136 mutex_unlock(&fw_lock);
1137 return 0;
1138}
1139
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001140/* called from request_firmware() and request_firmware_work_func() */
1141static int
1142_request_firmware(const struct firmware **firmware_p, const char *name,
Stephen Boyda098ecd2016-08-02 14:04:28 -07001143 struct device *device, void *buf, size_t size,
1144 unsigned int opt_flags)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001145{
Brian Norris715780a2015-12-09 14:50:28 -08001146 struct firmware *fw = NULL;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001147 long timeout;
1148 int ret;
1149
1150 if (!firmware_p)
1151 return -EINVAL;
1152
Brian Norris715780a2015-12-09 14:50:28 -08001153 if (!name || name[0] == '\0') {
1154 ret = -EINVAL;
1155 goto out;
1156 }
Kees Cook471b0952014-09-18 11:25:37 -07001157
Vikram Mulukutlab19be882017-04-24 19:31:14 -07001158 ret = _request_firmware_prepare(&fw, name, device, buf, size,
1159 opt_flags);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001160 if (ret <= 0) /* error or already assigned */
1161 goto out;
1162
1163 ret = 0;
1164 timeout = firmware_loading_timeout();
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001165 if (opt_flags & FW_OPT_NOWAIT) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001166 timeout = usermodehelper_read_lock_wait(timeout);
1167 if (!timeout) {
1168 dev_dbg(device, "firmware: %s loading timed out\n",
1169 name);
1170 ret = -EBUSY;
1171 goto out;
1172 }
1173 } else {
1174 ret = usermodehelper_read_trylock();
1175 if (WARN_ON(ret)) {
1176 dev_err(device, "firmware: %s will not be loaded\n",
1177 name);
1178 goto out;
1179 }
1180 }
1181
Neil Horman3e358ac2013-09-06 15:36:08 -04001182 ret = fw_get_filesystem_firmware(device, fw->priv);
1183 if (ret) {
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001184 if (!(opt_flags & FW_OPT_NO_WARN))
Kyle Yan7e2a08d2017-05-09 15:40:17 -07001185 dev_dbg(device,
1186 "Firmware %s was not found in kernel paths. rc:%d\n",
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001187 name, ret);
1188 if (opt_flags & FW_OPT_USERHELPER) {
Kyle Yan7e2a08d2017-05-09 15:40:17 -07001189 dev_dbg(device, "Falling back to user helper\n");
Takashi Iwaibba3a872013-12-02 15:38:16 +01001190 ret = fw_load_from_user_helper(fw, name, device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001191 opt_flags, timeout);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001192 }
Neil Horman3e358ac2013-09-06 15:36:08 -04001193 }
Ming Leie771d1a2013-05-27 18:30:03 +08001194
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001195 if (!ret)
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001196 ret = assign_firmware_buf(fw, device, opt_flags);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001197
1198 usermodehelper_read_unlock();
1199
1200 out:
1201 if (ret < 0) {
1202 release_firmware(fw);
1203 fw = NULL;
1204 }
1205
1206 *firmware_p = fw;
1207 return ret;
1208}
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210/**
Kay Sievers312c0042005-11-16 09:00:00 +01001211 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001212 * @firmware_p: pointer to firmware image
1213 * @name: name of firmware file
1214 * @device: device for which firmware is being loaded
1215 *
1216 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001217 * of @name for device @device.
1218 *
1219 * Should be called from user context where sleeping is allowed.
1220 *
Kay Sievers312c0042005-11-16 09:00:00 +01001221 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001222 * should be distinctive enough not to be confused with any other
1223 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +08001224 *
1225 * Caller must hold the reference count of @device.
Ming Lei6a927852012-11-03 17:48:16 +08001226 *
1227 * The function can be called safely inside device's suspend and
1228 * resume callback.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001229 **/
1230int
1231request_firmware(const struct firmware **firmware_p, const char *name,
Andrei Opreaea310032015-03-08 12:41:15 +02001232 struct device *device)
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001233{
Ming Leid6c8aa32013-06-06 20:01:48 +08001234 int ret;
1235
1236 /* Need to pin this module until return */
1237 __module_get(THIS_MODULE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001238 ret = _request_firmware(firmware_p, name, device, NULL, 0,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001239 FW_OPT_UEVENT | FW_OPT_FALLBACK);
Ming Leid6c8aa32013-06-06 20:01:48 +08001240 module_put(THIS_MODULE);
1241 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001242}
Daniel Mackf4945132013-05-23 22:17:18 +02001243EXPORT_SYMBOL(request_firmware);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001244
Takashi Iwaibba3a872013-12-02 15:38:16 +01001245/**
Borislav Petkov3c1556b2014-12-03 22:46:37 +01001246 * request_firmware_direct: - load firmware directly without usermode helper
Takashi Iwaibba3a872013-12-02 15:38:16 +01001247 * @firmware_p: pointer to firmware image
1248 * @name: name of firmware file
1249 * @device: device for which firmware is being loaded
1250 *
1251 * This function works pretty much like request_firmware(), but this doesn't
1252 * fall back to usermode helper even if the firmware couldn't be loaded
1253 * directly from fs. Hence it's useful for loading optional firmwares, which
1254 * aren't always present, without extra long timeouts of udev.
1255 **/
1256int request_firmware_direct(const struct firmware **firmware_p,
1257 const char *name, struct device *device)
1258{
1259 int ret;
Andrei Opreaea310032015-03-08 12:41:15 +02001260
Takashi Iwaibba3a872013-12-02 15:38:16 +01001261 __module_get(THIS_MODULE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001262 ret = _request_firmware(firmware_p, name, device, NULL, 0,
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001263 FW_OPT_UEVENT | FW_OPT_NO_WARN);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001264 module_put(THIS_MODULE);
1265 return ret;
1266}
1267EXPORT_SYMBOL_GPL(request_firmware_direct);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001268
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001269/**
Stephen Boyda098ecd2016-08-02 14:04:28 -07001270 * request_firmware_into_buf - load firmware into a previously allocated buffer
1271 * @firmware_p: pointer to firmware image
1272 * @name: name of firmware file
1273 * @device: device for which firmware is being loaded and DMA region allocated
1274 * @buf: address of buffer to load firmware into
1275 * @size: size of buffer
1276 *
1277 * This function works pretty much like request_firmware(), but it doesn't
1278 * allocate a buffer to hold the firmware data. Instead, the firmware
1279 * is loaded directly into the buffer pointed to by @buf and the @firmware_p
1280 * data member is pointed at @buf.
1281 *
1282 * This function doesn't cache firmware either.
1283 */
1284int
1285request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
1286 struct device *device, void *buf, size_t size)
1287{
1288 int ret;
1289
1290 __module_get(THIS_MODULE);
1291 ret = _request_firmware(firmware_p, name, device, buf, size,
1292 FW_OPT_UEVENT | FW_OPT_FALLBACK |
1293 FW_OPT_NOCACHE);
1294 module_put(THIS_MODULE);
1295 return ret;
1296}
1297EXPORT_SYMBOL(request_firmware_into_buf);
1298
1299/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001301 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001303void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
1305 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001306 if (!fw_is_builtin_firmware(fw))
1307 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 kfree(fw);
1309 }
1310}
Daniel Mackf4945132013-05-23 22:17:18 +02001311EXPORT_SYMBOL(release_firmware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313/* Async support */
1314struct firmware_work {
1315 struct work_struct work;
1316 struct module *module;
1317 const char *name;
1318 struct device *device;
1319 void *context;
1320 void (*cont)(const struct firmware *fw, void *context);
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001321 unsigned int opt_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322};
1323
Stephen Boyda36cf842012-03-28 23:31:00 +02001324static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Stephen Boyda36cf842012-03-28 23:31:00 +02001326 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 const struct firmware *fw;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001328
Stephen Boyda36cf842012-03-28 23:31:00 +02001329 fw_work = container_of(work, struct firmware_work, work);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001330
Stephen Boyda098ecd2016-08-02 14:04:28 -07001331 _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001332 fw_work->opt_flags);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001333 fw_work->cont(fw, fw_work->context);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001334 put_device(fw_work->device); /* taken in request_firmware_nowait() */
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 module_put(fw_work->module);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001337 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
1341/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001342 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001343 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001344 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001345 * is non-zero else the firmware copy must be done manually.
1346 * @name: name of firmware file
1347 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001348 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001349 * @context: will be passed over to @cont, and
1350 * @fw may be %NULL if firmware request fails.
1351 * @cont: function will be called asynchronously when the firmware
1352 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001354 * Caller must hold the reference count of @device.
1355 *
Ming Lei6f21a622012-08-04 12:01:24 +08001356 * Asynchronous variant of request_firmware() for user contexts:
1357 * - sleep for as small periods as possible since it may
1358 * increase kernel boot time of built-in device drivers
1359 * requesting firmware in their ->probe() methods, if
1360 * @gfp is GFP_KERNEL.
1361 *
1362 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 **/
1364int
1365request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001366 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001367 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 void (*cont)(const struct firmware *fw, void *context))
1369{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001370 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Andrei Opreaea310032015-03-08 12:41:15 +02001372 fw_work = kzalloc(sizeof(struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 if (!fw_work)
1374 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001375
1376 fw_work->module = module;
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001377 fw_work->name = kstrdup_const(name, gfp);
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001378 if (!fw_work->name) {
1379 kfree(fw_work);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001380 return -ENOMEM;
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001381 }
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001382 fw_work->device = device;
1383 fw_work->context = context;
1384 fw_work->cont = cont;
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001385 fw_work->opt_flags = FW_OPT_NOWAIT | FW_OPT_FALLBACK |
Takashi Iwai5a1379e2014-06-04 17:48:15 +02001386 (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if (!try_module_get(module)) {
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001389 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 kfree(fw_work);
1391 return -EFAULT;
1392 }
1393
Ming Lei0cfc1e12012-08-04 12:01:23 +08001394 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001395 INIT_WORK(&fw_work->work, request_firmware_work_func);
1396 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return 0;
1398}
Daniel Mackf4945132013-05-23 22:17:18 +02001399EXPORT_SYMBOL(request_firmware_nowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Tim Murray57c7ee12016-10-20 13:53:58 -07001401#ifdef CONFIG_FW_CACHE
Ming Lei90f890812013-06-20 12:30:16 +08001402static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1403
Ming Lei2887b392012-08-04 12:01:22 +08001404/**
1405 * cache_firmware - cache one firmware image in kernel memory space
1406 * @fw_name: the firmware image name
1407 *
1408 * Cache firmware in kernel memory so that drivers can use it when
1409 * system isn't ready for them to request firmware image from userspace.
1410 * Once it returns successfully, driver can use request_firmware or its
1411 * nowait version to get the cached firmware without any interacting
1412 * with userspace
1413 *
1414 * Return 0 if the firmware image has been cached successfully
1415 * Return !0 otherwise
1416 *
1417 */
Ming Lei93232e42013-06-06 20:01:47 +08001418static int cache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001419{
1420 int ret;
1421 const struct firmware *fw;
1422
1423 pr_debug("%s: %s\n", __func__, fw_name);
1424
1425 ret = request_firmware(&fw, fw_name, NULL);
1426 if (!ret)
1427 kfree(fw);
1428
1429 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1430
1431 return ret;
1432}
1433
Ming Lei6a2c1232013-06-26 09:28:17 +08001434static struct firmware_buf *fw_lookup_buf(const char *fw_name)
1435{
1436 struct firmware_buf *tmp;
1437 struct firmware_cache *fwc = &fw_cache;
1438
1439 spin_lock(&fwc->lock);
1440 tmp = __fw_lookup_buf(fw_name);
1441 spin_unlock(&fwc->lock);
1442
1443 return tmp;
1444}
1445
Ming Lei2887b392012-08-04 12:01:22 +08001446/**
1447 * uncache_firmware - remove one cached firmware image
1448 * @fw_name: the firmware image name
1449 *
1450 * Uncache one firmware image which has been cached successfully
1451 * before.
1452 *
1453 * Return 0 if the firmware cache has been removed successfully
1454 * Return !0 otherwise
1455 *
1456 */
Ming Lei93232e42013-06-06 20:01:47 +08001457static int uncache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001458{
1459 struct firmware_buf *buf;
1460 struct firmware fw;
1461
1462 pr_debug("%s: %s\n", __func__, fw_name);
1463
Stephen Boyda098ecd2016-08-02 14:04:28 -07001464 if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0))
Ming Lei2887b392012-08-04 12:01:22 +08001465 return 0;
1466
1467 buf = fw_lookup_buf(fw_name);
1468 if (buf) {
1469 fw_free_buf(buf);
1470 return 0;
1471 }
1472
1473 return -EINVAL;
1474}
1475
Ming Lei37276a52012-08-04 12:01:27 +08001476static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1477{
1478 struct fw_cache_entry *fce;
1479
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001480 fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
Ming Lei37276a52012-08-04 12:01:27 +08001481 if (!fce)
1482 goto exit;
1483
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001484 fce->name = kstrdup_const(name, GFP_ATOMIC);
1485 if (!fce->name) {
1486 kfree(fce);
1487 fce = NULL;
1488 goto exit;
1489 }
Ming Lei37276a52012-08-04 12:01:27 +08001490exit:
1491 return fce;
1492}
1493
Ming Lei373304f2012-10-09 12:01:01 +08001494static int __fw_entry_found(const char *name)
1495{
1496 struct firmware_cache *fwc = &fw_cache;
1497 struct fw_cache_entry *fce;
1498
1499 list_for_each_entry(fce, &fwc->fw_names, list) {
1500 if (!strcmp(fce->name, name))
1501 return 1;
1502 }
1503 return 0;
1504}
1505
Ming Leiac39b3e2012-08-20 19:04:16 +08001506static int fw_cache_piggyback_on_request(const char *name)
1507{
1508 struct firmware_cache *fwc = &fw_cache;
1509 struct fw_cache_entry *fce;
1510 int ret = 0;
1511
1512 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001513 if (__fw_entry_found(name))
1514 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001515
1516 fce = alloc_fw_cache_entry(name);
1517 if (fce) {
1518 ret = 1;
1519 list_add(&fce->list, &fwc->fw_names);
1520 pr_debug("%s: fw: %s\n", __func__, name);
1521 }
1522found:
1523 spin_unlock(&fwc->name_lock);
1524 return ret;
1525}
1526
Ming Lei37276a52012-08-04 12:01:27 +08001527static void free_fw_cache_entry(struct fw_cache_entry *fce)
1528{
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001529 kfree_const(fce->name);
Ming Lei37276a52012-08-04 12:01:27 +08001530 kfree(fce);
1531}
1532
1533static void __async_dev_cache_fw_image(void *fw_entry,
1534 async_cookie_t cookie)
1535{
1536 struct fw_cache_entry *fce = fw_entry;
1537 struct firmware_cache *fwc = &fw_cache;
1538 int ret;
1539
1540 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001541 if (ret) {
1542 spin_lock(&fwc->name_lock);
1543 list_del(&fce->list);
1544 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001545
Ming Leiac39b3e2012-08-20 19:04:16 +08001546 free_fw_cache_entry(fce);
1547 }
Ming Lei37276a52012-08-04 12:01:27 +08001548}
1549
1550/* called with dev->devres_lock held */
1551static void dev_create_fw_entry(struct device *dev, void *res,
1552 void *data)
1553{
1554 struct fw_name_devm *fwn = res;
1555 const char *fw_name = fwn->name;
1556 struct list_head *head = data;
1557 struct fw_cache_entry *fce;
1558
1559 fce = alloc_fw_cache_entry(fw_name);
1560 if (fce)
1561 list_add(&fce->list, head);
1562}
1563
1564static int devm_name_match(struct device *dev, void *res,
1565 void *match_data)
1566{
1567 struct fw_name_devm *fwn = res;
1568 return (fwn->magic == (unsigned long)match_data);
1569}
1570
Ming Leiab6dd8e2012-08-17 22:07:00 +08001571static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001572{
1573 LIST_HEAD(todo);
1574 struct fw_cache_entry *fce;
1575 struct fw_cache_entry *fce_next;
1576 struct firmware_cache *fwc = &fw_cache;
1577
1578 devres_for_each_res(dev, fw_name_devm_release,
1579 devm_name_match, &fw_cache,
1580 dev_create_fw_entry, &todo);
1581
1582 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1583 list_del(&fce->list);
1584
1585 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001586 /* only one cache entry for one firmware */
1587 if (!__fw_entry_found(fce->name)) {
Ming Lei373304f2012-10-09 12:01:01 +08001588 list_add(&fce->list, &fwc->fw_names);
1589 } else {
1590 free_fw_cache_entry(fce);
1591 fce = NULL;
1592 }
Ming Lei37276a52012-08-04 12:01:27 +08001593 spin_unlock(&fwc->name_lock);
1594
Ming Lei373304f2012-10-09 12:01:01 +08001595 if (fce)
Ming Leid28d3882012-10-09 12:01:04 +08001596 async_schedule_domain(__async_dev_cache_fw_image,
1597 (void *)fce,
1598 &fw_cache_domain);
Ming Lei37276a52012-08-04 12:01:27 +08001599 }
1600}
1601
1602static void __device_uncache_fw_images(void)
1603{
1604 struct firmware_cache *fwc = &fw_cache;
1605 struct fw_cache_entry *fce;
1606
1607 spin_lock(&fwc->name_lock);
1608 while (!list_empty(&fwc->fw_names)) {
1609 fce = list_entry(fwc->fw_names.next,
1610 struct fw_cache_entry, list);
1611 list_del(&fce->list);
1612 spin_unlock(&fwc->name_lock);
1613
1614 uncache_firmware(fce->name);
1615 free_fw_cache_entry(fce);
1616
1617 spin_lock(&fwc->name_lock);
1618 }
1619 spin_unlock(&fwc->name_lock);
1620}
1621
1622/**
1623 * device_cache_fw_images - cache devices' firmware
1624 *
1625 * If one device called request_firmware or its nowait version
1626 * successfully before, the firmware names are recored into the
1627 * device's devres link list, so device_cache_fw_images can call
1628 * cache_firmware() to cache these firmwares for the device,
1629 * then the device driver can load its firmwares easily at
1630 * time when system is not ready to complete loading firmware.
1631 */
1632static void device_cache_fw_images(void)
1633{
1634 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001635 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001636 DEFINE_WAIT(wait);
1637
1638 pr_debug("%s\n", __func__);
1639
Ming Lei373304f2012-10-09 12:01:01 +08001640 /* cancel uncache work */
1641 cancel_delayed_work_sync(&fwc->work);
1642
Ming Leiffe53f62012-08-04 12:01:28 +08001643 /*
1644 * use small loading timeout for caching devices' firmware
1645 * because all these firmware images have been loaded
1646 * successfully at lease once, also system is ready for
1647 * completing firmware loading now. The maximum size of
1648 * firmware in current distributions is about 2M bytes,
1649 * so 10 secs should be enough.
1650 */
1651 old_timeout = loading_timeout;
1652 loading_timeout = 10;
1653
Ming Leiac39b3e2012-08-20 19:04:16 +08001654 mutex_lock(&fw_lock);
1655 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001656 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001657 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001658
1659 /* wait for completion of caching firmware for all devices */
Ming Leid28d3882012-10-09 12:01:04 +08001660 async_synchronize_full_domain(&fw_cache_domain);
Ming Leiffe53f62012-08-04 12:01:28 +08001661
1662 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001663}
1664
1665/**
1666 * device_uncache_fw_images - uncache devices' firmware
1667 *
1668 * uncache all firmwares which have been cached successfully
1669 * by device_uncache_fw_images earlier
1670 */
1671static void device_uncache_fw_images(void)
1672{
1673 pr_debug("%s\n", __func__);
1674 __device_uncache_fw_images();
1675}
1676
1677static void device_uncache_fw_images_work(struct work_struct *work)
1678{
1679 device_uncache_fw_images();
1680}
1681
1682/**
1683 * device_uncache_fw_images_delay - uncache devices firmwares
1684 * @delay: number of milliseconds to delay uncache device firmwares
1685 *
1686 * uncache all devices's firmwares which has been cached successfully
1687 * by device_cache_fw_images after @delay milliseconds.
1688 */
1689static void device_uncache_fw_images_delay(unsigned long delay)
1690{
Shaibal Duttabce66182014-01-31 15:44:58 -08001691 queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
1692 msecs_to_jiffies(delay));
Ming Lei37276a52012-08-04 12:01:27 +08001693}
1694
Ming Lei07646d92012-08-04 12:01:29 +08001695static int fw_pm_notify(struct notifier_block *notify_block,
1696 unsigned long mode, void *unused)
1697{
1698 switch (mode) {
1699 case PM_HIBERNATION_PREPARE:
1700 case PM_SUSPEND_PREPARE:
Sebastian Capellaf8d5b9e2014-02-18 17:52:08 -08001701 case PM_RESTORE_PREPARE:
Ming Leiaf5bc112013-05-27 18:30:04 +08001702 kill_requests_without_uevent();
Ming Lei07646d92012-08-04 12:01:29 +08001703 device_cache_fw_images();
1704 break;
1705
1706 case PM_POST_SUSPEND:
1707 case PM_POST_HIBERNATION:
1708 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001709 /*
1710 * In case that system sleep failed and syscore_suspend is
1711 * not called.
1712 */
1713 mutex_lock(&fw_lock);
1714 fw_cache.state = FW_LOADER_NO_CACHE;
1715 mutex_unlock(&fw_lock);
1716
Ming Lei07646d92012-08-04 12:01:29 +08001717 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1718 break;
1719 }
1720
1721 return 0;
1722}
Ming Lei07646d92012-08-04 12:01:29 +08001723
Ming Leiac39b3e2012-08-20 19:04:16 +08001724/* stop caching firmware once syscore_suspend is reached */
1725static int fw_suspend(void)
1726{
1727 fw_cache.state = FW_LOADER_NO_CACHE;
1728 return 0;
1729}
1730
1731static struct syscore_ops fw_syscore_ops = {
1732 .suspend = fw_suspend,
1733};
Ming Leicfe016b2012-09-08 17:32:30 +08001734#else
1735static int fw_cache_piggyback_on_request(const char *name)
1736{
1737 return 0;
1738}
1739#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001740
Ming Lei37276a52012-08-04 12:01:27 +08001741static void __init fw_cache_init(void)
1742{
1743 spin_lock_init(&fw_cache.lock);
1744 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001745 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001746
Tim Murray57c7ee12016-10-20 13:53:58 -07001747#ifdef CONFIG_FW_CACHE
Ming Lei37276a52012-08-04 12:01:27 +08001748 spin_lock_init(&fw_cache.name_lock);
1749 INIT_LIST_HEAD(&fw_cache.fw_names);
Ming Lei37276a52012-08-04 12:01:27 +08001750
Ming Lei37276a52012-08-04 12:01:27 +08001751 INIT_DELAYED_WORK(&fw_cache.work,
1752 device_uncache_fw_images_work);
Ming Lei07646d92012-08-04 12:01:29 +08001753
1754 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1755 register_pm_notifier(&fw_cache.pm_notify);
Ming Leiac39b3e2012-08-20 19:04:16 +08001756
1757 register_syscore_ops(&fw_syscore_ops);
Ming Leicfe016b2012-09-08 17:32:30 +08001758#endif
Ming Lei37276a52012-08-04 12:01:27 +08001759}
1760
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001761static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762{
Ming Lei1f2b7952012-08-04 12:01:21 +08001763 fw_cache_init();
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001764#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaife304142013-05-22 18:28:38 +02001765 register_reboot_notifier(&fw_shutdown_nb);
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001766 return class_register(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001767#else
1768 return 0;
1769#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001771
1772static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
Tim Murray57c7ee12016-10-20 13:53:58 -07001774#ifdef CONFIG_FW_CACHE
Ming Leiac39b3e2012-08-20 19:04:16 +08001775 unregister_syscore_ops(&fw_syscore_ops);
Ming Lei07646d92012-08-04 12:01:29 +08001776 unregister_pm_notifier(&fw_cache.pm_notify);
Ming Leicfe016b2012-09-08 17:32:30 +08001777#endif
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001778#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaife304142013-05-22 18:28:38 +02001779 unregister_reboot_notifier(&fw_shutdown_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 class_unregister(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001781#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782}
1783
Shaohua Lia30a6a22006-09-27 01:50:52 -07001784fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785module_exit(firmware_class_exit);