blob: 84b1c8d0ea56367eb9788320bbaea68a79bffc72 [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>
Ming Lei37276a52012-08-04 12:01:27 +080030
Linus Torvaldsabb139e2012-10-03 15:58:32 -070031#include <generated/utsrelease.h>
32
Ming Lei37276a52012-08-04 12:01:27 +080033#include "base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Markus Rechberger87d37a42007-06-04 18:45:44 +020035MODULE_AUTHOR("Manuel Estrada Sainz");
Linus Torvalds1da177e2005-04-16 15:20:36 -070036MODULE_DESCRIPTION("Multi purpose firmware loading support");
37MODULE_LICENSE("GPL");
38
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080039/* Builtin firmware support */
40
41#ifdef CONFIG_FW_LOADER
42
43extern struct builtin_fw __start_builtin_fw[];
44extern struct builtin_fw __end_builtin_fw[];
45
46static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
47{
48 struct builtin_fw *b_fw;
49
50 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
51 if (strcmp(name, b_fw->name) == 0) {
52 fw->size = b_fw->size;
53 fw->data = b_fw->data;
54 return true;
55 }
56 }
57
58 return false;
59}
60
61static bool fw_is_builtin_firmware(const struct firmware *fw)
62{
63 struct builtin_fw *b_fw;
64
65 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
66 if (fw->data == b_fw->data)
67 return true;
68
69 return false;
70}
71
72#else /* Module case - no builtin firmware support */
73
74static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
75{
76 return false;
77}
78
79static inline bool fw_is_builtin_firmware(const struct firmware *fw)
80{
81 return false;
82}
83#endif
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085enum {
86 FW_STATUS_LOADING,
87 FW_STATUS_DONE,
88 FW_STATUS_ABORT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070089};
90
Takashi Iwai7b1269f2013-01-31 11:13:55 +010091#ifdef CONFIG_FW_LOADER_USER_HELPER
Ming Lei746058f2012-10-09 12:01:03 +080092enum fw_buf_fmt {
93 VMALLOC_BUF, /* used in direct loading */
94 PAGE_BUF, /* used in loading via userspace */
95};
Takashi Iwai7b1269f2013-01-31 11:13:55 +010096#endif /* CONFIG_FW_LOADER_USER_HELPER */
Ming Lei746058f2012-10-09 12:01:03 +080097
Dave Jones2f651682007-01-25 15:56:15 -050098static int loading_timeout = 60; /* In seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200100static inline long firmware_loading_timeout(void)
101{
102 return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
103}
104
Ming Lei1f2b7952012-08-04 12:01:21 +0800105struct firmware_cache {
106 /* firmware_buf instance will be added into the below list */
107 spinlock_t lock;
108 struct list_head head;
Ming Leicfe016b2012-09-08 17:32:30 +0800109 int state;
Ming Lei37276a52012-08-04 12:01:27 +0800110
Ming Leicfe016b2012-09-08 17:32:30 +0800111#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +0800112 /*
113 * Names of firmware images which have been cached successfully
114 * will be added into the below list so that device uncache
115 * helper can trace which firmware images have been cached
116 * before.
117 */
118 spinlock_t name_lock;
119 struct list_head fw_names;
120
Ming Lei37276a52012-08-04 12:01:27 +0800121 struct delayed_work work;
Ming Lei07646d92012-08-04 12:01:29 +0800122
123 struct notifier_block pm_notify;
Ming Leicfe016b2012-09-08 17:32:30 +0800124#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800125};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Ming Lei12446912012-08-04 12:01:20 +0800127struct firmware_buf {
Ming Lei1f2b7952012-08-04 12:01:21 +0800128 struct kref ref;
129 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 struct completion completion;
Ming Lei1f2b7952012-08-04 12:01:21 +0800131 struct firmware_cache *fwc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 unsigned long status;
Ming Lei65710cb2012-08-04 12:01:16 +0800133 void *data;
134 size_t size;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100135#ifdef CONFIG_FW_LOADER_USER_HELPER
136 enum fw_buf_fmt fmt;
David Woodhouse6e03a202009-04-09 22:04:07 -0700137 struct page **pages;
138 int nr_pages;
139 int page_array_size;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100140#endif
Dmitry Torokhove1771232010-03-13 23:49:23 -0800141 char fw_id[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142};
143
Ming Lei37276a52012-08-04 12:01:27 +0800144struct fw_cache_entry {
145 struct list_head list;
146 char name[];
147};
148
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100149#ifdef CONFIG_FW_LOADER_USER_HELPER
Ming Lei12446912012-08-04 12:01:20 +0800150struct firmware_priv {
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800151 struct delayed_work timeout_work;
Ming Lei12446912012-08-04 12:01:20 +0800152 bool nowait;
153 struct device dev;
154 struct firmware_buf *buf;
Ming Lei1f2b7952012-08-04 12:01:21 +0800155 struct firmware *fw;
Ming Lei12446912012-08-04 12:01:20 +0800156};
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100157#endif
Ming Lei12446912012-08-04 12:01:20 +0800158
Ming Leif531f05a2012-08-04 12:01:25 +0800159struct fw_name_devm {
160 unsigned long magic;
161 char name[];
162};
163
Ming Lei1f2b7952012-08-04 12:01:21 +0800164#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
165
Ming Leiac39b3e2012-08-20 19:04:16 +0800166#define FW_LOADER_NO_CACHE 0
167#define FW_LOADER_START_CACHE 1
168
169static int fw_cache_piggyback_on_request(const char *name);
170
Ming Lei1f2b7952012-08-04 12:01:21 +0800171/* fw_lock could be moved to 'struct firmware_priv' but since it is just
172 * guarding for corner cases a global lock should be OK */
173static DEFINE_MUTEX(fw_lock);
174
175static struct firmware_cache fw_cache;
176
177static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
178 struct firmware_cache *fwc)
179{
180 struct firmware_buf *buf;
181
182 buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
183
184 if (!buf)
185 return buf;
186
187 kref_init(&buf->ref);
188 strcpy(buf->fw_id, fw_name);
189 buf->fwc = fwc;
190 init_completion(&buf->completion);
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100191#ifdef CONFIG_FW_LOADER_USER_HELPER
Ming Lei746058f2012-10-09 12:01:03 +0800192 buf->fmt = VMALLOC_BUF;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100193#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800194
195 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
196
197 return buf;
198}
199
Ming Lei2887b392012-08-04 12:01:22 +0800200static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
201{
202 struct firmware_buf *tmp;
203 struct firmware_cache *fwc = &fw_cache;
204
205 list_for_each_entry(tmp, &fwc->head, list)
206 if (!strcmp(tmp->fw_id, fw_name))
207 return tmp;
208 return NULL;
209}
210
Ming Lei1f2b7952012-08-04 12:01:21 +0800211static int fw_lookup_and_allocate_buf(const char *fw_name,
212 struct firmware_cache *fwc,
213 struct firmware_buf **buf)
214{
215 struct firmware_buf *tmp;
216
217 spin_lock(&fwc->lock);
Ming Lei2887b392012-08-04 12:01:22 +0800218 tmp = __fw_lookup_buf(fw_name);
219 if (tmp) {
220 kref_get(&tmp->ref);
221 spin_unlock(&fwc->lock);
222 *buf = tmp;
223 return 1;
224 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800225 tmp = __allocate_fw_buf(fw_name, fwc);
226 if (tmp)
227 list_add(&tmp->list, &fwc->head);
228 spin_unlock(&fwc->lock);
229
230 *buf = tmp;
231
232 return tmp ? 0 : -ENOMEM;
233}
234
Ming Lei2887b392012-08-04 12:01:22 +0800235static struct firmware_buf *fw_lookup_buf(const char *fw_name)
236{
237 struct firmware_buf *tmp;
238 struct firmware_cache *fwc = &fw_cache;
239
240 spin_lock(&fwc->lock);
241 tmp = __fw_lookup_buf(fw_name);
242 spin_unlock(&fwc->lock);
243
244 return tmp;
245}
246
Ming Lei1f2b7952012-08-04 12:01:21 +0800247static void __fw_free_buf(struct kref *ref)
248{
249 struct firmware_buf *buf = to_fwbuf(ref);
250 struct firmware_cache *fwc = buf->fwc;
Ming Lei1f2b7952012-08-04 12:01:21 +0800251
252 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
253 __func__, buf->fw_id, buf, buf->data,
254 (unsigned int)buf->size);
255
Ming Lei1f2b7952012-08-04 12:01:21 +0800256 list_del(&buf->list);
257 spin_unlock(&fwc->lock);
258
Ming Lei746058f2012-10-09 12:01:03 +0800259
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100260#ifdef CONFIG_FW_LOADER_USER_HELPER
Ming Lei746058f2012-10-09 12:01:03 +0800261 if (buf->fmt == PAGE_BUF) {
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100262 int i;
Ming Lei746058f2012-10-09 12:01:03 +0800263 vunmap(buf->data);
264 for (i = 0; i < buf->nr_pages; i++)
265 __free_page(buf->pages[i]);
266 kfree(buf->pages);
267 } else
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100268#endif
Ming Lei746058f2012-10-09 12:01:03 +0800269 vfree(buf->data);
Ming Lei1f2b7952012-08-04 12:01:21 +0800270 kfree(buf);
271}
272
273static void fw_free_buf(struct firmware_buf *buf)
274{
Chuansheng Liubd9eb7f2012-11-10 01:27:22 +0800275 struct firmware_cache *fwc = buf->fwc;
276 spin_lock(&fwc->lock);
277 if (!kref_put(&buf->ref, __fw_free_buf))
278 spin_unlock(&fwc->lock);
Ming Lei1f2b7952012-08-04 12:01:21 +0800279}
280
Ming Lei746058f2012-10-09 12:01:03 +0800281/* direct firmware loading support */
Ming Lei27602842012-11-03 17:47:58 +0800282static char fw_path_para[256];
283static const char * const fw_path[] = {
284 fw_path_para,
Ming Lei746058f2012-10-09 12:01:03 +0800285 "/lib/firmware/updates/" UTS_RELEASE,
286 "/lib/firmware/updates",
287 "/lib/firmware/" UTS_RELEASE,
288 "/lib/firmware"
289};
290
Ming Lei27602842012-11-03 17:47:58 +0800291/*
292 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
293 * from kernel command line because firmware_class is generally built in
294 * kernel instead of module.
295 */
296module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
297MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
298
Ming Lei746058f2012-10-09 12:01:03 +0800299/* Don't inline this: 'struct kstat' is biggish */
Cesar Eduardo Barros60dac5e2012-10-27 20:37:10 -0200300static noinline_for_stack long fw_file_size(struct file *file)
Ming Lei746058f2012-10-09 12:01:03 +0800301{
302 struct kstat st;
303 if (vfs_getattr(file->f_path.mnt, file->f_path.dentry, &st))
304 return -1;
305 if (!S_ISREG(st.mode))
306 return -1;
307 if (st.size != (long)st.size)
308 return -1;
309 return st.size;
310}
311
312static bool fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf)
313{
314 long size;
315 char *buf;
316
317 size = fw_file_size(file);
Luciano Coelho4adf07f2013-01-15 10:43:43 +0200318 if (size <= 0)
Ming Lei746058f2012-10-09 12:01:03 +0800319 return false;
320 buf = vmalloc(size);
321 if (!buf)
322 return false;
323 if (kernel_read(file, 0, buf, size) != size) {
324 vfree(buf);
325 return false;
326 }
327 fw_buf->data = buf;
328 fw_buf->size = size;
329 return true;
330}
331
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100332static bool fw_get_filesystem_firmware(struct device *device,
333 struct firmware_buf *buf)
Ming Lei746058f2012-10-09 12:01:03 +0800334{
335 int i;
336 bool success = false;
337 char *path = __getname();
338
339 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
340 struct file *file;
Ming Lei27602842012-11-03 17:47:58 +0800341
342 /* skip the unset customized path */
343 if (!fw_path[i][0])
344 continue;
345
Ming Lei746058f2012-10-09 12:01:03 +0800346 snprintf(path, PATH_MAX, "%s/%s", fw_path[i], buf->fw_id);
347
348 file = filp_open(path, O_RDONLY, 0);
349 if (IS_ERR(file))
350 continue;
351 success = fw_read_file_contents(file, buf);
352 fput(file);
353 if (success)
354 break;
355 }
356 __putname(path);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100357
358 if (success) {
359 dev_dbg(device, "firmware: direct-loading firmware %s\n",
360 buf->fw_id);
361 mutex_lock(&fw_lock);
362 set_bit(FW_STATUS_DONE, &buf->status);
363 complete_all(&buf->completion);
364 mutex_unlock(&fw_lock);
365 }
366
Ming Lei746058f2012-10-09 12:01:03 +0800367 return success;
368}
369
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100370/* firmware holds the ownership of pages */
371static void firmware_free_data(const struct firmware *fw)
372{
373 /* Loaded directly? */
374 if (!fw->priv) {
375 vfree(fw->data);
376 return;
377 }
378 fw_free_buf(fw->priv);
379}
380
381#ifdef CONFIG_FW_LOADER_USER_HELPER
382
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700383static struct firmware_priv *to_firmware_priv(struct device *dev)
384{
385 return container_of(dev, struct firmware_priv, dev);
386}
387
388static void fw_load_abort(struct firmware_priv *fw_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Ming Lei12446912012-08-04 12:01:20 +0800390 struct firmware_buf *buf = fw_priv->buf;
391
392 set_bit(FW_STATUS_ABORT, &buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800393 complete_all(&buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700396static ssize_t firmware_timeout_show(struct class *class,
397 struct class_attribute *attr,
398 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 return sprintf(buf, "%d\n", loading_timeout);
401}
402
403/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800404 * firmware_timeout_store - set number of seconds to wait for firmware
405 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800406 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800407 * @buf: buffer to scan for timeout value
408 * @count: number of bytes in @buf
409 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800411 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 * firmware will be provided.
413 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800414 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700416static ssize_t firmware_timeout_store(struct class *class,
417 struct class_attribute *attr,
418 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700421 if (loading_timeout < 0)
422 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return count;
425}
426
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800427static struct class_attribute firmware_class_attrs[] = {
428 __ATTR(timeout, S_IWUSR | S_IRUGO,
429 firmware_timeout_show, firmware_timeout_store),
430 __ATTR_NULL
431};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800433static void fw_dev_release(struct device *dev)
434{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700435 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800436
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800437 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800438
439 module_put(THIS_MODULE);
440}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Kay Sievers7eff2e72007-08-14 15:15:12 +0200442static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700444 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Ming Lei12446912012-08-04 12:01:20 +0800446 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200448 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700449 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200450 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
451 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 return 0;
454}
455
Adrian Bunk1b81d662006-05-20 15:00:16 -0700456static struct class firmware_class = {
457 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800458 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700459 .dev_uevent = firmware_uevent,
460 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700461};
462
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700463static ssize_t firmware_loading_show(struct device *dev,
464 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700466 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800467 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return sprintf(buf, "%d\n", loading);
470}
471
David Woodhouse6e03a202009-04-09 22:04:07 -0700472/* Some architectures don't have PAGE_KERNEL_RO */
473#ifndef PAGE_KERNEL_RO
474#define PAGE_KERNEL_RO PAGE_KERNEL
475#endif
Ming Lei253c9242012-10-09 12:01:02 +0800476
477/* one pages buffer should be mapped/unmapped only once */
478static int fw_map_pages_buf(struct firmware_buf *buf)
479{
Ming Lei746058f2012-10-09 12:01:03 +0800480 if (buf->fmt != PAGE_BUF)
481 return 0;
482
Ming Lei253c9242012-10-09 12:01:02 +0800483 if (buf->data)
484 vunmap(buf->data);
485 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
486 if (!buf->data)
487 return -ENOMEM;
488 return 0;
489}
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800492 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700493 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800494 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800495 * @buf: buffer to scan for loading control value
496 * @count: number of bytes in @buf
497 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 * The relevant values are:
499 *
500 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800501 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 * -1: Conclude the load with an error and discard any written data.
503 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700504static ssize_t firmware_loading_store(struct device *dev,
505 struct device_attribute *attr,
506 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700508 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800509 struct firmware_buf *fw_buf = fw_priv->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700511 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Neil Hormaneea915b2012-01-02 15:31:23 -0500513 mutex_lock(&fw_lock);
514
Ming Lei12446912012-08-04 12:01:20 +0800515 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500516 goto out;
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 switch (loading) {
519 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800520 /* discarding any previous partial load */
Ming Lei12446912012-08-04 12:01:20 +0800521 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
522 for (i = 0; i < fw_buf->nr_pages; i++)
523 __free_page(fw_buf->pages[i]);
524 kfree(fw_buf->pages);
525 fw_buf->pages = NULL;
526 fw_buf->page_array_size = 0;
527 fw_buf->nr_pages = 0;
528 set_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei28eefa72012-08-04 12:01:17 +0800529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 break;
531 case 0:
Ming Lei12446912012-08-04 12:01:20 +0800532 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
533 set_bit(FW_STATUS_DONE, &fw_buf->status);
534 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei253c9242012-10-09 12:01:02 +0800535
536 /*
537 * Several loading requests may be pending on
538 * one same firmware buf, so let all requests
539 * see the mapped 'buf->data' once the loading
540 * is completed.
541 * */
542 fw_map_pages_buf(fw_buf);
Ming Lei1f2b7952012-08-04 12:01:21 +0800543 complete_all(&fw_buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 break;
545 }
546 /* fallthrough */
547 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700548 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 /* fallthrough */
550 case -1:
551 fw_load_abort(fw_priv);
552 break;
553 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500554out:
555 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return count;
557}
558
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700559static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700561static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
562 struct bin_attribute *bin_attr,
563 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200565 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700566 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800567 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700568 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Laura Garciacad1e552006-05-23 23:22:38 +0200570 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800571 buf = fw_priv->buf;
572 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 ret_count = -ENODEV;
574 goto out;
575 }
Ming Lei12446912012-08-04 12:01:20 +0800576 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200577 ret_count = 0;
578 goto out;
579 }
Ming Lei12446912012-08-04 12:01:20 +0800580 if (count > buf->size - offset)
581 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700582
583 ret_count = count;
584
585 while (count) {
586 void *page_data;
587 int page_nr = offset >> PAGE_SHIFT;
588 int page_ofs = offset & (PAGE_SIZE-1);
589 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
590
Ming Lei12446912012-08-04 12:01:20 +0800591 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700592
593 memcpy(buffer, page_data + page_ofs, page_cnt);
594
Ming Lei12446912012-08-04 12:01:20 +0800595 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700596 buffer += page_cnt;
597 offset += page_cnt;
598 count -= page_cnt;
599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600out:
Laura Garciacad1e552006-05-23 23:22:38 +0200601 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return ret_count;
603}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800604
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700605static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Ming Lei12446912012-08-04 12:01:20 +0800607 struct firmware_buf *buf = fw_priv->buf;
David Woodhouse6e03a202009-04-09 22:04:07 -0700608 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
David Woodhouse6e03a202009-04-09 22:04:07 -0700610 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800611 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700612 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800613 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700614 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
David Woodhouse6e03a202009-04-09 22:04:07 -0700616 new_pages = kmalloc(new_array_size * sizeof(void *),
617 GFP_KERNEL);
618 if (!new_pages) {
619 fw_load_abort(fw_priv);
620 return -ENOMEM;
621 }
Ming Lei12446912012-08-04 12:01:20 +0800622 memcpy(new_pages, buf->pages,
623 buf->page_array_size * sizeof(void *));
624 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
625 (new_array_size - buf->page_array_size));
626 kfree(buf->pages);
627 buf->pages = new_pages;
628 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700630
Ming Lei12446912012-08-04 12:01:20 +0800631 while (buf->nr_pages < pages_needed) {
632 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700633 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
634
Ming Lei12446912012-08-04 12:01:20 +0800635 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700636 fw_load_abort(fw_priv);
637 return -ENOMEM;
638 }
Ming Lei12446912012-08-04 12:01:20 +0800639 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return 0;
642}
643
644/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800645 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700646 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700647 * @kobj: kobject for the device
Randy Dunlap42e61f4a2007-07-23 21:42:11 -0700648 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800649 * @buffer: buffer being written
650 * @offset: buffer offset for write in total data store area
651 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800653 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 * the driver as a firmware image.
655 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700656static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
657 struct bin_attribute *bin_attr,
658 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200660 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700661 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800662 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 ssize_t retval;
664
665 if (!capable(CAP_SYS_RAWIO))
666 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700667
Laura Garciacad1e552006-05-23 23:22:38 +0200668 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800669 buf = fw_priv->buf;
670 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 retval = -ENODEV;
672 goto out;
673 }
Ming Lei65710cb2012-08-04 12:01:16 +0800674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 retval = fw_realloc_buffer(fw_priv, offset + count);
676 if (retval)
677 goto out;
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700680
681 while (count) {
682 void *page_data;
683 int page_nr = offset >> PAGE_SHIFT;
684 int page_ofs = offset & (PAGE_SIZE - 1);
685 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
686
Ming Lei12446912012-08-04 12:01:20 +0800687 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700688
689 memcpy(page_data + page_ofs, buffer, page_cnt);
690
Ming Lei12446912012-08-04 12:01:20 +0800691 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700692 buffer += page_cnt;
693 offset += page_cnt;
694 count -= page_cnt;
695 }
696
Ming Lei12446912012-08-04 12:01:20 +0800697 buf->size = max_t(size_t, offset, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698out:
Laura Garciacad1e552006-05-23 23:22:38 +0200699 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return retval;
701}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800702
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700703static struct bin_attribute firmware_attr_data = {
704 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 .size = 0,
706 .read = firmware_data_read,
707 .write = firmware_data_write,
708};
709
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800710static void firmware_class_timeout_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800712 struct firmware_priv *fw_priv = container_of(work,
713 struct firmware_priv, timeout_work.work);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700714
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800715 mutex_lock(&fw_lock);
716 if (test_bit(FW_STATUS_DONE, &(fw_priv->buf->status))) {
717 mutex_unlock(&fw_lock);
718 return;
719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 fw_load_abort(fw_priv);
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800721 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700724static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200725fw_create_instance(struct firmware *firmware, const char *fw_name,
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700726 struct device *device, bool uevent, bool nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700728 struct firmware_priv *fw_priv;
729 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Ming Lei12446912012-08-04 12:01:20 +0800731 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700732 if (!fw_priv) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700733 dev_err(device, "%s: kmalloc failed\n", __func__);
Ming Lei12446912012-08-04 12:01:20 +0800734 fw_priv = ERR_PTR(-ENOMEM);
735 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700738 fw_priv->nowait = nowait;
Ming Lei1f2b7952012-08-04 12:01:21 +0800739 fw_priv->fw = firmware;
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800740 INIT_DELAYED_WORK(&fw_priv->timeout_work,
741 firmware_class_timeout_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700743 f_dev = &fw_priv->dev;
744
745 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800746 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700747 f_dev->parent = device;
748 f_dev->class = &firmware_class;
Ming Lei12446912012-08-04 12:01:20 +0800749exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700750 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100752#endif /* CONFIG_FW_LOADER_USER_HELPER */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Ming Lei1f2b7952012-08-04 12:01:21 +0800754/* store the pages buffer info firmware from buf */
755static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
756{
757 fw->priv = buf;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100758#ifdef CONFIG_FW_LOADER_USER_HELPER
Ming Lei1f2b7952012-08-04 12:01:21 +0800759 fw->pages = buf->pages;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100760#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800761 fw->size = buf->size;
762 fw->data = buf->data;
763
764 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
765 __func__, buf->fw_id, buf, buf->data,
766 (unsigned int)buf->size);
767}
768
Ming Leicfe016b2012-09-08 17:32:30 +0800769#ifdef CONFIG_PM_SLEEP
Ming Leif531f05a2012-08-04 12:01:25 +0800770static void fw_name_devm_release(struct device *dev, void *res)
771{
772 struct fw_name_devm *fwn = res;
773
774 if (fwn->magic == (unsigned long)&fw_cache)
775 pr_debug("%s: fw_name-%s devm-%p released\n",
776 __func__, fwn->name, res);
777}
778
779static int fw_devm_match(struct device *dev, void *res,
780 void *match_data)
781{
782 struct fw_name_devm *fwn = res;
783
784 return (fwn->magic == (unsigned long)&fw_cache) &&
785 !strcmp(fwn->name, match_data);
786}
787
788static struct fw_name_devm *fw_find_devm_name(struct device *dev,
789 const char *name)
790{
791 struct fw_name_devm *fwn;
792
793 fwn = devres_find(dev, fw_name_devm_release,
794 fw_devm_match, (void *)name);
795 return fwn;
796}
797
798/* add firmware name into devres list */
799static int fw_add_devm_name(struct device *dev, const char *name)
800{
801 struct fw_name_devm *fwn;
802
803 fwn = fw_find_devm_name(dev, name);
804 if (fwn)
805 return 1;
806
807 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
808 strlen(name) + 1, GFP_KERNEL);
809 if (!fwn)
810 return -ENOMEM;
811
812 fwn->magic = (unsigned long)&fw_cache;
813 strcpy(fwn->name, name);
814 devres_add(dev, fwn);
815
816 return 0;
817}
Ming Leicfe016b2012-09-08 17:32:30 +0800818#else
819static int fw_add_devm_name(struct device *dev, const char *name)
820{
821 return 0;
822}
823#endif
Ming Leif531f05a2012-08-04 12:01:25 +0800824
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100825/* wait until the shared firmware_buf becomes ready (or error) */
826static int sync_cached_firmware_buf(struct firmware_buf *buf)
Ming Lei1f2b7952012-08-04 12:01:21 +0800827{
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100828 int ret = 0;
829
830 mutex_lock(&fw_lock);
831 while (!test_bit(FW_STATUS_DONE, &buf->status)) {
832 if (test_bit(FW_STATUS_ABORT, &buf->status)) {
833 ret = -ENOENT;
834 break;
835 }
836 mutex_unlock(&fw_lock);
837 wait_for_completion(&buf->completion);
838 mutex_lock(&fw_lock);
839 }
840 mutex_unlock(&fw_lock);
841 return ret;
Ming Lei1f2b7952012-08-04 12:01:21 +0800842}
843
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100844/* prepare firmware and firmware_buf structs;
845 * return 0 if a firmware is already assigned, 1 if need to load one,
846 * or a negative error code
847 */
848static int
849_request_firmware_prepare(struct firmware **firmware_p, const char *name,
850 struct device *device)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700851{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +0800853 struct firmware_buf *buf;
854 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Jiri Slaby4aed0642005-09-13 01:25:01 -0700856 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700858 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
859 __func__);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100860 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800863 if (fw_get_builtin_firmware(firmware, name)) {
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100864 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100865 return 0; /* assigned */
David Woodhouse5658c762008-05-23 13:52:42 +0100866 }
867
Ming Lei1f2b7952012-08-04 12:01:21 +0800868 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
Ming Lei1f2b7952012-08-04 12:01:21 +0800869
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100870 /*
871 * bind with 'buf' now to avoid warning in failure path
872 * of requesting firmware.
873 */
874 firmware->priv = buf;
Ming Lei1f2b7952012-08-04 12:01:21 +0800875
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100876 if (ret > 0) {
877 ret = sync_cached_firmware_buf(buf);
878 if (!ret) {
879 fw_set_page_data(buf, firmware);
880 return 0; /* assigned */
881 }
Stephen Boyddddb5542012-03-28 23:30:43 +0200882 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800883
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100884 if (ret < 0)
885 return ret;
886 return 1; /* need to load */
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200887}
888
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100889static int assign_firmware_buf(struct firmware *fw, struct device *device)
890{
891 struct firmware_buf *buf = fw->priv;
892
893 mutex_lock(&fw_lock);
894 if (!buf->size || test_bit(FW_STATUS_ABORT, &buf->status)) {
895 mutex_unlock(&fw_lock);
896 return -ENOENT;
897 }
898
899 /*
900 * add firmware name into devres list so that we can auto cache
901 * and uncache firmware for device.
902 *
903 * device may has been deleted already, but the problem
904 * should be fixed in devres or driver core.
905 */
906 if (device)
907 fw_add_devm_name(device, buf->fw_id);
908
909 /*
910 * After caching firmware image is started, let it piggyback
911 * on request firmware.
912 */
913 if (buf->fwc->state == FW_LOADER_START_CACHE) {
914 if (fw_cache_piggyback_on_request(buf->fw_id))
915 kref_get(&buf->ref);
916 }
917
918 /* pass the pages buffer to driver at the last minute */
919 fw_set_page_data(buf, fw);
920 mutex_unlock(&fw_lock);
921 return 0;
922}
923
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100924#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100925/* load a firmware via user helper */
Stephen Boyddddb5542012-03-28 23:30:43 +0200926static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
927 long timeout)
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200928{
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200929 int retval = 0;
Stephen Boyddddb5542012-03-28 23:30:43 +0200930 struct device *f_dev = &fw_priv->dev;
Ming Lei12446912012-08-04 12:01:20 +0800931 struct firmware_buf *buf = fw_priv->buf;
Ming Lei746058f2012-10-09 12:01:03 +0800932
933 /* fall back on userspace loading */
934 buf->fmt = PAGE_BUF;
Linus Torvaldscaca9512011-08-24 15:55:30 -0700935
Stephen Boyddddb5542012-03-28 23:30:43 +0200936 dev_set_uevent_suppress(f_dev, true);
David Woodhouse5658c762008-05-23 13:52:42 +0100937
Stephen Boyddddb5542012-03-28 23:30:43 +0200938 /* Need to pin this module until class device is destroyed */
939 __module_get(THIS_MODULE);
940
941 retval = device_add(f_dev);
942 if (retval) {
943 dev_err(f_dev, "%s: device_register failed\n", __func__);
944 goto err_put_dev;
945 }
946
947 retval = device_create_bin_file(f_dev, &firmware_attr_data);
948 if (retval) {
949 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
950 goto err_del_dev;
951 }
952
953 retval = device_create_file(f_dev, &dev_attr_loading);
954 if (retval) {
955 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
956 goto err_del_bin_attr;
957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Kay Sievers312c0042005-11-16 09:00:00 +0100959 if (uevent) {
Stephen Boyddddb5542012-03-28 23:30:43 +0200960 dev_set_uevent_suppress(f_dev, false);
Ming Lei12446912012-08-04 12:01:20 +0800961 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200962 if (timeout != MAX_SCHEDULE_TIMEOUT)
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800963 schedule_delayed_work(&fw_priv->timeout_work, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700965 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
966 }
967
Ming Lei12446912012-08-04 12:01:20 +0800968 wait_for_completion(&buf->completion);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700969
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800970 cancel_delayed_work_sync(&fw_priv->timeout_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Ming Lei12446912012-08-04 12:01:20 +0800972 fw_priv->buf = NULL;
Ming Lei746058f2012-10-09 12:01:03 +0800973
Stephen Boyddddb5542012-03-28 23:30:43 +0200974 device_remove_file(f_dev, &dev_attr_loading);
975err_del_bin_attr:
976 device_remove_bin_file(f_dev, &firmware_attr_data);
977err_del_dev:
978 device_del(f_dev);
979err_put_dev:
980 put_device(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return retval;
982}
983
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100984static int fw_load_from_user_helper(struct firmware *firmware,
985 const char *name, struct device *device,
986 bool uevent, bool nowait, long timeout)
987{
988 struct firmware_priv *fw_priv;
989
990 fw_priv = fw_create_instance(firmware, name, device, uevent, nowait);
991 if (IS_ERR(fw_priv))
992 return PTR_ERR(fw_priv);
993
994 fw_priv->buf = firmware->priv;
995 return _request_firmware_load(fw_priv, uevent, timeout);
996}
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100997#else /* CONFIG_FW_LOADER_USER_HELPER */
998static inline int
999fw_load_from_user_helper(struct firmware *firmware, const char *name,
1000 struct device *device, bool uevent, bool nowait,
1001 long timeout)
1002{
1003 return -ENOENT;
1004}
1005#endif /* CONFIG_FW_LOADER_USER_HELPER */
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001006
1007/* called from request_firmware() and request_firmware_work_func() */
1008static int
1009_request_firmware(const struct firmware **firmware_p, const char *name,
1010 struct device *device, bool uevent, bool nowait)
1011{
1012 struct firmware *fw;
1013 long timeout;
1014 int ret;
1015
1016 if (!firmware_p)
1017 return -EINVAL;
1018
1019 ret = _request_firmware_prepare(&fw, name, device);
1020 if (ret <= 0) /* error or already assigned */
1021 goto out;
1022
1023 ret = 0;
1024 timeout = firmware_loading_timeout();
1025 if (nowait) {
1026 timeout = usermodehelper_read_lock_wait(timeout);
1027 if (!timeout) {
1028 dev_dbg(device, "firmware: %s loading timed out\n",
1029 name);
1030 ret = -EBUSY;
1031 goto out;
1032 }
1033 } else {
1034 ret = usermodehelper_read_trylock();
1035 if (WARN_ON(ret)) {
1036 dev_err(device, "firmware: %s will not be loaded\n",
1037 name);
1038 goto out;
1039 }
1040 }
1041
1042 if (!fw_get_filesystem_firmware(device, fw->priv))
1043 ret = fw_load_from_user_helper(fw, name, device,
1044 uevent, nowait, timeout);
1045 if (!ret)
1046 ret = assign_firmware_buf(fw, device);
1047
1048 usermodehelper_read_unlock();
1049
1050 out:
1051 if (ret < 0) {
1052 release_firmware(fw);
1053 fw = NULL;
1054 }
1055
1056 *firmware_p = fw;
1057 return ret;
1058}
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060/**
Kay Sievers312c0042005-11-16 09:00:00 +01001061 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001062 * @firmware_p: pointer to firmware image
1063 * @name: name of firmware file
1064 * @device: device for which firmware is being loaded
1065 *
1066 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001067 * of @name for device @device.
1068 *
1069 * Should be called from user context where sleeping is allowed.
1070 *
Kay Sievers312c0042005-11-16 09:00:00 +01001071 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001072 * should be distinctive enough not to be confused with any other
1073 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +08001074 *
1075 * Caller must hold the reference count of @device.
Ming Lei6a927852012-11-03 17:48:16 +08001076 *
1077 * The function can be called safely inside device's suspend and
1078 * resume callback.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001079 **/
1080int
1081request_firmware(const struct firmware **firmware_p, const char *name,
1082 struct device *device)
1083{
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001084 return _request_firmware(firmware_p, name, device, true, false);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001085}
1086
1087/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001089 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001091void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
1093 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001094 if (!fw_is_builtin_firmware(fw))
1095 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 kfree(fw);
1097 }
1098}
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100/* Async support */
1101struct firmware_work {
1102 struct work_struct work;
1103 struct module *module;
1104 const char *name;
1105 struct device *device;
1106 void *context;
1107 void (*cont)(const struct firmware *fw, void *context);
Bob Liu072fc8f2011-01-26 18:33:32 +08001108 bool uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109};
1110
Stephen Boyda36cf842012-03-28 23:31:00 +02001111static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
Stephen Boyda36cf842012-03-28 23:31:00 +02001113 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 const struct firmware *fw;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001115
Stephen Boyda36cf842012-03-28 23:31:00 +02001116 fw_work = container_of(work, struct firmware_work, work);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001117
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001118 _request_firmware(&fw, fw_work->name, fw_work->device,
1119 fw_work->uevent, true);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001120 fw_work->cont(fw, fw_work->context);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001121 put_device(fw_work->device); /* taken in request_firmware_nowait() */
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 module_put(fw_work->module);
1124 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
1126
1127/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001128 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001129 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001130 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001131 * is non-zero else the firmware copy must be done manually.
1132 * @name: name of firmware file
1133 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001134 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001135 * @context: will be passed over to @cont, and
1136 * @fw may be %NULL if firmware request fails.
1137 * @cont: function will be called asynchronously when the firmware
1138 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001140 * Caller must hold the reference count of @device.
1141 *
Ming Lei6f21a622012-08-04 12:01:24 +08001142 * Asynchronous variant of request_firmware() for user contexts:
1143 * - sleep for as small periods as possible since it may
1144 * increase kernel boot time of built-in device drivers
1145 * requesting firmware in their ->probe() methods, if
1146 * @gfp is GFP_KERNEL.
1147 *
1148 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 **/
1150int
1151request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001152 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001153 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 void (*cont)(const struct firmware *fw, void *context))
1155{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001156 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001158 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 if (!fw_work)
1160 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001161
1162 fw_work->module = module;
1163 fw_work->name = name;
1164 fw_work->device = device;
1165 fw_work->context = context;
1166 fw_work->cont = cont;
1167 fw_work->uevent = uevent;
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (!try_module_get(module)) {
1170 kfree(fw_work);
1171 return -EFAULT;
1172 }
1173
Ming Lei0cfc1e12012-08-04 12:01:23 +08001174 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001175 INIT_WORK(&fw_work->work, request_firmware_work_func);
1176 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return 0;
1178}
1179
Ming Lei2887b392012-08-04 12:01:22 +08001180/**
1181 * cache_firmware - cache one firmware image in kernel memory space
1182 * @fw_name: the firmware image name
1183 *
1184 * Cache firmware in kernel memory so that drivers can use it when
1185 * system isn't ready for them to request firmware image from userspace.
1186 * Once it returns successfully, driver can use request_firmware or its
1187 * nowait version to get the cached firmware without any interacting
1188 * with userspace
1189 *
1190 * Return 0 if the firmware image has been cached successfully
1191 * Return !0 otherwise
1192 *
1193 */
1194int cache_firmware(const char *fw_name)
1195{
1196 int ret;
1197 const struct firmware *fw;
1198
1199 pr_debug("%s: %s\n", __func__, fw_name);
1200
1201 ret = request_firmware(&fw, fw_name, NULL);
1202 if (!ret)
1203 kfree(fw);
1204
1205 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1206
1207 return ret;
1208}
1209
1210/**
1211 * uncache_firmware - remove one cached firmware image
1212 * @fw_name: the firmware image name
1213 *
1214 * Uncache one firmware image which has been cached successfully
1215 * before.
1216 *
1217 * Return 0 if the firmware cache has been removed successfully
1218 * Return !0 otherwise
1219 *
1220 */
1221int uncache_firmware(const char *fw_name)
1222{
1223 struct firmware_buf *buf;
1224 struct firmware fw;
1225
1226 pr_debug("%s: %s\n", __func__, fw_name);
1227
1228 if (fw_get_builtin_firmware(&fw, fw_name))
1229 return 0;
1230
1231 buf = fw_lookup_buf(fw_name);
1232 if (buf) {
1233 fw_free_buf(buf);
1234 return 0;
1235 }
1236
1237 return -EINVAL;
1238}
1239
Ming Leicfe016b2012-09-08 17:32:30 +08001240#ifdef CONFIG_PM_SLEEP
Ming Leid28d3882012-10-09 12:01:04 +08001241static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1242
Ming Lei37276a52012-08-04 12:01:27 +08001243static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1244{
1245 struct fw_cache_entry *fce;
1246
1247 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1248 if (!fce)
1249 goto exit;
1250
1251 strcpy(fce->name, name);
1252exit:
1253 return fce;
1254}
1255
Ming Lei373304f2012-10-09 12:01:01 +08001256static int __fw_entry_found(const char *name)
1257{
1258 struct firmware_cache *fwc = &fw_cache;
1259 struct fw_cache_entry *fce;
1260
1261 list_for_each_entry(fce, &fwc->fw_names, list) {
1262 if (!strcmp(fce->name, name))
1263 return 1;
1264 }
1265 return 0;
1266}
1267
Ming Leiac39b3e2012-08-20 19:04:16 +08001268static int fw_cache_piggyback_on_request(const char *name)
1269{
1270 struct firmware_cache *fwc = &fw_cache;
1271 struct fw_cache_entry *fce;
1272 int ret = 0;
1273
1274 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001275 if (__fw_entry_found(name))
1276 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001277
1278 fce = alloc_fw_cache_entry(name);
1279 if (fce) {
1280 ret = 1;
1281 list_add(&fce->list, &fwc->fw_names);
1282 pr_debug("%s: fw: %s\n", __func__, name);
1283 }
1284found:
1285 spin_unlock(&fwc->name_lock);
1286 return ret;
1287}
1288
Ming Lei37276a52012-08-04 12:01:27 +08001289static void free_fw_cache_entry(struct fw_cache_entry *fce)
1290{
1291 kfree(fce);
1292}
1293
1294static void __async_dev_cache_fw_image(void *fw_entry,
1295 async_cookie_t cookie)
1296{
1297 struct fw_cache_entry *fce = fw_entry;
1298 struct firmware_cache *fwc = &fw_cache;
1299 int ret;
1300
1301 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001302 if (ret) {
1303 spin_lock(&fwc->name_lock);
1304 list_del(&fce->list);
1305 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001306
Ming Leiac39b3e2012-08-20 19:04:16 +08001307 free_fw_cache_entry(fce);
1308 }
Ming Lei37276a52012-08-04 12:01:27 +08001309}
1310
1311/* called with dev->devres_lock held */
1312static void dev_create_fw_entry(struct device *dev, void *res,
1313 void *data)
1314{
1315 struct fw_name_devm *fwn = res;
1316 const char *fw_name = fwn->name;
1317 struct list_head *head = data;
1318 struct fw_cache_entry *fce;
1319
1320 fce = alloc_fw_cache_entry(fw_name);
1321 if (fce)
1322 list_add(&fce->list, head);
1323}
1324
1325static int devm_name_match(struct device *dev, void *res,
1326 void *match_data)
1327{
1328 struct fw_name_devm *fwn = res;
1329 return (fwn->magic == (unsigned long)match_data);
1330}
1331
Ming Leiab6dd8e2012-08-17 22:07:00 +08001332static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001333{
1334 LIST_HEAD(todo);
1335 struct fw_cache_entry *fce;
1336 struct fw_cache_entry *fce_next;
1337 struct firmware_cache *fwc = &fw_cache;
1338
1339 devres_for_each_res(dev, fw_name_devm_release,
1340 devm_name_match, &fw_cache,
1341 dev_create_fw_entry, &todo);
1342
1343 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1344 list_del(&fce->list);
1345
1346 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001347 /* only one cache entry for one firmware */
1348 if (!__fw_entry_found(fce->name)) {
Ming Lei373304f2012-10-09 12:01:01 +08001349 list_add(&fce->list, &fwc->fw_names);
1350 } else {
1351 free_fw_cache_entry(fce);
1352 fce = NULL;
1353 }
Ming Lei37276a52012-08-04 12:01:27 +08001354 spin_unlock(&fwc->name_lock);
1355
Ming Lei373304f2012-10-09 12:01:01 +08001356 if (fce)
Ming Leid28d3882012-10-09 12:01:04 +08001357 async_schedule_domain(__async_dev_cache_fw_image,
1358 (void *)fce,
1359 &fw_cache_domain);
Ming Lei37276a52012-08-04 12:01:27 +08001360 }
1361}
1362
1363static void __device_uncache_fw_images(void)
1364{
1365 struct firmware_cache *fwc = &fw_cache;
1366 struct fw_cache_entry *fce;
1367
1368 spin_lock(&fwc->name_lock);
1369 while (!list_empty(&fwc->fw_names)) {
1370 fce = list_entry(fwc->fw_names.next,
1371 struct fw_cache_entry, list);
1372 list_del(&fce->list);
1373 spin_unlock(&fwc->name_lock);
1374
1375 uncache_firmware(fce->name);
1376 free_fw_cache_entry(fce);
1377
1378 spin_lock(&fwc->name_lock);
1379 }
1380 spin_unlock(&fwc->name_lock);
1381}
1382
1383/**
1384 * device_cache_fw_images - cache devices' firmware
1385 *
1386 * If one device called request_firmware or its nowait version
1387 * successfully before, the firmware names are recored into the
1388 * device's devres link list, so device_cache_fw_images can call
1389 * cache_firmware() to cache these firmwares for the device,
1390 * then the device driver can load its firmwares easily at
1391 * time when system is not ready to complete loading firmware.
1392 */
1393static void device_cache_fw_images(void)
1394{
1395 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001396 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001397 DEFINE_WAIT(wait);
1398
1399 pr_debug("%s\n", __func__);
1400
Ming Lei373304f2012-10-09 12:01:01 +08001401 /* cancel uncache work */
1402 cancel_delayed_work_sync(&fwc->work);
1403
Ming Leiffe53f62012-08-04 12:01:28 +08001404 /*
1405 * use small loading timeout for caching devices' firmware
1406 * because all these firmware images have been loaded
1407 * successfully at lease once, also system is ready for
1408 * completing firmware loading now. The maximum size of
1409 * firmware in current distributions is about 2M bytes,
1410 * so 10 secs should be enough.
1411 */
1412 old_timeout = loading_timeout;
1413 loading_timeout = 10;
1414
Ming Leiac39b3e2012-08-20 19:04:16 +08001415 mutex_lock(&fw_lock);
1416 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001417 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001418 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001419
1420 /* wait for completion of caching firmware for all devices */
Ming Leid28d3882012-10-09 12:01:04 +08001421 async_synchronize_full_domain(&fw_cache_domain);
Ming Leiffe53f62012-08-04 12:01:28 +08001422
1423 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001424}
1425
1426/**
1427 * device_uncache_fw_images - uncache devices' firmware
1428 *
1429 * uncache all firmwares which have been cached successfully
1430 * by device_uncache_fw_images earlier
1431 */
1432static void device_uncache_fw_images(void)
1433{
1434 pr_debug("%s\n", __func__);
1435 __device_uncache_fw_images();
1436}
1437
1438static void device_uncache_fw_images_work(struct work_struct *work)
1439{
1440 device_uncache_fw_images();
1441}
1442
1443/**
1444 * device_uncache_fw_images_delay - uncache devices firmwares
1445 * @delay: number of milliseconds to delay uncache device firmwares
1446 *
1447 * uncache all devices's firmwares which has been cached successfully
1448 * by device_cache_fw_images after @delay milliseconds.
1449 */
1450static void device_uncache_fw_images_delay(unsigned long delay)
1451{
1452 schedule_delayed_work(&fw_cache.work,
1453 msecs_to_jiffies(delay));
1454}
1455
Ming Lei07646d92012-08-04 12:01:29 +08001456static int fw_pm_notify(struct notifier_block *notify_block,
1457 unsigned long mode, void *unused)
1458{
1459 switch (mode) {
1460 case PM_HIBERNATION_PREPARE:
1461 case PM_SUSPEND_PREPARE:
1462 device_cache_fw_images();
1463 break;
1464
1465 case PM_POST_SUSPEND:
1466 case PM_POST_HIBERNATION:
1467 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001468 /*
1469 * In case that system sleep failed and syscore_suspend is
1470 * not called.
1471 */
1472 mutex_lock(&fw_lock);
1473 fw_cache.state = FW_LOADER_NO_CACHE;
1474 mutex_unlock(&fw_lock);
1475
Ming Lei07646d92012-08-04 12:01:29 +08001476 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1477 break;
1478 }
1479
1480 return 0;
1481}
Ming Lei07646d92012-08-04 12:01:29 +08001482
Ming Leiac39b3e2012-08-20 19:04:16 +08001483/* stop caching firmware once syscore_suspend is reached */
1484static int fw_suspend(void)
1485{
1486 fw_cache.state = FW_LOADER_NO_CACHE;
1487 return 0;
1488}
1489
1490static struct syscore_ops fw_syscore_ops = {
1491 .suspend = fw_suspend,
1492};
Ming Leicfe016b2012-09-08 17:32:30 +08001493#else
1494static int fw_cache_piggyback_on_request(const char *name)
1495{
1496 return 0;
1497}
1498#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001499
Ming Lei37276a52012-08-04 12:01:27 +08001500static void __init fw_cache_init(void)
1501{
1502 spin_lock_init(&fw_cache.lock);
1503 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001504 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001505
Ming Leicfe016b2012-09-08 17:32:30 +08001506#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +08001507 spin_lock_init(&fw_cache.name_lock);
1508 INIT_LIST_HEAD(&fw_cache.fw_names);
Ming Lei37276a52012-08-04 12:01:27 +08001509
Ming Lei37276a52012-08-04 12:01:27 +08001510 INIT_DELAYED_WORK(&fw_cache.work,
1511 device_uncache_fw_images_work);
Ming Lei07646d92012-08-04 12:01:29 +08001512
1513 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1514 register_pm_notifier(&fw_cache.pm_notify);
Ming Leiac39b3e2012-08-20 19:04:16 +08001515
1516 register_syscore_ops(&fw_syscore_ops);
Ming Leicfe016b2012-09-08 17:32:30 +08001517#endif
Ming Lei37276a52012-08-04 12:01:27 +08001518}
1519
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001520static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
Ming Lei1f2b7952012-08-04 12:01:21 +08001522 fw_cache_init();
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001523#ifdef CONFIG_FW_LOADER_USER_HELPER
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001524 return class_register(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001525#else
1526 return 0;
1527#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001529
1530static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
Ming Leicfe016b2012-09-08 17:32:30 +08001532#ifdef CONFIG_PM_SLEEP
Ming Leiac39b3e2012-08-20 19:04:16 +08001533 unregister_syscore_ops(&fw_syscore_ops);
Ming Lei07646d92012-08-04 12:01:29 +08001534 unregister_pm_notifier(&fw_cache.pm_notify);
Ming Leicfe016b2012-09-08 17:32:30 +08001535#endif
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001536#ifdef CONFIG_FW_LOADER_USER_HELPER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 class_unregister(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001538#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
Shaohua Lia30a6a22006-09-27 01:50:52 -07001541fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542module_exit(firmware_class_exit);
1543
1544EXPORT_SYMBOL(release_firmware);
1545EXPORT_SYMBOL(request_firmware);
1546EXPORT_SYMBOL(request_firmware_nowait);
Ming Lei2887b392012-08-04 12:01:22 +08001547EXPORT_SYMBOL_GPL(cache_firmware);
1548EXPORT_SYMBOL_GPL(uncache_firmware);