blob: b44ed35ac38d1cba7f58c8be3e10f4ad5f757087 [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
Ming Lei746058f2012-10-09 12:01:03 +080091enum fw_buf_fmt {
92 VMALLOC_BUF, /* used in direct loading */
93 PAGE_BUF, /* used in loading via userspace */
94};
95
Dave Jones2f651682007-01-25 15:56:15 -050096static int loading_timeout = 60; /* In seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +020098static inline long firmware_loading_timeout(void)
99{
100 return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
101}
102
Ming Lei1f2b7952012-08-04 12:01:21 +0800103struct firmware_cache {
104 /* firmware_buf instance will be added into the below list */
105 spinlock_t lock;
106 struct list_head head;
Ming Leicfe016b2012-09-08 17:32:30 +0800107 int state;
Ming Lei37276a52012-08-04 12:01:27 +0800108
Ming Leicfe016b2012-09-08 17:32:30 +0800109#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +0800110 /*
111 * Names of firmware images which have been cached successfully
112 * will be added into the below list so that device uncache
113 * helper can trace which firmware images have been cached
114 * before.
115 */
116 spinlock_t name_lock;
117 struct list_head fw_names;
118
Ming Lei37276a52012-08-04 12:01:27 +0800119 struct delayed_work work;
Ming Lei07646d92012-08-04 12:01:29 +0800120
121 struct notifier_block pm_notify;
Ming Leicfe016b2012-09-08 17:32:30 +0800122#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800123};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Ming Lei12446912012-08-04 12:01:20 +0800125struct firmware_buf {
Ming Lei1f2b7952012-08-04 12:01:21 +0800126 struct kref ref;
127 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 struct completion completion;
Ming Lei1f2b7952012-08-04 12:01:21 +0800129 struct firmware_cache *fwc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 unsigned long status;
Ming Lei746058f2012-10-09 12:01:03 +0800131 enum fw_buf_fmt fmt;
Ming Lei65710cb2012-08-04 12:01:16 +0800132 void *data;
133 size_t size;
David Woodhouse6e03a202009-04-09 22:04:07 -0700134 struct page **pages;
135 int nr_pages;
136 int page_array_size;
Dmitry Torokhove1771232010-03-13 23:49:23 -0800137 char fw_id[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
Ming Lei37276a52012-08-04 12:01:27 +0800140struct fw_cache_entry {
141 struct list_head list;
142 char name[];
143};
144
Ming Lei12446912012-08-04 12:01:20 +0800145struct firmware_priv {
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800146 struct delayed_work timeout_work;
Ming Lei12446912012-08-04 12:01:20 +0800147 bool nowait;
148 struct device dev;
149 struct firmware_buf *buf;
Ming Lei1f2b7952012-08-04 12:01:21 +0800150 struct firmware *fw;
Ming Lei12446912012-08-04 12:01:20 +0800151};
152
Ming Leif531f05a2012-08-04 12:01:25 +0800153struct fw_name_devm {
154 unsigned long magic;
155 char name[];
156};
157
Ming Lei1f2b7952012-08-04 12:01:21 +0800158#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
159
Ming Leiac39b3e2012-08-20 19:04:16 +0800160#define FW_LOADER_NO_CACHE 0
161#define FW_LOADER_START_CACHE 1
162
163static int fw_cache_piggyback_on_request(const char *name);
164
Ming Lei1f2b7952012-08-04 12:01:21 +0800165/* fw_lock could be moved to 'struct firmware_priv' but since it is just
166 * guarding for corner cases a global lock should be OK */
167static DEFINE_MUTEX(fw_lock);
168
169static struct firmware_cache fw_cache;
170
171static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
172 struct firmware_cache *fwc)
173{
174 struct firmware_buf *buf;
175
176 buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
177
178 if (!buf)
179 return buf;
180
181 kref_init(&buf->ref);
182 strcpy(buf->fw_id, fw_name);
183 buf->fwc = fwc;
184 init_completion(&buf->completion);
Ming Lei746058f2012-10-09 12:01:03 +0800185 buf->fmt = VMALLOC_BUF;
Ming Lei1f2b7952012-08-04 12:01:21 +0800186
187 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
188
189 return buf;
190}
191
Ming Lei2887b392012-08-04 12:01:22 +0800192static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
193{
194 struct firmware_buf *tmp;
195 struct firmware_cache *fwc = &fw_cache;
196
197 list_for_each_entry(tmp, &fwc->head, list)
198 if (!strcmp(tmp->fw_id, fw_name))
199 return tmp;
200 return NULL;
201}
202
Ming Lei1f2b7952012-08-04 12:01:21 +0800203static int fw_lookup_and_allocate_buf(const char *fw_name,
204 struct firmware_cache *fwc,
205 struct firmware_buf **buf)
206{
207 struct firmware_buf *tmp;
208
209 spin_lock(&fwc->lock);
Ming Lei2887b392012-08-04 12:01:22 +0800210 tmp = __fw_lookup_buf(fw_name);
211 if (tmp) {
212 kref_get(&tmp->ref);
213 spin_unlock(&fwc->lock);
214 *buf = tmp;
215 return 1;
216 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800217 tmp = __allocate_fw_buf(fw_name, fwc);
218 if (tmp)
219 list_add(&tmp->list, &fwc->head);
220 spin_unlock(&fwc->lock);
221
222 *buf = tmp;
223
224 return tmp ? 0 : -ENOMEM;
225}
226
Ming Lei2887b392012-08-04 12:01:22 +0800227static struct firmware_buf *fw_lookup_buf(const char *fw_name)
228{
229 struct firmware_buf *tmp;
230 struct firmware_cache *fwc = &fw_cache;
231
232 spin_lock(&fwc->lock);
233 tmp = __fw_lookup_buf(fw_name);
234 spin_unlock(&fwc->lock);
235
236 return tmp;
237}
238
Ming Lei1f2b7952012-08-04 12:01:21 +0800239static void __fw_free_buf(struct kref *ref)
240{
241 struct firmware_buf *buf = to_fwbuf(ref);
242 struct firmware_cache *fwc = buf->fwc;
243 int i;
244
245 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
246 __func__, buf->fw_id, buf, buf->data,
247 (unsigned int)buf->size);
248
249 spin_lock(&fwc->lock);
250 list_del(&buf->list);
251 spin_unlock(&fwc->lock);
252
Ming Lei746058f2012-10-09 12:01:03 +0800253
254 if (buf->fmt == PAGE_BUF) {
255 vunmap(buf->data);
256 for (i = 0; i < buf->nr_pages; i++)
257 __free_page(buf->pages[i]);
258 kfree(buf->pages);
259 } else
260 vfree(buf->data);
Ming Lei1f2b7952012-08-04 12:01:21 +0800261 kfree(buf);
262}
263
264static void fw_free_buf(struct firmware_buf *buf)
265{
266 kref_put(&buf->ref, __fw_free_buf);
267}
268
Ming Lei746058f2012-10-09 12:01:03 +0800269/* direct firmware loading support */
270static const char *fw_path[] = {
271 "/lib/firmware/updates/" UTS_RELEASE,
272 "/lib/firmware/updates",
273 "/lib/firmware/" UTS_RELEASE,
274 "/lib/firmware"
275};
276
277/* Don't inline this: 'struct kstat' is biggish */
278static noinline long fw_file_size(struct file *file)
279{
280 struct kstat st;
281 if (vfs_getattr(file->f_path.mnt, file->f_path.dentry, &st))
282 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
290static bool fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf)
291{
292 long size;
293 char *buf;
294
295 size = fw_file_size(file);
296 if (size < 0)
297 return false;
298 buf = vmalloc(size);
299 if (!buf)
300 return false;
301 if (kernel_read(file, 0, buf, size) != size) {
302 vfree(buf);
303 return false;
304 }
305 fw_buf->data = buf;
306 fw_buf->size = size;
307 return true;
308}
309
310static bool fw_get_filesystem_firmware(struct firmware_buf *buf)
311{
312 int i;
313 bool success = false;
314 char *path = __getname();
315
316 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
317 struct file *file;
318 snprintf(path, PATH_MAX, "%s/%s", fw_path[i], buf->fw_id);
319
320 file = filp_open(path, O_RDONLY, 0);
321 if (IS_ERR(file))
322 continue;
323 success = fw_read_file_contents(file, buf);
324 fput(file);
325 if (success)
326 break;
327 }
328 __putname(path);
329 return success;
330}
331
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700332static struct firmware_priv *to_firmware_priv(struct device *dev)
333{
334 return container_of(dev, struct firmware_priv, dev);
335}
336
337static void fw_load_abort(struct firmware_priv *fw_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Ming Lei12446912012-08-04 12:01:20 +0800339 struct firmware_buf *buf = fw_priv->buf;
340
341 set_bit(FW_STATUS_ABORT, &buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800342 complete_all(&buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700345static ssize_t firmware_timeout_show(struct class *class,
346 struct class_attribute *attr,
347 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 return sprintf(buf, "%d\n", loading_timeout);
350}
351
352/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800353 * firmware_timeout_store - set number of seconds to wait for firmware
354 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800355 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800356 * @buf: buffer to scan for timeout value
357 * @count: number of bytes in @buf
358 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800360 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 * firmware will be provided.
362 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800363 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700365static ssize_t firmware_timeout_store(struct class *class,
366 struct class_attribute *attr,
367 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700370 if (loading_timeout < 0)
371 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return count;
374}
375
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800376static struct class_attribute firmware_class_attrs[] = {
377 __ATTR(timeout, S_IWUSR | S_IRUGO,
378 firmware_timeout_show, firmware_timeout_store),
379 __ATTR_NULL
380};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800382static void fw_dev_release(struct device *dev)
383{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700384 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800385
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800386 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800387
388 module_put(THIS_MODULE);
389}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Kay Sievers7eff2e72007-08-14 15:15:12 +0200391static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700393 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Ming Lei12446912012-08-04 12:01:20 +0800395 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200397 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700398 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200399 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
400 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 return 0;
403}
404
Adrian Bunk1b81d662006-05-20 15:00:16 -0700405static struct class firmware_class = {
406 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800407 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700408 .dev_uevent = firmware_uevent,
409 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700410};
411
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700412static ssize_t firmware_loading_show(struct device *dev,
413 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700415 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800416 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return sprintf(buf, "%d\n", loading);
419}
420
Ming Lei65710cb2012-08-04 12:01:16 +0800421/* firmware holds the ownership of pages */
David Woodhousedd336c52010-05-02 11:21:21 +0300422static void firmware_free_data(const struct firmware *fw)
423{
Linus Torvaldsabb139e2012-10-03 15:58:32 -0700424 /* Loaded directly? */
425 if (!fw->priv) {
426 vfree(fw->data);
427 return;
428 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800429 fw_free_buf(fw->priv);
David Woodhousedd336c52010-05-02 11:21:21 +0300430}
431
David Woodhouse6e03a202009-04-09 22:04:07 -0700432/* Some architectures don't have PAGE_KERNEL_RO */
433#ifndef PAGE_KERNEL_RO
434#define PAGE_KERNEL_RO PAGE_KERNEL
435#endif
Ming Lei253c9242012-10-09 12:01:02 +0800436
437/* one pages buffer should be mapped/unmapped only once */
438static int fw_map_pages_buf(struct firmware_buf *buf)
439{
Ming Lei746058f2012-10-09 12:01:03 +0800440 if (buf->fmt != PAGE_BUF)
441 return 0;
442
Ming Lei253c9242012-10-09 12:01:02 +0800443 if (buf->data)
444 vunmap(buf->data);
445 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
446 if (!buf->data)
447 return -ENOMEM;
448 return 0;
449}
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800452 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700453 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800454 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800455 * @buf: buffer to scan for loading control value
456 * @count: number of bytes in @buf
457 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 * The relevant values are:
459 *
460 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800461 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 * -1: Conclude the load with an error and discard any written data.
463 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700464static ssize_t firmware_loading_store(struct device *dev,
465 struct device_attribute *attr,
466 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700468 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800469 struct firmware_buf *fw_buf = fw_priv->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700471 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Neil Hormaneea915b2012-01-02 15:31:23 -0500473 mutex_lock(&fw_lock);
474
Ming Lei12446912012-08-04 12:01:20 +0800475 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500476 goto out;
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 switch (loading) {
479 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800480 /* discarding any previous partial load */
Ming Lei12446912012-08-04 12:01:20 +0800481 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
482 for (i = 0; i < fw_buf->nr_pages; i++)
483 __free_page(fw_buf->pages[i]);
484 kfree(fw_buf->pages);
485 fw_buf->pages = NULL;
486 fw_buf->page_array_size = 0;
487 fw_buf->nr_pages = 0;
488 set_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei28eefa72012-08-04 12:01:17 +0800489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 break;
491 case 0:
Ming Lei12446912012-08-04 12:01:20 +0800492 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
493 set_bit(FW_STATUS_DONE, &fw_buf->status);
494 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei253c9242012-10-09 12:01:02 +0800495
496 /*
497 * Several loading requests may be pending on
498 * one same firmware buf, so let all requests
499 * see the mapped 'buf->data' once the loading
500 * is completed.
501 * */
502 fw_map_pages_buf(fw_buf);
Ming Lei1f2b7952012-08-04 12:01:21 +0800503 complete_all(&fw_buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 break;
505 }
506 /* fallthrough */
507 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700508 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 /* fallthrough */
510 case -1:
511 fw_load_abort(fw_priv);
512 break;
513 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500514out:
515 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return count;
517}
518
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700519static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700521static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
522 struct bin_attribute *bin_attr,
523 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200525 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700526 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800527 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700528 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Laura Garciacad1e552006-05-23 23:22:38 +0200530 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800531 buf = fw_priv->buf;
532 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 ret_count = -ENODEV;
534 goto out;
535 }
Ming Lei12446912012-08-04 12:01:20 +0800536 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200537 ret_count = 0;
538 goto out;
539 }
Ming Lei12446912012-08-04 12:01:20 +0800540 if (count > buf->size - offset)
541 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700542
543 ret_count = count;
544
545 while (count) {
546 void *page_data;
547 int page_nr = offset >> PAGE_SHIFT;
548 int page_ofs = offset & (PAGE_SIZE-1);
549 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
550
Ming Lei12446912012-08-04 12:01:20 +0800551 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700552
553 memcpy(buffer, page_data + page_ofs, page_cnt);
554
Ming Lei12446912012-08-04 12:01:20 +0800555 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700556 buffer += page_cnt;
557 offset += page_cnt;
558 count -= page_cnt;
559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560out:
Laura Garciacad1e552006-05-23 23:22:38 +0200561 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return ret_count;
563}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800564
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700565static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Ming Lei12446912012-08-04 12:01:20 +0800567 struct firmware_buf *buf = fw_priv->buf;
David Woodhouse6e03a202009-04-09 22:04:07 -0700568 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
David Woodhouse6e03a202009-04-09 22:04:07 -0700570 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800571 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700572 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800573 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700574 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
David Woodhouse6e03a202009-04-09 22:04:07 -0700576 new_pages = kmalloc(new_array_size * sizeof(void *),
577 GFP_KERNEL);
578 if (!new_pages) {
579 fw_load_abort(fw_priv);
580 return -ENOMEM;
581 }
Ming Lei12446912012-08-04 12:01:20 +0800582 memcpy(new_pages, buf->pages,
583 buf->page_array_size * sizeof(void *));
584 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
585 (new_array_size - buf->page_array_size));
586 kfree(buf->pages);
587 buf->pages = new_pages;
588 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700590
Ming Lei12446912012-08-04 12:01:20 +0800591 while (buf->nr_pages < pages_needed) {
592 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700593 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
594
Ming Lei12446912012-08-04 12:01:20 +0800595 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700596 fw_load_abort(fw_priv);
597 return -ENOMEM;
598 }
Ming Lei12446912012-08-04 12:01:20 +0800599 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return 0;
602}
603
604/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800605 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700606 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700607 * @kobj: kobject for the device
Randy Dunlap42e61f4a2007-07-23 21:42:11 -0700608 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800609 * @buffer: buffer being written
610 * @offset: buffer offset for write in total data store area
611 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800613 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 * the driver as a firmware image.
615 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700616static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
617 struct bin_attribute *bin_attr,
618 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200620 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700621 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800622 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 ssize_t retval;
624
625 if (!capable(CAP_SYS_RAWIO))
626 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700627
Laura Garciacad1e552006-05-23 23:22:38 +0200628 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800629 buf = fw_priv->buf;
630 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 retval = -ENODEV;
632 goto out;
633 }
Ming Lei65710cb2012-08-04 12:01:16 +0800634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 retval = fw_realloc_buffer(fw_priv, offset + count);
636 if (retval)
637 goto out;
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700640
641 while (count) {
642 void *page_data;
643 int page_nr = offset >> PAGE_SHIFT;
644 int page_ofs = offset & (PAGE_SIZE - 1);
645 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
646
Ming Lei12446912012-08-04 12:01:20 +0800647 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700648
649 memcpy(page_data + page_ofs, buffer, page_cnt);
650
Ming Lei12446912012-08-04 12:01:20 +0800651 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700652 buffer += page_cnt;
653 offset += page_cnt;
654 count -= page_cnt;
655 }
656
Ming Lei12446912012-08-04 12:01:20 +0800657 buf->size = max_t(size_t, offset, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658out:
Laura Garciacad1e552006-05-23 23:22:38 +0200659 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return retval;
661}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800662
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700663static struct bin_attribute firmware_attr_data = {
664 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 .size = 0,
666 .read = firmware_data_read,
667 .write = firmware_data_write,
668};
669
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800670static void firmware_class_timeout_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800672 struct firmware_priv *fw_priv = container_of(work,
673 struct firmware_priv, timeout_work.work);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700674
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800675 mutex_lock(&fw_lock);
676 if (test_bit(FW_STATUS_DONE, &(fw_priv->buf->status))) {
677 mutex_unlock(&fw_lock);
678 return;
679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 fw_load_abort(fw_priv);
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800681 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700684static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200685fw_create_instance(struct firmware *firmware, const char *fw_name,
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700686 struct device *device, bool uevent, bool nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700688 struct firmware_priv *fw_priv;
689 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Ming Lei12446912012-08-04 12:01:20 +0800691 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700692 if (!fw_priv) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700693 dev_err(device, "%s: kmalloc failed\n", __func__);
Ming Lei12446912012-08-04 12:01:20 +0800694 fw_priv = ERR_PTR(-ENOMEM);
695 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700698 fw_priv->nowait = nowait;
Ming Lei1f2b7952012-08-04 12:01:21 +0800699 fw_priv->fw = firmware;
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800700 INIT_DELAYED_WORK(&fw_priv->timeout_work,
701 firmware_class_timeout_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700703 f_dev = &fw_priv->dev;
704
705 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800706 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700707 f_dev->parent = device;
708 f_dev->class = &firmware_class;
Ming Lei12446912012-08-04 12:01:20 +0800709exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700710 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
Ming Lei1f2b7952012-08-04 12:01:21 +0800713/* store the pages buffer info firmware from buf */
714static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
715{
716 fw->priv = buf;
717 fw->pages = buf->pages;
718 fw->size = buf->size;
719 fw->data = buf->data;
720
721 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
722 __func__, buf->fw_id, buf, buf->data,
723 (unsigned int)buf->size);
724}
725
Ming Leicfe016b2012-09-08 17:32:30 +0800726#ifdef CONFIG_PM_SLEEP
Ming Leif531f05a2012-08-04 12:01:25 +0800727static void fw_name_devm_release(struct device *dev, void *res)
728{
729 struct fw_name_devm *fwn = res;
730
731 if (fwn->magic == (unsigned long)&fw_cache)
732 pr_debug("%s: fw_name-%s devm-%p released\n",
733 __func__, fwn->name, res);
734}
735
736static int fw_devm_match(struct device *dev, void *res,
737 void *match_data)
738{
739 struct fw_name_devm *fwn = res;
740
741 return (fwn->magic == (unsigned long)&fw_cache) &&
742 !strcmp(fwn->name, match_data);
743}
744
745static struct fw_name_devm *fw_find_devm_name(struct device *dev,
746 const char *name)
747{
748 struct fw_name_devm *fwn;
749
750 fwn = devres_find(dev, fw_name_devm_release,
751 fw_devm_match, (void *)name);
752 return fwn;
753}
754
755/* add firmware name into devres list */
756static int fw_add_devm_name(struct device *dev, const char *name)
757{
758 struct fw_name_devm *fwn;
759
760 fwn = fw_find_devm_name(dev, name);
761 if (fwn)
762 return 1;
763
764 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
765 strlen(name) + 1, GFP_KERNEL);
766 if (!fwn)
767 return -ENOMEM;
768
769 fwn->magic = (unsigned long)&fw_cache;
770 strcpy(fwn->name, name);
771 devres_add(dev, fwn);
772
773 return 0;
774}
Ming Leicfe016b2012-09-08 17:32:30 +0800775#else
776static int fw_add_devm_name(struct device *dev, const char *name)
777{
778 return 0;
779}
780#endif
Ming Leif531f05a2012-08-04 12:01:25 +0800781
Ming Lei1f2b7952012-08-04 12:01:21 +0800782static void _request_firmware_cleanup(const struct firmware **firmware_p)
783{
784 release_firmware(*firmware_p);
785 *firmware_p = NULL;
786}
787
Stephen Boyddddb5542012-03-28 23:30:43 +0200788static struct firmware_priv *
789_request_firmware_prepare(const struct firmware **firmware_p, const char *name,
790 struct device *device, bool uevent, bool nowait)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700791{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +0800793 struct firmware_priv *fw_priv = NULL;
794 struct firmware_buf *buf;
795 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 if (!firmware_p)
Stephen Boyddddb5542012-03-28 23:30:43 +0200798 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Jiri Slaby4aed0642005-09-13 01:25:01 -0700800 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700802 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
803 __func__);
Stephen Boyddddb5542012-03-28 23:30:43 +0200804 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800807 if (fw_get_builtin_firmware(firmware, name)) {
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100808 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
Stephen Boyddddb5542012-03-28 23:30:43 +0200809 return NULL;
David Woodhouse5658c762008-05-23 13:52:42 +0100810 }
811
Ming Lei1f2b7952012-08-04 12:01:21 +0800812 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
813 if (!ret)
814 fw_priv = fw_create_instance(firmware, name, device,
815 uevent, nowait);
816
817 if (IS_ERR(fw_priv) || ret < 0) {
818 kfree(firmware);
Stephen Boyddddb5542012-03-28 23:30:43 +0200819 *firmware_p = NULL;
Ming Lei1f2b7952012-08-04 12:01:21 +0800820 return ERR_PTR(-ENOMEM);
821 } else if (fw_priv) {
822 fw_priv->buf = buf;
823
824 /*
825 * bind with 'buf' now to avoid warning in failure path
826 * of requesting firmware.
827 */
828 firmware->priv = buf;
829 return fw_priv;
Stephen Boyddddb5542012-03-28 23:30:43 +0200830 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800831
832 /* share the cached buf, which is inprogessing or completed */
833 check_status:
834 mutex_lock(&fw_lock);
835 if (test_bit(FW_STATUS_ABORT, &buf->status)) {
836 fw_priv = ERR_PTR(-ENOENT);
Ming Leief40bb12012-08-20 19:04:15 +0800837 firmware->priv = buf;
Ming Lei1f2b7952012-08-04 12:01:21 +0800838 _request_firmware_cleanup(firmware_p);
839 goto exit;
840 } else if (test_bit(FW_STATUS_DONE, &buf->status)) {
841 fw_priv = NULL;
842 fw_set_page_data(buf, firmware);
843 goto exit;
844 }
845 mutex_unlock(&fw_lock);
846 wait_for_completion(&buf->completion);
847 goto check_status;
848
849exit:
850 mutex_unlock(&fw_lock);
Stephen Boyddddb5542012-03-28 23:30:43 +0200851 return fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200852}
853
Stephen Boyddddb5542012-03-28 23:30:43 +0200854static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
855 long timeout)
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200856{
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200857 int retval = 0;
Stephen Boyddddb5542012-03-28 23:30:43 +0200858 struct device *f_dev = &fw_priv->dev;
Ming Lei12446912012-08-04 12:01:20 +0800859 struct firmware_buf *buf = fw_priv->buf;
Ming Leiac39b3e2012-08-20 19:04:16 +0800860 struct firmware_cache *fwc = &fw_cache;
Ming Lei746058f2012-10-09 12:01:03 +0800861 int direct_load = 0;
862
863 /* try direct loading from fs first */
864 if (fw_get_filesystem_firmware(buf)) {
865 dev_dbg(f_dev->parent, "firmware: direct-loading"
866 " firmware %s\n", buf->fw_id);
867
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800868 mutex_lock(&fw_lock);
Ming Lei746058f2012-10-09 12:01:03 +0800869 set_bit(FW_STATUS_DONE, &buf->status);
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800870 mutex_unlock(&fw_lock);
Ming Lei746058f2012-10-09 12:01:03 +0800871 complete_all(&buf->completion);
872 direct_load = 1;
873 goto handle_fw;
874 }
875
876 /* fall back on userspace loading */
877 buf->fmt = PAGE_BUF;
Linus Torvaldscaca9512011-08-24 15:55:30 -0700878
Stephen Boyddddb5542012-03-28 23:30:43 +0200879 dev_set_uevent_suppress(f_dev, true);
David Woodhouse5658c762008-05-23 13:52:42 +0100880
Stephen Boyddddb5542012-03-28 23:30:43 +0200881 /* Need to pin this module until class device is destroyed */
882 __module_get(THIS_MODULE);
883
884 retval = device_add(f_dev);
885 if (retval) {
886 dev_err(f_dev, "%s: device_register failed\n", __func__);
887 goto err_put_dev;
888 }
889
890 retval = device_create_bin_file(f_dev, &firmware_attr_data);
891 if (retval) {
892 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
893 goto err_del_dev;
894 }
895
896 retval = device_create_file(f_dev, &dev_attr_loading);
897 if (retval) {
898 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
899 goto err_del_bin_attr;
900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Kay Sievers312c0042005-11-16 09:00:00 +0100902 if (uevent) {
Stephen Boyddddb5542012-03-28 23:30:43 +0200903 dev_set_uevent_suppress(f_dev, false);
Ming Lei12446912012-08-04 12:01:20 +0800904 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200905 if (timeout != MAX_SCHEDULE_TIMEOUT)
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800906 schedule_delayed_work(&fw_priv->timeout_work, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700908 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
909 }
910
Ming Lei12446912012-08-04 12:01:20 +0800911 wait_for_completion(&buf->completion);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700912
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800913 cancel_delayed_work_sync(&fw_priv->timeout_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Ming Lei746058f2012-10-09 12:01:03 +0800915handle_fw:
Laura Garciacad1e552006-05-23 23:22:38 +0200916 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800917 if (!buf->size || test_bit(FW_STATUS_ABORT, &buf->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 retval = -ENOENT;
Ming Lei65710cb2012-08-04 12:01:16 +0800919
Ming Leif531f05a2012-08-04 12:01:25 +0800920 /*
921 * add firmware name into devres list so that we can auto cache
922 * and uncache firmware for device.
923 *
924 * f_dev->parent may has been deleted already, but the problem
925 * should be fixed in devres or driver core.
926 */
927 if (!retval && f_dev->parent)
928 fw_add_devm_name(f_dev->parent, buf->fw_id);
929
Ming Leiac39b3e2012-08-20 19:04:16 +0800930 /*
931 * After caching firmware image is started, let it piggyback
932 * on request firmware.
933 */
934 if (!retval && fwc->state == FW_LOADER_START_CACHE) {
935 if (fw_cache_piggyback_on_request(buf->fw_id))
936 kref_get(&buf->ref);
937 }
938
Ming Lei1f2b7952012-08-04 12:01:21 +0800939 /* pass the pages buffer to driver at the last minute */
940 fw_set_page_data(buf, fw_priv->fw);
941
Ming Lei12446912012-08-04 12:01:20 +0800942 fw_priv->buf = NULL;
Laura Garciacad1e552006-05-23 23:22:38 +0200943 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Ming Lei746058f2012-10-09 12:01:03 +0800945 if (direct_load)
946 goto err_put_dev;
947
Stephen Boyddddb5542012-03-28 23:30:43 +0200948 device_remove_file(f_dev, &dev_attr_loading);
949err_del_bin_attr:
950 device_remove_bin_file(f_dev, &firmware_attr_data);
951err_del_dev:
952 device_del(f_dev);
953err_put_dev:
954 put_device(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return retval;
956}
957
958/**
Kay Sievers312c0042005-11-16 09:00:00 +0100959 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800960 * @firmware_p: pointer to firmware image
961 * @name: name of firmware file
962 * @device: device for which firmware is being loaded
963 *
964 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700965 * of @name for device @device.
966 *
967 * Should be called from user context where sleeping is allowed.
968 *
Kay Sievers312c0042005-11-16 09:00:00 +0100969 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700970 * should be distinctive enough not to be confused with any other
971 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +0800972 *
973 * Caller must hold the reference count of @device.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700974 **/
975int
976request_firmware(const struct firmware **firmware_p, const char *name,
977 struct device *device)
978{
Stephen Boyddddb5542012-03-28 23:30:43 +0200979 struct firmware_priv *fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200980 int ret;
981
Stephen Boyddddb5542012-03-28 23:30:43 +0200982 fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
983 false);
984 if (IS_ERR_OR_NULL(fw_priv))
985 return PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200986
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200987 ret = usermodehelper_read_trylock();
988 if (WARN_ON(ret)) {
989 dev_err(device, "firmware: %s will not be loaded\n", name);
990 } else {
Stephen Boyddddb5542012-03-28 23:30:43 +0200991 ret = _request_firmware_load(fw_priv, true,
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200992 firmware_loading_timeout());
993 usermodehelper_read_unlock();
994 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200995 if (ret)
996 _request_firmware_cleanup(firmware_p);
997
998 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700999}
1000
1001/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001003 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001005void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
1007 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001008 if (!fw_is_builtin_firmware(fw))
1009 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 kfree(fw);
1011 }
1012}
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014/* Async support */
1015struct firmware_work {
1016 struct work_struct work;
1017 struct module *module;
1018 const char *name;
1019 struct device *device;
1020 void *context;
1021 void (*cont)(const struct firmware *fw, void *context);
Bob Liu072fc8f2011-01-26 18:33:32 +08001022 bool uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023};
1024
Stephen Boyda36cf842012-03-28 23:31:00 +02001025static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Stephen Boyda36cf842012-03-28 23:31:00 +02001027 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 const struct firmware *fw;
Stephen Boyddddb5542012-03-28 23:30:43 +02001029 struct firmware_priv *fw_priv;
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +02001030 long timeout;
matthieu castet113fab12005-11-13 16:07:39 -08001031 int ret;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001032
Stephen Boyda36cf842012-03-28 23:31:00 +02001033 fw_work = container_of(work, struct firmware_work, work);
Stephen Boyddddb5542012-03-28 23:30:43 +02001034 fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
1035 fw_work->uevent, true);
1036 if (IS_ERR_OR_NULL(fw_priv)) {
1037 ret = PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001038 goto out;
Stephen Boyddddb5542012-03-28 23:30:43 +02001039 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001040
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +02001041 timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
1042 if (timeout) {
Stephen Boyddddb5542012-03-28 23:30:43 +02001043 ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +02001044 usermodehelper_read_unlock();
1045 } else {
1046 dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
1047 fw_work->name);
1048 ret = -EAGAIN;
1049 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001050 if (ret)
1051 _request_firmware_cleanup(&fw);
1052
1053 out:
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001054 fw_work->cont(fw, fw_work->context);
Ming Lei0cfc1e12012-08-04 12:01:23 +08001055 put_device(fw_work->device);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 module_put(fw_work->module);
1058 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
1061/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001062 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001063 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001064 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001065 * is non-zero else the firmware copy must be done manually.
1066 * @name: name of firmware file
1067 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001068 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001069 * @context: will be passed over to @cont, and
1070 * @fw may be %NULL if firmware request fails.
1071 * @cont: function will be called asynchronously when the firmware
1072 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001074 * Caller must hold the reference count of @device.
1075 *
Ming Lei6f21a622012-08-04 12:01:24 +08001076 * Asynchronous variant of request_firmware() for user contexts:
1077 * - sleep for as small periods as possible since it may
1078 * increase kernel boot time of built-in device drivers
1079 * requesting firmware in their ->probe() methods, if
1080 * @gfp is GFP_KERNEL.
1081 *
1082 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 **/
1084int
1085request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001086 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001087 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 void (*cont)(const struct firmware *fw, void *context))
1089{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001090 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001092 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (!fw_work)
1094 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001095
1096 fw_work->module = module;
1097 fw_work->name = name;
1098 fw_work->device = device;
1099 fw_work->context = context;
1100 fw_work->cont = cont;
1101 fw_work->uevent = uevent;
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (!try_module_get(module)) {
1104 kfree(fw_work);
1105 return -EFAULT;
1106 }
1107
Ming Lei0cfc1e12012-08-04 12:01:23 +08001108 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001109 INIT_WORK(&fw_work->work, request_firmware_work_func);
1110 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return 0;
1112}
1113
Ming Lei2887b392012-08-04 12:01:22 +08001114/**
1115 * cache_firmware - cache one firmware image in kernel memory space
1116 * @fw_name: the firmware image name
1117 *
1118 * Cache firmware in kernel memory so that drivers can use it when
1119 * system isn't ready for them to request firmware image from userspace.
1120 * Once it returns successfully, driver can use request_firmware or its
1121 * nowait version to get the cached firmware without any interacting
1122 * with userspace
1123 *
1124 * Return 0 if the firmware image has been cached successfully
1125 * Return !0 otherwise
1126 *
1127 */
1128int cache_firmware(const char *fw_name)
1129{
1130 int ret;
1131 const struct firmware *fw;
1132
1133 pr_debug("%s: %s\n", __func__, fw_name);
1134
1135 ret = request_firmware(&fw, fw_name, NULL);
1136 if (!ret)
1137 kfree(fw);
1138
1139 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1140
1141 return ret;
1142}
1143
1144/**
1145 * uncache_firmware - remove one cached firmware image
1146 * @fw_name: the firmware image name
1147 *
1148 * Uncache one firmware image which has been cached successfully
1149 * before.
1150 *
1151 * Return 0 if the firmware cache has been removed successfully
1152 * Return !0 otherwise
1153 *
1154 */
1155int uncache_firmware(const char *fw_name)
1156{
1157 struct firmware_buf *buf;
1158 struct firmware fw;
1159
1160 pr_debug("%s: %s\n", __func__, fw_name);
1161
1162 if (fw_get_builtin_firmware(&fw, fw_name))
1163 return 0;
1164
1165 buf = fw_lookup_buf(fw_name);
1166 if (buf) {
1167 fw_free_buf(buf);
1168 return 0;
1169 }
1170
1171 return -EINVAL;
1172}
1173
Ming Leicfe016b2012-09-08 17:32:30 +08001174#ifdef CONFIG_PM_SLEEP
Ming Leid28d3882012-10-09 12:01:04 +08001175static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1176
Ming Lei37276a52012-08-04 12:01:27 +08001177static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1178{
1179 struct fw_cache_entry *fce;
1180
1181 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1182 if (!fce)
1183 goto exit;
1184
1185 strcpy(fce->name, name);
1186exit:
1187 return fce;
1188}
1189
Ming Lei373304f2012-10-09 12:01:01 +08001190static int __fw_entry_found(const char *name)
1191{
1192 struct firmware_cache *fwc = &fw_cache;
1193 struct fw_cache_entry *fce;
1194
1195 list_for_each_entry(fce, &fwc->fw_names, list) {
1196 if (!strcmp(fce->name, name))
1197 return 1;
1198 }
1199 return 0;
1200}
1201
Ming Leiac39b3e2012-08-20 19:04:16 +08001202static int fw_cache_piggyback_on_request(const char *name)
1203{
1204 struct firmware_cache *fwc = &fw_cache;
1205 struct fw_cache_entry *fce;
1206 int ret = 0;
1207
1208 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001209 if (__fw_entry_found(name))
1210 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001211
1212 fce = alloc_fw_cache_entry(name);
1213 if (fce) {
1214 ret = 1;
1215 list_add(&fce->list, &fwc->fw_names);
1216 pr_debug("%s: fw: %s\n", __func__, name);
1217 }
1218found:
1219 spin_unlock(&fwc->name_lock);
1220 return ret;
1221}
1222
Ming Lei37276a52012-08-04 12:01:27 +08001223static void free_fw_cache_entry(struct fw_cache_entry *fce)
1224{
1225 kfree(fce);
1226}
1227
1228static void __async_dev_cache_fw_image(void *fw_entry,
1229 async_cookie_t cookie)
1230{
1231 struct fw_cache_entry *fce = fw_entry;
1232 struct firmware_cache *fwc = &fw_cache;
1233 int ret;
1234
1235 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001236 if (ret) {
1237 spin_lock(&fwc->name_lock);
1238 list_del(&fce->list);
1239 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001240
Ming Leiac39b3e2012-08-20 19:04:16 +08001241 free_fw_cache_entry(fce);
1242 }
Ming Lei37276a52012-08-04 12:01:27 +08001243}
1244
1245/* called with dev->devres_lock held */
1246static void dev_create_fw_entry(struct device *dev, void *res,
1247 void *data)
1248{
1249 struct fw_name_devm *fwn = res;
1250 const char *fw_name = fwn->name;
1251 struct list_head *head = data;
1252 struct fw_cache_entry *fce;
1253
1254 fce = alloc_fw_cache_entry(fw_name);
1255 if (fce)
1256 list_add(&fce->list, head);
1257}
1258
1259static int devm_name_match(struct device *dev, void *res,
1260 void *match_data)
1261{
1262 struct fw_name_devm *fwn = res;
1263 return (fwn->magic == (unsigned long)match_data);
1264}
1265
Ming Leiab6dd8e2012-08-17 22:07:00 +08001266static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001267{
1268 LIST_HEAD(todo);
1269 struct fw_cache_entry *fce;
1270 struct fw_cache_entry *fce_next;
1271 struct firmware_cache *fwc = &fw_cache;
1272
1273 devres_for_each_res(dev, fw_name_devm_release,
1274 devm_name_match, &fw_cache,
1275 dev_create_fw_entry, &todo);
1276
1277 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1278 list_del(&fce->list);
1279
1280 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001281 /* only one cache entry for one firmware */
1282 if (!__fw_entry_found(fce->name)) {
Ming Lei373304f2012-10-09 12:01:01 +08001283 list_add(&fce->list, &fwc->fw_names);
1284 } else {
1285 free_fw_cache_entry(fce);
1286 fce = NULL;
1287 }
Ming Lei37276a52012-08-04 12:01:27 +08001288 spin_unlock(&fwc->name_lock);
1289
Ming Lei373304f2012-10-09 12:01:01 +08001290 if (fce)
Ming Leid28d3882012-10-09 12:01:04 +08001291 async_schedule_domain(__async_dev_cache_fw_image,
1292 (void *)fce,
1293 &fw_cache_domain);
Ming Lei37276a52012-08-04 12:01:27 +08001294 }
1295}
1296
1297static void __device_uncache_fw_images(void)
1298{
1299 struct firmware_cache *fwc = &fw_cache;
1300 struct fw_cache_entry *fce;
1301
1302 spin_lock(&fwc->name_lock);
1303 while (!list_empty(&fwc->fw_names)) {
1304 fce = list_entry(fwc->fw_names.next,
1305 struct fw_cache_entry, list);
1306 list_del(&fce->list);
1307 spin_unlock(&fwc->name_lock);
1308
1309 uncache_firmware(fce->name);
1310 free_fw_cache_entry(fce);
1311
1312 spin_lock(&fwc->name_lock);
1313 }
1314 spin_unlock(&fwc->name_lock);
1315}
1316
1317/**
1318 * device_cache_fw_images - cache devices' firmware
1319 *
1320 * If one device called request_firmware or its nowait version
1321 * successfully before, the firmware names are recored into the
1322 * device's devres link list, so device_cache_fw_images can call
1323 * cache_firmware() to cache these firmwares for the device,
1324 * then the device driver can load its firmwares easily at
1325 * time when system is not ready to complete loading firmware.
1326 */
1327static void device_cache_fw_images(void)
1328{
1329 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001330 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001331 DEFINE_WAIT(wait);
1332
1333 pr_debug("%s\n", __func__);
1334
Ming Lei373304f2012-10-09 12:01:01 +08001335 /* cancel uncache work */
1336 cancel_delayed_work_sync(&fwc->work);
1337
Ming Leiffe53f62012-08-04 12:01:28 +08001338 /*
1339 * use small loading timeout for caching devices' firmware
1340 * because all these firmware images have been loaded
1341 * successfully at lease once, also system is ready for
1342 * completing firmware loading now. The maximum size of
1343 * firmware in current distributions is about 2M bytes,
1344 * so 10 secs should be enough.
1345 */
1346 old_timeout = loading_timeout;
1347 loading_timeout = 10;
1348
Ming Leiac39b3e2012-08-20 19:04:16 +08001349 mutex_lock(&fw_lock);
1350 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001351 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001352 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001353
1354 /* wait for completion of caching firmware for all devices */
Ming Leid28d3882012-10-09 12:01:04 +08001355 async_synchronize_full_domain(&fw_cache_domain);
Ming Leiffe53f62012-08-04 12:01:28 +08001356
1357 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001358}
1359
1360/**
1361 * device_uncache_fw_images - uncache devices' firmware
1362 *
1363 * uncache all firmwares which have been cached successfully
1364 * by device_uncache_fw_images earlier
1365 */
1366static void device_uncache_fw_images(void)
1367{
1368 pr_debug("%s\n", __func__);
1369 __device_uncache_fw_images();
1370}
1371
1372static void device_uncache_fw_images_work(struct work_struct *work)
1373{
1374 device_uncache_fw_images();
1375}
1376
1377/**
1378 * device_uncache_fw_images_delay - uncache devices firmwares
1379 * @delay: number of milliseconds to delay uncache device firmwares
1380 *
1381 * uncache all devices's firmwares which has been cached successfully
1382 * by device_cache_fw_images after @delay milliseconds.
1383 */
1384static void device_uncache_fw_images_delay(unsigned long delay)
1385{
1386 schedule_delayed_work(&fw_cache.work,
1387 msecs_to_jiffies(delay));
1388}
1389
Ming Lei07646d92012-08-04 12:01:29 +08001390static int fw_pm_notify(struct notifier_block *notify_block,
1391 unsigned long mode, void *unused)
1392{
1393 switch (mode) {
1394 case PM_HIBERNATION_PREPARE:
1395 case PM_SUSPEND_PREPARE:
1396 device_cache_fw_images();
1397 break;
1398
1399 case PM_POST_SUSPEND:
1400 case PM_POST_HIBERNATION:
1401 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001402 /*
1403 * In case that system sleep failed and syscore_suspend is
1404 * not called.
1405 */
1406 mutex_lock(&fw_lock);
1407 fw_cache.state = FW_LOADER_NO_CACHE;
1408 mutex_unlock(&fw_lock);
1409
Ming Lei07646d92012-08-04 12:01:29 +08001410 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1411 break;
1412 }
1413
1414 return 0;
1415}
Ming Lei07646d92012-08-04 12:01:29 +08001416
Ming Leiac39b3e2012-08-20 19:04:16 +08001417/* stop caching firmware once syscore_suspend is reached */
1418static int fw_suspend(void)
1419{
1420 fw_cache.state = FW_LOADER_NO_CACHE;
1421 return 0;
1422}
1423
1424static struct syscore_ops fw_syscore_ops = {
1425 .suspend = fw_suspend,
1426};
Ming Leicfe016b2012-09-08 17:32:30 +08001427#else
1428static int fw_cache_piggyback_on_request(const char *name)
1429{
1430 return 0;
1431}
1432#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001433
Ming Lei37276a52012-08-04 12:01:27 +08001434static void __init fw_cache_init(void)
1435{
1436 spin_lock_init(&fw_cache.lock);
1437 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001438 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001439
Ming Leicfe016b2012-09-08 17:32:30 +08001440#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +08001441 spin_lock_init(&fw_cache.name_lock);
1442 INIT_LIST_HEAD(&fw_cache.fw_names);
Ming Lei37276a52012-08-04 12:01:27 +08001443
Ming Lei37276a52012-08-04 12:01:27 +08001444 INIT_DELAYED_WORK(&fw_cache.work,
1445 device_uncache_fw_images_work);
Ming Lei07646d92012-08-04 12:01:29 +08001446
1447 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1448 register_pm_notifier(&fw_cache.pm_notify);
Ming Leiac39b3e2012-08-20 19:04:16 +08001449
1450 register_syscore_ops(&fw_syscore_ops);
Ming Leicfe016b2012-09-08 17:32:30 +08001451#endif
Ming Lei37276a52012-08-04 12:01:27 +08001452}
1453
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001454static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
Ming Lei1f2b7952012-08-04 12:01:21 +08001456 fw_cache_init();
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001457 return class_register(&firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001459
1460static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
Ming Leicfe016b2012-09-08 17:32:30 +08001462#ifdef CONFIG_PM_SLEEP
Ming Leiac39b3e2012-08-20 19:04:16 +08001463 unregister_syscore_ops(&fw_syscore_ops);
Ming Lei07646d92012-08-04 12:01:29 +08001464 unregister_pm_notifier(&fw_cache.pm_notify);
Ming Leicfe016b2012-09-08 17:32:30 +08001465#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 class_unregister(&firmware_class);
1467}
1468
Shaohua Lia30a6a22006-09-27 01:50:52 -07001469fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470module_exit(firmware_class_exit);
1471
1472EXPORT_SYMBOL(release_firmware);
1473EXPORT_SYMBOL(request_firmware);
1474EXPORT_SYMBOL(request_firmware_nowait);
Ming Lei2887b392012-08-04 12:01:22 +08001475EXPORT_SYMBOL_GPL(cache_firmware);
1476EXPORT_SYMBOL_GPL(uncache_firmware);