blob: 65797742c6b62519304c2d720c98b9f85ae06d25 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * firmware_class.c - Multi purpose firmware loading support
3 *
Markus Rechberger87d37a42007-06-04 18:45:44 +02004 * Copyright (c) 2003 Manuel Estrada Sainz
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Please see Documentation/firmware_class/ for more information.
7 *
8 */
9
Randy.Dunlapc59ede72006-01-11 12:17:46 -080010#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/device.h>
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/timer.h>
15#include <linux/vmalloc.h>
16#include <linux/interrupt.h>
17#include <linux/bitops.h>
Laura Garciacad1e552006-05-23 23:22:38 +020018#include <linux/mutex.h>
Stephen Boyda36cf842012-03-28 23:31:00 +020019#include <linux/workqueue.h>
David Woodhouse6e03a202009-04-09 22:04:07 -070020#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/firmware.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Stephen Boyda36cf842012-03-28 23:31:00 +020023#include <linux/sched.h>
Linus Torvaldsabb139e2012-10-03 15:58:32 -070024#include <linux/file.h>
Ming Lei1f2b7952012-08-04 12:01:21 +080025#include <linux/list.h>
Ming Lei37276a52012-08-04 12:01:27 +080026#include <linux/async.h>
27#include <linux/pm.h>
Ming Lei07646d92012-08-04 12:01:29 +080028#include <linux/suspend.h>
Ming Leiac39b3e2012-08-20 19:04:16 +080029#include <linux/syscore_ops.h>
Takashi Iwaife304142013-05-22 18:28:38 +020030#include <linux/reboot.h>
Ming Lei37276a52012-08-04 12:01:27 +080031
Linus Torvaldsabb139e2012-10-03 15:58:32 -070032#include <generated/utsrelease.h>
33
Ming Lei37276a52012-08-04 12:01:27 +080034#include "base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Markus Rechberger87d37a42007-06-04 18:45:44 +020036MODULE_AUTHOR("Manuel Estrada Sainz");
Linus Torvalds1da177e2005-04-16 15:20:36 -070037MODULE_DESCRIPTION("Multi purpose firmware loading support");
38MODULE_LICENSE("GPL");
39
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080040/* Builtin firmware support */
41
42#ifdef CONFIG_FW_LOADER
43
44extern struct builtin_fw __start_builtin_fw[];
45extern struct builtin_fw __end_builtin_fw[];
46
47static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
48{
49 struct builtin_fw *b_fw;
50
51 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
52 if (strcmp(name, b_fw->name) == 0) {
53 fw->size = b_fw->size;
54 fw->data = b_fw->data;
55 return true;
56 }
57 }
58
59 return false;
60}
61
62static bool fw_is_builtin_firmware(const struct firmware *fw)
63{
64 struct builtin_fw *b_fw;
65
66 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
67 if (fw->data == b_fw->data)
68 return true;
69
70 return false;
71}
72
73#else /* Module case - no builtin firmware support */
74
75static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
76{
77 return false;
78}
79
80static inline bool fw_is_builtin_firmware(const struct firmware *fw)
81{
82 return false;
83}
84#endif
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086enum {
87 FW_STATUS_LOADING,
88 FW_STATUS_DONE,
89 FW_STATUS_ABORT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090};
91
Dave Jones2f651682007-01-25 15:56:15 -050092static int loading_timeout = 60; /* In seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +020094static inline long firmware_loading_timeout(void)
95{
96 return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
97}
98
Takashi Iwai14c4bae2013-12-02 15:38:18 +010099/* firmware behavior options */
100#define FW_OPT_UEVENT (1U << 0)
101#define FW_OPT_NOWAIT (1U << 1)
102#define FW_OPT_FALLBACK (1U << 2)
103
Ming Lei1f2b7952012-08-04 12:01:21 +0800104struct firmware_cache {
105 /* firmware_buf instance will be added into the below list */
106 spinlock_t lock;
107 struct list_head head;
Ming Leicfe016b2012-09-08 17:32:30 +0800108 int state;
Ming Lei37276a52012-08-04 12:01:27 +0800109
Ming Leicfe016b2012-09-08 17:32:30 +0800110#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +0800111 /*
112 * Names of firmware images which have been cached successfully
113 * will be added into the below list so that device uncache
114 * helper can trace which firmware images have been cached
115 * before.
116 */
117 spinlock_t name_lock;
118 struct list_head fw_names;
119
Ming Lei37276a52012-08-04 12:01:27 +0800120 struct delayed_work work;
Ming Lei07646d92012-08-04 12:01:29 +0800121
122 struct notifier_block pm_notify;
Ming Leicfe016b2012-09-08 17:32:30 +0800123#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800124};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Ming Lei12446912012-08-04 12:01:20 +0800126struct firmware_buf {
Ming Lei1f2b7952012-08-04 12:01:21 +0800127 struct kref ref;
128 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 struct completion completion;
Ming Lei1f2b7952012-08-04 12:01:21 +0800130 struct firmware_cache *fwc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 unsigned long status;
Ming Lei65710cb2012-08-04 12:01:16 +0800132 void *data;
133 size_t size;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100134#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100135 bool is_paged_buf;
Ming Leiaf5bc112013-05-27 18:30:04 +0800136 bool need_uevent;
David Woodhouse6e03a202009-04-09 22:04:07 -0700137 struct page **pages;
138 int nr_pages;
139 int page_array_size;
Takashi Iwaife304142013-05-22 18:28:38 +0200140 struct list_head pending_list;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100141#endif
Dmitry Torokhove1771232010-03-13 23:49:23 -0800142 char fw_id[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
144
Ming Lei37276a52012-08-04 12:01:27 +0800145struct fw_cache_entry {
146 struct list_head list;
147 char name[];
148};
149
Ming Leif531f05a2012-08-04 12:01:25 +0800150struct fw_name_devm {
151 unsigned long magic;
152 char name[];
153};
154
Ming Lei1f2b7952012-08-04 12:01:21 +0800155#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
156
Ming Leiac39b3e2012-08-20 19:04:16 +0800157#define FW_LOADER_NO_CACHE 0
158#define FW_LOADER_START_CACHE 1
159
160static int fw_cache_piggyback_on_request(const char *name);
161
Ming Lei1f2b7952012-08-04 12:01:21 +0800162/* fw_lock could be moved to 'struct firmware_priv' but since it is just
163 * guarding for corner cases a global lock should be OK */
164static DEFINE_MUTEX(fw_lock);
165
166static struct firmware_cache fw_cache;
167
168static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
169 struct firmware_cache *fwc)
170{
171 struct firmware_buf *buf;
172
173 buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
174
175 if (!buf)
176 return buf;
177
178 kref_init(&buf->ref);
179 strcpy(buf->fw_id, fw_name);
180 buf->fwc = fwc;
181 init_completion(&buf->completion);
Takashi Iwaife304142013-05-22 18:28:38 +0200182#ifdef CONFIG_FW_LOADER_USER_HELPER
183 INIT_LIST_HEAD(&buf->pending_list);
184#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800185
186 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
187
188 return buf;
189}
190
Ming Lei2887b392012-08-04 12:01:22 +0800191static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
192{
193 struct firmware_buf *tmp;
194 struct firmware_cache *fwc = &fw_cache;
195
196 list_for_each_entry(tmp, &fwc->head, list)
197 if (!strcmp(tmp->fw_id, fw_name))
198 return tmp;
199 return NULL;
200}
201
Ming Lei1f2b7952012-08-04 12:01:21 +0800202static int fw_lookup_and_allocate_buf(const char *fw_name,
203 struct firmware_cache *fwc,
204 struct firmware_buf **buf)
205{
206 struct firmware_buf *tmp;
207
208 spin_lock(&fwc->lock);
Ming Lei2887b392012-08-04 12:01:22 +0800209 tmp = __fw_lookup_buf(fw_name);
210 if (tmp) {
211 kref_get(&tmp->ref);
212 spin_unlock(&fwc->lock);
213 *buf = tmp;
214 return 1;
215 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800216 tmp = __allocate_fw_buf(fw_name, fwc);
217 if (tmp)
218 list_add(&tmp->list, &fwc->head);
219 spin_unlock(&fwc->lock);
220
221 *buf = tmp;
222
223 return tmp ? 0 : -ENOMEM;
224}
225
226static void __fw_free_buf(struct kref *ref)
227{
228 struct firmware_buf *buf = to_fwbuf(ref);
229 struct firmware_cache *fwc = buf->fwc;
Ming Lei1f2b7952012-08-04 12:01:21 +0800230
231 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
232 __func__, buf->fw_id, buf, buf->data,
233 (unsigned int)buf->size);
234
Ming Lei1f2b7952012-08-04 12:01:21 +0800235 list_del(&buf->list);
236 spin_unlock(&fwc->lock);
237
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100238#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100239 if (buf->is_paged_buf) {
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100240 int i;
Ming Lei746058f2012-10-09 12:01:03 +0800241 vunmap(buf->data);
242 for (i = 0; i < buf->nr_pages; i++)
243 __free_page(buf->pages[i]);
244 kfree(buf->pages);
245 } else
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100246#endif
Ming Lei746058f2012-10-09 12:01:03 +0800247 vfree(buf->data);
Ming Lei1f2b7952012-08-04 12:01:21 +0800248 kfree(buf);
249}
250
251static void fw_free_buf(struct firmware_buf *buf)
252{
Chuansheng Liubd9eb7f2012-11-10 01:27:22 +0800253 struct firmware_cache *fwc = buf->fwc;
254 spin_lock(&fwc->lock);
255 if (!kref_put(&buf->ref, __fw_free_buf))
256 spin_unlock(&fwc->lock);
Ming Lei1f2b7952012-08-04 12:01:21 +0800257}
258
Ming Lei746058f2012-10-09 12:01:03 +0800259/* direct firmware loading support */
Ming Lei27602842012-11-03 17:47:58 +0800260static char fw_path_para[256];
261static const char * const fw_path[] = {
262 fw_path_para,
Ming Lei746058f2012-10-09 12:01:03 +0800263 "/lib/firmware/updates/" UTS_RELEASE,
264 "/lib/firmware/updates",
265 "/lib/firmware/" UTS_RELEASE,
266 "/lib/firmware"
267};
268
Ming Lei27602842012-11-03 17:47:58 +0800269/*
270 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
271 * from kernel command line because firmware_class is generally built in
272 * kernel instead of module.
273 */
274module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
275MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
276
Ming Lei746058f2012-10-09 12:01:03 +0800277/* Don't inline this: 'struct kstat' is biggish */
Cesar Eduardo Barros60dac5e2012-10-27 20:37:10 -0200278static noinline_for_stack long fw_file_size(struct file *file)
Ming Lei746058f2012-10-09 12:01:03 +0800279{
280 struct kstat st;
Al Viro3dadecce2013-01-24 02:18:08 -0500281 if (vfs_getattr(&file->f_path, &st))
Ming Lei746058f2012-10-09 12:01:03 +0800282 return -1;
283 if (!S_ISREG(st.mode))
284 return -1;
285 if (st.size != (long)st.size)
286 return -1;
287 return st.size;
288}
289
Neil Horman3e358ac2013-09-06 15:36:08 -0400290static int fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf)
Ming Lei746058f2012-10-09 12:01:03 +0800291{
292 long size;
293 char *buf;
Neil Horman3e358ac2013-09-06 15:36:08 -0400294 int rc;
Ming Lei746058f2012-10-09 12:01:03 +0800295
296 size = fw_file_size(file);
Luciano Coelho4adf07f2013-01-15 10:43:43 +0200297 if (size <= 0)
Neil Horman3e358ac2013-09-06 15:36:08 -0400298 return -EINVAL;
Ming Lei746058f2012-10-09 12:01:03 +0800299 buf = vmalloc(size);
300 if (!buf)
Neil Horman3e358ac2013-09-06 15:36:08 -0400301 return -ENOMEM;
302 rc = kernel_read(file, 0, buf, size);
303 if (rc != size) {
304 if (rc > 0)
305 rc = -EIO;
Ming Lei746058f2012-10-09 12:01:03 +0800306 vfree(buf);
Neil Horman3e358ac2013-09-06 15:36:08 -0400307 return rc;
Ming Lei746058f2012-10-09 12:01:03 +0800308 }
309 fw_buf->data = buf;
310 fw_buf->size = size;
Neil Horman3e358ac2013-09-06 15:36:08 -0400311 return 0;
Ming Lei746058f2012-10-09 12:01:03 +0800312}
313
Neil Horman3e358ac2013-09-06 15:36:08 -0400314static int fw_get_filesystem_firmware(struct device *device,
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100315 struct firmware_buf *buf)
Ming Lei746058f2012-10-09 12:01:03 +0800316{
317 int i;
Neil Horman3e358ac2013-09-06 15:36:08 -0400318 int rc = -ENOENT;
Ming Lei746058f2012-10-09 12:01:03 +0800319 char *path = __getname();
320
321 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
322 struct file *file;
Ming Lei27602842012-11-03 17:47:58 +0800323
324 /* skip the unset customized path */
325 if (!fw_path[i][0])
326 continue;
327
Ming Lei746058f2012-10-09 12:01:03 +0800328 snprintf(path, PATH_MAX, "%s/%s", fw_path[i], buf->fw_id);
329
330 file = filp_open(path, O_RDONLY, 0);
331 if (IS_ERR(file))
332 continue;
Neil Horman3e358ac2013-09-06 15:36:08 -0400333 rc = fw_read_file_contents(file, buf);
Ming Lei746058f2012-10-09 12:01:03 +0800334 fput(file);
Neil Horman3e358ac2013-09-06 15:36:08 -0400335 if (rc)
336 dev_warn(device, "firmware, attempted to load %s, but failed with error %d\n",
337 path, rc);
338 else
Ming Lei746058f2012-10-09 12:01:03 +0800339 break;
340 }
341 __putname(path);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100342
Neil Horman3e358ac2013-09-06 15:36:08 -0400343 if (!rc) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100344 dev_dbg(device, "firmware: direct-loading firmware %s\n",
345 buf->fw_id);
346 mutex_lock(&fw_lock);
347 set_bit(FW_STATUS_DONE, &buf->status);
348 complete_all(&buf->completion);
349 mutex_unlock(&fw_lock);
350 }
351
Neil Horman3e358ac2013-09-06 15:36:08 -0400352 return rc;
Ming Lei746058f2012-10-09 12:01:03 +0800353}
354
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100355/* firmware holds the ownership of pages */
356static void firmware_free_data(const struct firmware *fw)
357{
358 /* Loaded directly? */
359 if (!fw->priv) {
360 vfree(fw->data);
361 return;
362 }
363 fw_free_buf(fw->priv);
364}
365
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100366/* store the pages buffer info firmware from buf */
367static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
368{
369 fw->priv = buf;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100370#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100371 fw->pages = buf->pages;
372#endif
373 fw->size = buf->size;
374 fw->data = buf->data;
375
376 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
377 __func__, buf->fw_id, buf, buf->data,
378 (unsigned int)buf->size);
379}
380
381#ifdef CONFIG_PM_SLEEP
382static void fw_name_devm_release(struct device *dev, void *res)
383{
384 struct fw_name_devm *fwn = res;
385
386 if (fwn->magic == (unsigned long)&fw_cache)
387 pr_debug("%s: fw_name-%s devm-%p released\n",
388 __func__, fwn->name, res);
389}
390
391static int fw_devm_match(struct device *dev, void *res,
392 void *match_data)
393{
394 struct fw_name_devm *fwn = res;
395
396 return (fwn->magic == (unsigned long)&fw_cache) &&
397 !strcmp(fwn->name, match_data);
398}
399
400static struct fw_name_devm *fw_find_devm_name(struct device *dev,
401 const char *name)
402{
403 struct fw_name_devm *fwn;
404
405 fwn = devres_find(dev, fw_name_devm_release,
406 fw_devm_match, (void *)name);
407 return fwn;
408}
409
410/* add firmware name into devres list */
411static int fw_add_devm_name(struct device *dev, const char *name)
412{
413 struct fw_name_devm *fwn;
414
415 fwn = fw_find_devm_name(dev, name);
416 if (fwn)
417 return 1;
418
419 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
420 strlen(name) + 1, GFP_KERNEL);
421 if (!fwn)
422 return -ENOMEM;
423
424 fwn->magic = (unsigned long)&fw_cache;
425 strcpy(fwn->name, name);
426 devres_add(dev, fwn);
427
428 return 0;
429}
430#else
431static int fw_add_devm_name(struct device *dev, const char *name)
432{
433 return 0;
434}
435#endif
436
437
438/*
439 * user-mode helper code
440 */
441#ifdef CONFIG_FW_LOADER_USER_HELPER
442struct firmware_priv {
443 struct delayed_work timeout_work;
444 bool nowait;
445 struct device dev;
446 struct firmware_buf *buf;
447 struct firmware *fw;
448};
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100449
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700450static struct firmware_priv *to_firmware_priv(struct device *dev)
451{
452 return container_of(dev, struct firmware_priv, dev);
453}
454
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700455static void __fw_load_abort(struct firmware_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Ming Lei87597932013-06-15 16:36:38 +0800457 /*
458 * There is a small window in which user can write to 'loading'
459 * between loading done and disappearance of 'loading'
460 */
461 if (test_bit(FW_STATUS_DONE, &buf->status))
462 return;
463
Takashi Iwaife304142013-05-22 18:28:38 +0200464 list_del_init(&buf->pending_list);
Ming Lei12446912012-08-04 12:01:20 +0800465 set_bit(FW_STATUS_ABORT, &buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800466 complete_all(&buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700469static void fw_load_abort(struct firmware_priv *fw_priv)
470{
471 struct firmware_buf *buf = fw_priv->buf;
472
473 __fw_load_abort(buf);
Ming Lei87597932013-06-15 16:36:38 +0800474
475 /* avoid user action after loading abort */
476 fw_priv->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Takashi Iwai807be032013-01-31 11:13:57 +0100479#define is_fw_load_aborted(buf) \
480 test_bit(FW_STATUS_ABORT, &(buf)->status)
481
Takashi Iwaife304142013-05-22 18:28:38 +0200482static LIST_HEAD(pending_fw_head);
483
484/* reboot notifier for avoid deadlock with usermode_lock */
485static int fw_shutdown_notify(struct notifier_block *unused1,
486 unsigned long unused2, void *unused3)
487{
488 mutex_lock(&fw_lock);
489 while (!list_empty(&pending_fw_head))
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700490 __fw_load_abort(list_first_entry(&pending_fw_head,
Takashi Iwaife304142013-05-22 18:28:38 +0200491 struct firmware_buf,
492 pending_list));
493 mutex_unlock(&fw_lock);
494 return NOTIFY_DONE;
495}
496
497static struct notifier_block fw_shutdown_nb = {
498 .notifier_call = fw_shutdown_notify,
499};
500
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700501static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
502 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 return sprintf(buf, "%d\n", loading_timeout);
505}
506
507/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800508 * firmware_timeout_store - set number of seconds to wait for firmware
509 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800510 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800511 * @buf: buffer to scan for timeout value
512 * @count: number of bytes in @buf
513 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800515 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 * firmware will be provided.
517 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800518 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 **/
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700520static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
521 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700524 if (loading_timeout < 0)
525 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return count;
528}
529
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800530static struct class_attribute firmware_class_attrs[] = {
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700531 __ATTR_RW(timeout),
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800532 __ATTR_NULL
533};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800535static void fw_dev_release(struct device *dev)
536{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700537 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800538
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800539 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800540}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Kay Sievers7eff2e72007-08-14 15:15:12 +0200542static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700544 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Ming Lei12446912012-08-04 12:01:20 +0800546 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200548 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700549 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200550 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
551 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 return 0;
554}
555
Adrian Bunk1b81d662006-05-20 15:00:16 -0700556static struct class firmware_class = {
557 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800558 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700559 .dev_uevent = firmware_uevent,
560 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700561};
562
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700563static ssize_t firmware_loading_show(struct device *dev,
564 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700566 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800567 int loading = 0;
568
569 mutex_lock(&fw_lock);
570 if (fw_priv->buf)
571 loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
572 mutex_unlock(&fw_lock);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return sprintf(buf, "%d\n", loading);
575}
576
David Woodhouse6e03a202009-04-09 22:04:07 -0700577/* Some architectures don't have PAGE_KERNEL_RO */
578#ifndef PAGE_KERNEL_RO
579#define PAGE_KERNEL_RO PAGE_KERNEL
580#endif
Ming Lei253c9242012-10-09 12:01:02 +0800581
582/* one pages buffer should be mapped/unmapped only once */
583static int fw_map_pages_buf(struct firmware_buf *buf)
584{
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100585 if (!buf->is_paged_buf)
Ming Lei746058f2012-10-09 12:01:03 +0800586 return 0;
587
Ming Lei253c9242012-10-09 12:01:02 +0800588 if (buf->data)
589 vunmap(buf->data);
590 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
591 if (!buf->data)
592 return -ENOMEM;
593 return 0;
594}
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800597 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700598 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800599 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800600 * @buf: buffer to scan for loading control value
601 * @count: number of bytes in @buf
602 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 * The relevant values are:
604 *
605 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800606 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 * -1: Conclude the load with an error and discard any written data.
608 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700609static ssize_t firmware_loading_store(struct device *dev,
610 struct device_attribute *attr,
611 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700613 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800614 struct firmware_buf *fw_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700616 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Neil Hormaneea915b2012-01-02 15:31:23 -0500618 mutex_lock(&fw_lock);
Ming Lei87597932013-06-15 16:36:38 +0800619 fw_buf = fw_priv->buf;
Ming Lei12446912012-08-04 12:01:20 +0800620 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500621 goto out;
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 switch (loading) {
624 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800625 /* discarding any previous partial load */
Ming Lei12446912012-08-04 12:01:20 +0800626 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
627 for (i = 0; i < fw_buf->nr_pages; i++)
628 __free_page(fw_buf->pages[i]);
629 kfree(fw_buf->pages);
630 fw_buf->pages = NULL;
631 fw_buf->page_array_size = 0;
632 fw_buf->nr_pages = 0;
633 set_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei28eefa72012-08-04 12:01:17 +0800634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 break;
636 case 0:
Ming Lei12446912012-08-04 12:01:20 +0800637 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
638 set_bit(FW_STATUS_DONE, &fw_buf->status);
639 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei253c9242012-10-09 12:01:02 +0800640
641 /*
642 * Several loading requests may be pending on
643 * one same firmware buf, so let all requests
644 * see the mapped 'buf->data' once the loading
645 * is completed.
646 * */
647 fw_map_pages_buf(fw_buf);
Takashi Iwaife304142013-05-22 18:28:38 +0200648 list_del_init(&fw_buf->pending_list);
Ming Lei1f2b7952012-08-04 12:01:21 +0800649 complete_all(&fw_buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 break;
651 }
652 /* fallthrough */
653 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700654 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 /* fallthrough */
656 case -1:
657 fw_load_abort(fw_priv);
658 break;
659 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500660out:
661 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return count;
663}
664
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700665static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700667static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
668 struct bin_attribute *bin_attr,
669 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200671 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700672 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800673 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700674 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Laura Garciacad1e552006-05-23 23:22:38 +0200676 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800677 buf = fw_priv->buf;
678 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 ret_count = -ENODEV;
680 goto out;
681 }
Ming Lei12446912012-08-04 12:01:20 +0800682 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200683 ret_count = 0;
684 goto out;
685 }
Ming Lei12446912012-08-04 12:01:20 +0800686 if (count > buf->size - offset)
687 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700688
689 ret_count = count;
690
691 while (count) {
692 void *page_data;
693 int page_nr = offset >> PAGE_SHIFT;
694 int page_ofs = offset & (PAGE_SIZE-1);
695 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
696
Ming Lei12446912012-08-04 12:01:20 +0800697 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700698
699 memcpy(buffer, page_data + page_ofs, page_cnt);
700
Ming Lei12446912012-08-04 12:01:20 +0800701 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700702 buffer += page_cnt;
703 offset += page_cnt;
704 count -= page_cnt;
705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706out:
Laura Garciacad1e552006-05-23 23:22:38 +0200707 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return ret_count;
709}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800710
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700711static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Ming Lei12446912012-08-04 12:01:20 +0800713 struct firmware_buf *buf = fw_priv->buf;
David Woodhouse6e03a202009-04-09 22:04:07 -0700714 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
David Woodhouse6e03a202009-04-09 22:04:07 -0700716 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800717 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700718 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800719 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700720 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
David Woodhouse6e03a202009-04-09 22:04:07 -0700722 new_pages = kmalloc(new_array_size * sizeof(void *),
723 GFP_KERNEL);
724 if (!new_pages) {
725 fw_load_abort(fw_priv);
726 return -ENOMEM;
727 }
Ming Lei12446912012-08-04 12:01:20 +0800728 memcpy(new_pages, buf->pages,
729 buf->page_array_size * sizeof(void *));
730 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
731 (new_array_size - buf->page_array_size));
732 kfree(buf->pages);
733 buf->pages = new_pages;
734 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700736
Ming Lei12446912012-08-04 12:01:20 +0800737 while (buf->nr_pages < pages_needed) {
738 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700739 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
740
Ming Lei12446912012-08-04 12:01:20 +0800741 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700742 fw_load_abort(fw_priv);
743 return -ENOMEM;
744 }
Ming Lei12446912012-08-04 12:01:20 +0800745 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return 0;
748}
749
750/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800751 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700752 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700753 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700754 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800755 * @buffer: buffer being written
756 * @offset: buffer offset for write in total data store area
757 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800759 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 * the driver as a firmware image.
761 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700762static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
763 struct bin_attribute *bin_attr,
764 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200766 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700767 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800768 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 ssize_t retval;
770
771 if (!capable(CAP_SYS_RAWIO))
772 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700773
Laura Garciacad1e552006-05-23 23:22:38 +0200774 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800775 buf = fw_priv->buf;
776 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 retval = -ENODEV;
778 goto out;
779 }
Ming Lei65710cb2012-08-04 12:01:16 +0800780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 retval = fw_realloc_buffer(fw_priv, offset + count);
782 if (retval)
783 goto out;
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700786
787 while (count) {
788 void *page_data;
789 int page_nr = offset >> PAGE_SHIFT;
790 int page_ofs = offset & (PAGE_SIZE - 1);
791 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
792
Ming Lei12446912012-08-04 12:01:20 +0800793 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700794
795 memcpy(page_data + page_ofs, buffer, page_cnt);
796
Ming Lei12446912012-08-04 12:01:20 +0800797 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700798 buffer += page_cnt;
799 offset += page_cnt;
800 count -= page_cnt;
801 }
802
Ming Lei12446912012-08-04 12:01:20 +0800803 buf->size = max_t(size_t, offset, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804out:
Laura Garciacad1e552006-05-23 23:22:38 +0200805 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return retval;
807}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800808
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700809static struct bin_attribute firmware_attr_data = {
810 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 .size = 0,
812 .read = firmware_data_read,
813 .write = firmware_data_write,
814};
815
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800816static void firmware_class_timeout_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800818 struct firmware_priv *fw_priv = container_of(work,
819 struct firmware_priv, timeout_work.work);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700820
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800821 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 fw_load_abort(fw_priv);
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800823 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700826static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200827fw_create_instance(struct firmware *firmware, const char *fw_name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100828 struct device *device, unsigned int opt_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700830 struct firmware_priv *fw_priv;
831 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Ming Lei12446912012-08-04 12:01:20 +0800833 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700834 if (!fw_priv) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700835 dev_err(device, "%s: kmalloc failed\n", __func__);
Ming Lei12446912012-08-04 12:01:20 +0800836 fw_priv = ERR_PTR(-ENOMEM);
837 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100840 fw_priv->nowait = !!(opt_flags & FW_OPT_NOWAIT);
Ming Lei1f2b7952012-08-04 12:01:21 +0800841 fw_priv->fw = firmware;
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800842 INIT_DELAYED_WORK(&fw_priv->timeout_work,
843 firmware_class_timeout_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700845 f_dev = &fw_priv->dev;
846
847 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800848 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700849 f_dev->parent = device;
850 f_dev->class = &firmware_class;
Ming Lei12446912012-08-04 12:01:20 +0800851exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700852 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100854
855/* load a firmware via user helper */
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100856static int _request_firmware_load(struct firmware_priv *fw_priv,
857 unsigned int opt_flags, long timeout)
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100858{
859 int retval = 0;
860 struct device *f_dev = &fw_priv->dev;
861 struct firmware_buf *buf = fw_priv->buf;
862
863 /* fall back on userspace loading */
864 buf->is_paged_buf = true;
865
866 dev_set_uevent_suppress(f_dev, true);
867
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100868 retval = device_add(f_dev);
869 if (retval) {
870 dev_err(f_dev, "%s: device_register failed\n", __func__);
871 goto err_put_dev;
872 }
873
874 retval = device_create_bin_file(f_dev, &firmware_attr_data);
875 if (retval) {
876 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
877 goto err_del_dev;
878 }
879
Maxime Bizon1eeeef12013-08-29 20:28:13 +0200880 mutex_lock(&fw_lock);
881 list_add(&buf->pending_list, &pending_fw_head);
882 mutex_unlock(&fw_lock);
883
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100884 retval = device_create_file(f_dev, &dev_attr_loading);
885 if (retval) {
Maxime Bizon1eeeef12013-08-29 20:28:13 +0200886 mutex_lock(&fw_lock);
887 list_del_init(&buf->pending_list);
888 mutex_unlock(&fw_lock);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100889 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
890 goto err_del_bin_attr;
891 }
892
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100893 if (opt_flags & FW_OPT_UEVENT) {
Ming Leiaf5bc112013-05-27 18:30:04 +0800894 buf->need_uevent = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100895 dev_set_uevent_suppress(f_dev, false);
896 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
897 if (timeout != MAX_SCHEDULE_TIMEOUT)
898 schedule_delayed_work(&fw_priv->timeout_work, timeout);
899
900 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
901 }
902
903 wait_for_completion(&buf->completion);
904
905 cancel_delayed_work_sync(&fw_priv->timeout_work);
906
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100907 device_remove_file(f_dev, &dev_attr_loading);
908err_del_bin_attr:
909 device_remove_bin_file(f_dev, &firmware_attr_data);
910err_del_dev:
911 device_del(f_dev);
912err_put_dev:
913 put_device(f_dev);
914 return retval;
915}
916
917static int fw_load_from_user_helper(struct firmware *firmware,
918 const char *name, struct device *device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100919 unsigned int opt_flags, long timeout)
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100920{
921 struct firmware_priv *fw_priv;
922
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100923 fw_priv = fw_create_instance(firmware, name, device, opt_flags);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100924 if (IS_ERR(fw_priv))
925 return PTR_ERR(fw_priv);
926
927 fw_priv->buf = firmware->priv;
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100928 return _request_firmware_load(fw_priv, opt_flags, timeout);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100929}
Ming Leiddf1f062013-06-04 10:01:13 +0800930
Ming Lei5b7cb7a2013-06-06 19:52:54 +0800931#ifdef CONFIG_PM_SLEEP
Ming Leiddf1f062013-06-04 10:01:13 +0800932/* kill pending requests without uevent to avoid blocking suspend */
933static void kill_requests_without_uevent(void)
934{
935 struct firmware_buf *buf;
936 struct firmware_buf *next;
937
938 mutex_lock(&fw_lock);
939 list_for_each_entry_safe(buf, next, &pending_fw_head, pending_list) {
940 if (!buf->need_uevent)
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700941 __fw_load_abort(buf);
Ming Leiddf1f062013-06-04 10:01:13 +0800942 }
943 mutex_unlock(&fw_lock);
944}
Ming Lei5b7cb7a2013-06-06 19:52:54 +0800945#endif
Ming Leiddf1f062013-06-04 10:01:13 +0800946
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100947#else /* CONFIG_FW_LOADER_USER_HELPER */
948static inline int
949fw_load_from_user_helper(struct firmware *firmware, const char *name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100950 struct device *device, unsigned int opt_flags,
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100951 long timeout)
952{
953 return -ENOENT;
954}
Takashi Iwai807be032013-01-31 11:13:57 +0100955
956/* No abort during direct loading */
957#define is_fw_load_aborted(buf) false
958
Ming Lei5b7cb7a2013-06-06 19:52:54 +0800959#ifdef CONFIG_PM_SLEEP
Ming Leiddf1f062013-06-04 10:01:13 +0800960static inline void kill_requests_without_uevent(void) { }
Ming Lei5b7cb7a2013-06-06 19:52:54 +0800961#endif
Ming Leiddf1f062013-06-04 10:01:13 +0800962
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100963#endif /* CONFIG_FW_LOADER_USER_HELPER */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Ming Leif531f05a2012-08-04 12:01:25 +0800965
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100966/* wait until the shared firmware_buf becomes ready (or error) */
967static int sync_cached_firmware_buf(struct firmware_buf *buf)
Ming Lei1f2b7952012-08-04 12:01:21 +0800968{
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100969 int ret = 0;
970
971 mutex_lock(&fw_lock);
972 while (!test_bit(FW_STATUS_DONE, &buf->status)) {
Takashi Iwai807be032013-01-31 11:13:57 +0100973 if (is_fw_load_aborted(buf)) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100974 ret = -ENOENT;
975 break;
976 }
977 mutex_unlock(&fw_lock);
978 wait_for_completion(&buf->completion);
979 mutex_lock(&fw_lock);
980 }
981 mutex_unlock(&fw_lock);
982 return ret;
Ming Lei1f2b7952012-08-04 12:01:21 +0800983}
984
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100985/* prepare firmware and firmware_buf structs;
986 * return 0 if a firmware is already assigned, 1 if need to load one,
987 * or a negative error code
988 */
989static int
990_request_firmware_prepare(struct firmware **firmware_p, const char *name,
991 struct device *device)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700992{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +0800994 struct firmware_buf *buf;
995 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Jiri Slaby4aed0642005-09-13 01:25:01 -0700997 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700999 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
1000 __func__);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001001 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001004 if (fw_get_builtin_firmware(firmware, name)) {
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +01001005 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001006 return 0; /* assigned */
David Woodhouse5658c762008-05-23 13:52:42 +01001007 }
1008
Ming Lei1f2b7952012-08-04 12:01:21 +08001009 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
Ming Lei1f2b7952012-08-04 12:01:21 +08001010
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001011 /*
1012 * bind with 'buf' now to avoid warning in failure path
1013 * of requesting firmware.
1014 */
1015 firmware->priv = buf;
Ming Lei1f2b7952012-08-04 12:01:21 +08001016
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001017 if (ret > 0) {
1018 ret = sync_cached_firmware_buf(buf);
1019 if (!ret) {
1020 fw_set_page_data(buf, firmware);
1021 return 0; /* assigned */
1022 }
Stephen Boyddddb5542012-03-28 23:30:43 +02001023 }
Ming Lei1f2b7952012-08-04 12:01:21 +08001024
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001025 if (ret < 0)
1026 return ret;
1027 return 1; /* need to load */
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001028}
1029
Ming Leie771d1a2013-05-27 18:30:03 +08001030static int assign_firmware_buf(struct firmware *fw, struct device *device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001031 unsigned int opt_flags)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001032{
1033 struct firmware_buf *buf = fw->priv;
1034
1035 mutex_lock(&fw_lock);
Takashi Iwai807be032013-01-31 11:13:57 +01001036 if (!buf->size || is_fw_load_aborted(buf)) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001037 mutex_unlock(&fw_lock);
1038 return -ENOENT;
1039 }
1040
1041 /*
1042 * add firmware name into devres list so that we can auto cache
1043 * and uncache firmware for device.
1044 *
1045 * device may has been deleted already, but the problem
1046 * should be fixed in devres or driver core.
1047 */
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001048 /* don't cache firmware handled without uevent */
1049 if (device && (opt_flags & FW_OPT_UEVENT))
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001050 fw_add_devm_name(device, buf->fw_id);
1051
1052 /*
1053 * After caching firmware image is started, let it piggyback
1054 * on request firmware.
1055 */
1056 if (buf->fwc->state == FW_LOADER_START_CACHE) {
1057 if (fw_cache_piggyback_on_request(buf->fw_id))
1058 kref_get(&buf->ref);
1059 }
1060
1061 /* pass the pages buffer to driver at the last minute */
1062 fw_set_page_data(buf, fw);
1063 mutex_unlock(&fw_lock);
1064 return 0;
1065}
1066
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001067/* called from request_firmware() and request_firmware_work_func() */
1068static int
1069_request_firmware(const struct firmware **firmware_p, const char *name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001070 struct device *device, unsigned int opt_flags)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001071{
1072 struct firmware *fw;
1073 long timeout;
1074 int ret;
1075
1076 if (!firmware_p)
1077 return -EINVAL;
1078
1079 ret = _request_firmware_prepare(&fw, name, device);
1080 if (ret <= 0) /* error or already assigned */
1081 goto out;
1082
1083 ret = 0;
1084 timeout = firmware_loading_timeout();
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001085 if (opt_flags & FW_OPT_NOWAIT) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001086 timeout = usermodehelper_read_lock_wait(timeout);
1087 if (!timeout) {
1088 dev_dbg(device, "firmware: %s loading timed out\n",
1089 name);
1090 ret = -EBUSY;
1091 goto out;
1092 }
1093 } else {
1094 ret = usermodehelper_read_trylock();
1095 if (WARN_ON(ret)) {
1096 dev_err(device, "firmware: %s will not be loaded\n",
1097 name);
1098 goto out;
1099 }
1100 }
1101
Neil Horman3e358ac2013-09-06 15:36:08 -04001102 ret = fw_get_filesystem_firmware(device, fw->priv);
1103 if (ret) {
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001104 if (opt_flags & FW_OPT_FALLBACK) {
Takashi Iwaibba3a872013-12-02 15:38:16 +01001105 dev_warn(device,
1106 "Direct firmware load failed with error %d\n",
1107 ret);
1108 dev_warn(device, "Falling back to user helper\n");
1109 ret = fw_load_from_user_helper(fw, name, device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001110 opt_flags, timeout);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001111 }
Neil Horman3e358ac2013-09-06 15:36:08 -04001112 }
Ming Leie771d1a2013-05-27 18:30:03 +08001113
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001114 if (!ret)
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001115 ret = assign_firmware_buf(fw, device, opt_flags);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001116
1117 usermodehelper_read_unlock();
1118
1119 out:
1120 if (ret < 0) {
1121 release_firmware(fw);
1122 fw = NULL;
1123 }
1124
1125 *firmware_p = fw;
1126 return ret;
1127}
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129/**
Kay Sievers312c0042005-11-16 09:00:00 +01001130 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001131 * @firmware_p: pointer to firmware image
1132 * @name: name of firmware file
1133 * @device: device for which firmware is being loaded
1134 *
1135 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001136 * of @name for device @device.
1137 *
1138 * Should be called from user context where sleeping is allowed.
1139 *
Kay Sievers312c0042005-11-16 09:00:00 +01001140 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001141 * should be distinctive enough not to be confused with any other
1142 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +08001143 *
1144 * Caller must hold the reference count of @device.
Ming Lei6a927852012-11-03 17:48:16 +08001145 *
1146 * The function can be called safely inside device's suspend and
1147 * resume callback.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001148 **/
1149int
1150request_firmware(const struct firmware **firmware_p, const char *name,
1151 struct device *device)
1152{
Ming Leid6c8aa32013-06-06 20:01:48 +08001153 int ret;
1154
1155 /* Need to pin this module until return */
1156 __module_get(THIS_MODULE);
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001157 ret = _request_firmware(firmware_p, name, device,
1158 FW_OPT_UEVENT | FW_OPT_FALLBACK);
Ming Leid6c8aa32013-06-06 20:01:48 +08001159 module_put(THIS_MODULE);
1160 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001161}
Daniel Mackf4945132013-05-23 22:17:18 +02001162EXPORT_SYMBOL(request_firmware);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001163
Takashi Iwaibba3a872013-12-02 15:38:16 +01001164#ifdef CONFIG_FW_LOADER_USER_HELPER
1165/**
1166 * request_firmware: - load firmware directly without usermode helper
1167 * @firmware_p: pointer to firmware image
1168 * @name: name of firmware file
1169 * @device: device for which firmware is being loaded
1170 *
1171 * This function works pretty much like request_firmware(), but this doesn't
1172 * fall back to usermode helper even if the firmware couldn't be loaded
1173 * directly from fs. Hence it's useful for loading optional firmwares, which
1174 * aren't always present, without extra long timeouts of udev.
1175 **/
1176int request_firmware_direct(const struct firmware **firmware_p,
1177 const char *name, struct device *device)
1178{
1179 int ret;
1180 __module_get(THIS_MODULE);
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001181 ret = _request_firmware(firmware_p, name, device, FW_OPT_UEVENT);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001182 module_put(THIS_MODULE);
1183 return ret;
1184}
1185EXPORT_SYMBOL_GPL(request_firmware_direct);
1186#endif
1187
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001188/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001190 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001192void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
1194 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001195 if (!fw_is_builtin_firmware(fw))
1196 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 kfree(fw);
1198 }
1199}
Daniel Mackf4945132013-05-23 22:17:18 +02001200EXPORT_SYMBOL(release_firmware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202/* Async support */
1203struct firmware_work {
1204 struct work_struct work;
1205 struct module *module;
1206 const char *name;
1207 struct device *device;
1208 void *context;
1209 void (*cont)(const struct firmware *fw, void *context);
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001210 unsigned int opt_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211};
1212
Stephen Boyda36cf842012-03-28 23:31:00 +02001213static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Stephen Boyda36cf842012-03-28 23:31:00 +02001215 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 const struct firmware *fw;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001217
Stephen Boyda36cf842012-03-28 23:31:00 +02001218 fw_work = container_of(work, struct firmware_work, work);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001219
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001220 _request_firmware(&fw, fw_work->name, fw_work->device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001221 fw_work->opt_flags);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001222 fw_work->cont(fw, fw_work->context);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001223 put_device(fw_work->device); /* taken in request_firmware_nowait() */
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 module_put(fw_work->module);
1226 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
1229/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001230 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001231 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001232 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001233 * is non-zero else the firmware copy must be done manually.
1234 * @name: name of firmware file
1235 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001236 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001237 * @context: will be passed over to @cont, and
1238 * @fw may be %NULL if firmware request fails.
1239 * @cont: function will be called asynchronously when the firmware
1240 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001242 * Caller must hold the reference count of @device.
1243 *
Ming Lei6f21a622012-08-04 12:01:24 +08001244 * Asynchronous variant of request_firmware() for user contexts:
1245 * - sleep for as small periods as possible since it may
1246 * increase kernel boot time of built-in device drivers
1247 * requesting firmware in their ->probe() methods, if
1248 * @gfp is GFP_KERNEL.
1249 *
1250 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 **/
1252int
1253request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001254 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001255 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 void (*cont)(const struct firmware *fw, void *context))
1257{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001258 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001260 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 if (!fw_work)
1262 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001263
1264 fw_work->module = module;
1265 fw_work->name = name;
1266 fw_work->device = device;
1267 fw_work->context = context;
1268 fw_work->cont = cont;
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001269 fw_work->opt_flags = FW_OPT_NOWAIT | FW_OPT_FALLBACK |
1270 (uevent ? FW_OPT_UEVENT : 0);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if (!try_module_get(module)) {
1273 kfree(fw_work);
1274 return -EFAULT;
1275 }
1276
Ming Lei0cfc1e12012-08-04 12:01:23 +08001277 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001278 INIT_WORK(&fw_work->work, request_firmware_work_func);
1279 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 return 0;
1281}
Daniel Mackf4945132013-05-23 22:17:18 +02001282EXPORT_SYMBOL(request_firmware_nowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Ming Lei90f890812013-06-20 12:30:16 +08001284#ifdef CONFIG_PM_SLEEP
1285static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1286
Ming Lei2887b392012-08-04 12:01:22 +08001287/**
1288 * cache_firmware - cache one firmware image in kernel memory space
1289 * @fw_name: the firmware image name
1290 *
1291 * Cache firmware in kernel memory so that drivers can use it when
1292 * system isn't ready for them to request firmware image from userspace.
1293 * Once it returns successfully, driver can use request_firmware or its
1294 * nowait version to get the cached firmware without any interacting
1295 * with userspace
1296 *
1297 * Return 0 if the firmware image has been cached successfully
1298 * Return !0 otherwise
1299 *
1300 */
Ming Lei93232e42013-06-06 20:01:47 +08001301static int cache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001302{
1303 int ret;
1304 const struct firmware *fw;
1305
1306 pr_debug("%s: %s\n", __func__, fw_name);
1307
1308 ret = request_firmware(&fw, fw_name, NULL);
1309 if (!ret)
1310 kfree(fw);
1311
1312 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1313
1314 return ret;
1315}
1316
Ming Lei6a2c1232013-06-26 09:28:17 +08001317static struct firmware_buf *fw_lookup_buf(const char *fw_name)
1318{
1319 struct firmware_buf *tmp;
1320 struct firmware_cache *fwc = &fw_cache;
1321
1322 spin_lock(&fwc->lock);
1323 tmp = __fw_lookup_buf(fw_name);
1324 spin_unlock(&fwc->lock);
1325
1326 return tmp;
1327}
1328
Ming Lei2887b392012-08-04 12:01:22 +08001329/**
1330 * uncache_firmware - remove one cached firmware image
1331 * @fw_name: the firmware image name
1332 *
1333 * Uncache one firmware image which has been cached successfully
1334 * before.
1335 *
1336 * Return 0 if the firmware cache has been removed successfully
1337 * Return !0 otherwise
1338 *
1339 */
Ming Lei93232e42013-06-06 20:01:47 +08001340static int uncache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001341{
1342 struct firmware_buf *buf;
1343 struct firmware fw;
1344
1345 pr_debug("%s: %s\n", __func__, fw_name);
1346
1347 if (fw_get_builtin_firmware(&fw, fw_name))
1348 return 0;
1349
1350 buf = fw_lookup_buf(fw_name);
1351 if (buf) {
1352 fw_free_buf(buf);
1353 return 0;
1354 }
1355
1356 return -EINVAL;
1357}
1358
Ming Lei37276a52012-08-04 12:01:27 +08001359static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1360{
1361 struct fw_cache_entry *fce;
1362
1363 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1364 if (!fce)
1365 goto exit;
1366
1367 strcpy(fce->name, name);
1368exit:
1369 return fce;
1370}
1371
Ming Lei373304f2012-10-09 12:01:01 +08001372static int __fw_entry_found(const char *name)
1373{
1374 struct firmware_cache *fwc = &fw_cache;
1375 struct fw_cache_entry *fce;
1376
1377 list_for_each_entry(fce, &fwc->fw_names, list) {
1378 if (!strcmp(fce->name, name))
1379 return 1;
1380 }
1381 return 0;
1382}
1383
Ming Leiac39b3e2012-08-20 19:04:16 +08001384static int fw_cache_piggyback_on_request(const char *name)
1385{
1386 struct firmware_cache *fwc = &fw_cache;
1387 struct fw_cache_entry *fce;
1388 int ret = 0;
1389
1390 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001391 if (__fw_entry_found(name))
1392 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001393
1394 fce = alloc_fw_cache_entry(name);
1395 if (fce) {
1396 ret = 1;
1397 list_add(&fce->list, &fwc->fw_names);
1398 pr_debug("%s: fw: %s\n", __func__, name);
1399 }
1400found:
1401 spin_unlock(&fwc->name_lock);
1402 return ret;
1403}
1404
Ming Lei37276a52012-08-04 12:01:27 +08001405static void free_fw_cache_entry(struct fw_cache_entry *fce)
1406{
1407 kfree(fce);
1408}
1409
1410static void __async_dev_cache_fw_image(void *fw_entry,
1411 async_cookie_t cookie)
1412{
1413 struct fw_cache_entry *fce = fw_entry;
1414 struct firmware_cache *fwc = &fw_cache;
1415 int ret;
1416
1417 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001418 if (ret) {
1419 spin_lock(&fwc->name_lock);
1420 list_del(&fce->list);
1421 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001422
Ming Leiac39b3e2012-08-20 19:04:16 +08001423 free_fw_cache_entry(fce);
1424 }
Ming Lei37276a52012-08-04 12:01:27 +08001425}
1426
1427/* called with dev->devres_lock held */
1428static void dev_create_fw_entry(struct device *dev, void *res,
1429 void *data)
1430{
1431 struct fw_name_devm *fwn = res;
1432 const char *fw_name = fwn->name;
1433 struct list_head *head = data;
1434 struct fw_cache_entry *fce;
1435
1436 fce = alloc_fw_cache_entry(fw_name);
1437 if (fce)
1438 list_add(&fce->list, head);
1439}
1440
1441static int devm_name_match(struct device *dev, void *res,
1442 void *match_data)
1443{
1444 struct fw_name_devm *fwn = res;
1445 return (fwn->magic == (unsigned long)match_data);
1446}
1447
Ming Leiab6dd8e2012-08-17 22:07:00 +08001448static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001449{
1450 LIST_HEAD(todo);
1451 struct fw_cache_entry *fce;
1452 struct fw_cache_entry *fce_next;
1453 struct firmware_cache *fwc = &fw_cache;
1454
1455 devres_for_each_res(dev, fw_name_devm_release,
1456 devm_name_match, &fw_cache,
1457 dev_create_fw_entry, &todo);
1458
1459 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1460 list_del(&fce->list);
1461
1462 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001463 /* only one cache entry for one firmware */
1464 if (!__fw_entry_found(fce->name)) {
Ming Lei373304f2012-10-09 12:01:01 +08001465 list_add(&fce->list, &fwc->fw_names);
1466 } else {
1467 free_fw_cache_entry(fce);
1468 fce = NULL;
1469 }
Ming Lei37276a52012-08-04 12:01:27 +08001470 spin_unlock(&fwc->name_lock);
1471
Ming Lei373304f2012-10-09 12:01:01 +08001472 if (fce)
Ming Leid28d3882012-10-09 12:01:04 +08001473 async_schedule_domain(__async_dev_cache_fw_image,
1474 (void *)fce,
1475 &fw_cache_domain);
Ming Lei37276a52012-08-04 12:01:27 +08001476 }
1477}
1478
1479static void __device_uncache_fw_images(void)
1480{
1481 struct firmware_cache *fwc = &fw_cache;
1482 struct fw_cache_entry *fce;
1483
1484 spin_lock(&fwc->name_lock);
1485 while (!list_empty(&fwc->fw_names)) {
1486 fce = list_entry(fwc->fw_names.next,
1487 struct fw_cache_entry, list);
1488 list_del(&fce->list);
1489 spin_unlock(&fwc->name_lock);
1490
1491 uncache_firmware(fce->name);
1492 free_fw_cache_entry(fce);
1493
1494 spin_lock(&fwc->name_lock);
1495 }
1496 spin_unlock(&fwc->name_lock);
1497}
1498
1499/**
1500 * device_cache_fw_images - cache devices' firmware
1501 *
1502 * If one device called request_firmware or its nowait version
1503 * successfully before, the firmware names are recored into the
1504 * device's devres link list, so device_cache_fw_images can call
1505 * cache_firmware() to cache these firmwares for the device,
1506 * then the device driver can load its firmwares easily at
1507 * time when system is not ready to complete loading firmware.
1508 */
1509static void device_cache_fw_images(void)
1510{
1511 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001512 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001513 DEFINE_WAIT(wait);
1514
1515 pr_debug("%s\n", __func__);
1516
Ming Lei373304f2012-10-09 12:01:01 +08001517 /* cancel uncache work */
1518 cancel_delayed_work_sync(&fwc->work);
1519
Ming Leiffe53f62012-08-04 12:01:28 +08001520 /*
1521 * use small loading timeout for caching devices' firmware
1522 * because all these firmware images have been loaded
1523 * successfully at lease once, also system is ready for
1524 * completing firmware loading now. The maximum size of
1525 * firmware in current distributions is about 2M bytes,
1526 * so 10 secs should be enough.
1527 */
1528 old_timeout = loading_timeout;
1529 loading_timeout = 10;
1530
Ming Leiac39b3e2012-08-20 19:04:16 +08001531 mutex_lock(&fw_lock);
1532 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001533 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001534 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001535
1536 /* wait for completion of caching firmware for all devices */
Ming Leid28d3882012-10-09 12:01:04 +08001537 async_synchronize_full_domain(&fw_cache_domain);
Ming Leiffe53f62012-08-04 12:01:28 +08001538
1539 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001540}
1541
1542/**
1543 * device_uncache_fw_images - uncache devices' firmware
1544 *
1545 * uncache all firmwares which have been cached successfully
1546 * by device_uncache_fw_images earlier
1547 */
1548static void device_uncache_fw_images(void)
1549{
1550 pr_debug("%s\n", __func__);
1551 __device_uncache_fw_images();
1552}
1553
1554static void device_uncache_fw_images_work(struct work_struct *work)
1555{
1556 device_uncache_fw_images();
1557}
1558
1559/**
1560 * device_uncache_fw_images_delay - uncache devices firmwares
1561 * @delay: number of milliseconds to delay uncache device firmwares
1562 *
1563 * uncache all devices's firmwares which has been cached successfully
1564 * by device_cache_fw_images after @delay milliseconds.
1565 */
1566static void device_uncache_fw_images_delay(unsigned long delay)
1567{
1568 schedule_delayed_work(&fw_cache.work,
1569 msecs_to_jiffies(delay));
1570}
1571
Ming Lei07646d92012-08-04 12:01:29 +08001572static int fw_pm_notify(struct notifier_block *notify_block,
1573 unsigned long mode, void *unused)
1574{
1575 switch (mode) {
1576 case PM_HIBERNATION_PREPARE:
1577 case PM_SUSPEND_PREPARE:
Ming Leiaf5bc112013-05-27 18:30:04 +08001578 kill_requests_without_uevent();
Ming Lei07646d92012-08-04 12:01:29 +08001579 device_cache_fw_images();
1580 break;
1581
1582 case PM_POST_SUSPEND:
1583 case PM_POST_HIBERNATION:
1584 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001585 /*
1586 * In case that system sleep failed and syscore_suspend is
1587 * not called.
1588 */
1589 mutex_lock(&fw_lock);
1590 fw_cache.state = FW_LOADER_NO_CACHE;
1591 mutex_unlock(&fw_lock);
1592
Ming Lei07646d92012-08-04 12:01:29 +08001593 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1594 break;
1595 }
1596
1597 return 0;
1598}
Ming Lei07646d92012-08-04 12:01:29 +08001599
Ming Leiac39b3e2012-08-20 19:04:16 +08001600/* stop caching firmware once syscore_suspend is reached */
1601static int fw_suspend(void)
1602{
1603 fw_cache.state = FW_LOADER_NO_CACHE;
1604 return 0;
1605}
1606
1607static struct syscore_ops fw_syscore_ops = {
1608 .suspend = fw_suspend,
1609};
Ming Leicfe016b2012-09-08 17:32:30 +08001610#else
1611static int fw_cache_piggyback_on_request(const char *name)
1612{
1613 return 0;
1614}
1615#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001616
Ming Lei37276a52012-08-04 12:01:27 +08001617static void __init fw_cache_init(void)
1618{
1619 spin_lock_init(&fw_cache.lock);
1620 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001621 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001622
Ming Leicfe016b2012-09-08 17:32:30 +08001623#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +08001624 spin_lock_init(&fw_cache.name_lock);
1625 INIT_LIST_HEAD(&fw_cache.fw_names);
Ming Lei37276a52012-08-04 12:01:27 +08001626
Ming Lei37276a52012-08-04 12:01:27 +08001627 INIT_DELAYED_WORK(&fw_cache.work,
1628 device_uncache_fw_images_work);
Ming Lei07646d92012-08-04 12:01:29 +08001629
1630 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1631 register_pm_notifier(&fw_cache.pm_notify);
Ming Leiac39b3e2012-08-20 19:04:16 +08001632
1633 register_syscore_ops(&fw_syscore_ops);
Ming Leicfe016b2012-09-08 17:32:30 +08001634#endif
Ming Lei37276a52012-08-04 12:01:27 +08001635}
1636
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001637static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
Ming Lei1f2b7952012-08-04 12:01:21 +08001639 fw_cache_init();
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001640#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaife304142013-05-22 18:28:38 +02001641 register_reboot_notifier(&fw_shutdown_nb);
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001642 return class_register(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001643#else
1644 return 0;
1645#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001647
1648static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
Ming Leicfe016b2012-09-08 17:32:30 +08001650#ifdef CONFIG_PM_SLEEP
Ming Leiac39b3e2012-08-20 19:04:16 +08001651 unregister_syscore_ops(&fw_syscore_ops);
Ming Lei07646d92012-08-04 12:01:29 +08001652 unregister_pm_notifier(&fw_cache.pm_notify);
Ming Leicfe016b2012-09-08 17:32:30 +08001653#endif
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001654#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaife304142013-05-22 18:28:38 +02001655 unregister_reboot_notifier(&fw_shutdown_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 class_unregister(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001657#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658}
1659
Shaohua Lia30a6a22006-09-27 01:50:52 -07001660fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661module_exit(firmware_class_exit);