blob: 40ec60affd8f19c23f1f0e35208ddf5b2dd2d9dd [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
Ming Lei1f2b7952012-08-04 12:01:21 +0800249 list_del(&buf->list);
250 spin_unlock(&fwc->lock);
251
Ming Lei746058f2012-10-09 12:01:03 +0800252
253 if (buf->fmt == PAGE_BUF) {
254 vunmap(buf->data);
255 for (i = 0; i < buf->nr_pages; i++)
256 __free_page(buf->pages[i]);
257 kfree(buf->pages);
258 } else
259 vfree(buf->data);
Ming Lei1f2b7952012-08-04 12:01:21 +0800260 kfree(buf);
261}
262
263static void fw_free_buf(struct firmware_buf *buf)
264{
Chuansheng Liubd9eb7f2012-11-10 01:27:22 +0800265 struct firmware_cache *fwc = buf->fwc;
266 spin_lock(&fwc->lock);
267 if (!kref_put(&buf->ref, __fw_free_buf))
268 spin_unlock(&fwc->lock);
Ming Lei1f2b7952012-08-04 12:01:21 +0800269}
270
Ming Lei746058f2012-10-09 12:01:03 +0800271/* direct firmware loading support */
Ming Lei27602842012-11-03 17:47:58 +0800272static char fw_path_para[256];
273static const char * const fw_path[] = {
274 fw_path_para,
Ming Lei746058f2012-10-09 12:01:03 +0800275 "/lib/firmware/updates/" UTS_RELEASE,
276 "/lib/firmware/updates",
277 "/lib/firmware/" UTS_RELEASE,
278 "/lib/firmware"
279};
280
Ming Lei27602842012-11-03 17:47:58 +0800281/*
282 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
283 * from kernel command line because firmware_class is generally built in
284 * kernel instead of module.
285 */
286module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
287MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
288
Ming Lei746058f2012-10-09 12:01:03 +0800289/* Don't inline this: 'struct kstat' is biggish */
Cesar Eduardo Barros60dac5e2012-10-27 20:37:10 -0200290static noinline_for_stack long fw_file_size(struct file *file)
Ming Lei746058f2012-10-09 12:01:03 +0800291{
292 struct kstat st;
293 if (vfs_getattr(file->f_path.mnt, file->f_path.dentry, &st))
294 return -1;
295 if (!S_ISREG(st.mode))
296 return -1;
297 if (st.size != (long)st.size)
298 return -1;
299 return st.size;
300}
301
302static bool fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf)
303{
304 long size;
305 char *buf;
306
307 size = fw_file_size(file);
Luciano Coelho4adf07f2013-01-15 10:43:43 +0200308 if (size <= 0)
Ming Lei746058f2012-10-09 12:01:03 +0800309 return false;
310 buf = vmalloc(size);
311 if (!buf)
312 return false;
313 if (kernel_read(file, 0, buf, size) != size) {
314 vfree(buf);
315 return false;
316 }
317 fw_buf->data = buf;
318 fw_buf->size = size;
319 return true;
320}
321
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100322static bool fw_get_filesystem_firmware(struct device *device,
323 struct firmware_buf *buf)
Ming Lei746058f2012-10-09 12:01:03 +0800324{
325 int i;
326 bool success = false;
327 char *path = __getname();
328
329 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
330 struct file *file;
Ming Lei27602842012-11-03 17:47:58 +0800331
332 /* skip the unset customized path */
333 if (!fw_path[i][0])
334 continue;
335
Ming Lei746058f2012-10-09 12:01:03 +0800336 snprintf(path, PATH_MAX, "%s/%s", fw_path[i], buf->fw_id);
337
338 file = filp_open(path, O_RDONLY, 0);
339 if (IS_ERR(file))
340 continue;
341 success = fw_read_file_contents(file, buf);
342 fput(file);
343 if (success)
344 break;
345 }
346 __putname(path);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100347
348 if (success) {
349 dev_dbg(device, "firmware: direct-loading firmware %s\n",
350 buf->fw_id);
351 mutex_lock(&fw_lock);
352 set_bit(FW_STATUS_DONE, &buf->status);
353 complete_all(&buf->completion);
354 mutex_unlock(&fw_lock);
355 }
356
Ming Lei746058f2012-10-09 12:01:03 +0800357 return success;
358}
359
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700360static struct firmware_priv *to_firmware_priv(struct device *dev)
361{
362 return container_of(dev, struct firmware_priv, dev);
363}
364
365static void fw_load_abort(struct firmware_priv *fw_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Ming Lei12446912012-08-04 12:01:20 +0800367 struct firmware_buf *buf = fw_priv->buf;
368
369 set_bit(FW_STATUS_ABORT, &buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800370 complete_all(&buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700373static ssize_t firmware_timeout_show(struct class *class,
374 struct class_attribute *attr,
375 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 return sprintf(buf, "%d\n", loading_timeout);
378}
379
380/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800381 * firmware_timeout_store - set number of seconds to wait for firmware
382 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800383 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800384 * @buf: buffer to scan for timeout value
385 * @count: number of bytes in @buf
386 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800388 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 * firmware will be provided.
390 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800391 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700393static ssize_t firmware_timeout_store(struct class *class,
394 struct class_attribute *attr,
395 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700398 if (loading_timeout < 0)
399 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return count;
402}
403
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800404static struct class_attribute firmware_class_attrs[] = {
405 __ATTR(timeout, S_IWUSR | S_IRUGO,
406 firmware_timeout_show, firmware_timeout_store),
407 __ATTR_NULL
408};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800410static void fw_dev_release(struct device *dev)
411{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700412 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800413
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800414 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800415
416 module_put(THIS_MODULE);
417}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Kay Sievers7eff2e72007-08-14 15:15:12 +0200419static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700421 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Ming Lei12446912012-08-04 12:01:20 +0800423 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200425 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700426 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200427 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
428 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 return 0;
431}
432
Adrian Bunk1b81d662006-05-20 15:00:16 -0700433static struct class firmware_class = {
434 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800435 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700436 .dev_uevent = firmware_uevent,
437 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700438};
439
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700440static ssize_t firmware_loading_show(struct device *dev,
441 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700443 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800444 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return sprintf(buf, "%d\n", loading);
447}
448
Ming Lei65710cb2012-08-04 12:01:16 +0800449/* firmware holds the ownership of pages */
David Woodhousedd336c52010-05-02 11:21:21 +0300450static void firmware_free_data(const struct firmware *fw)
451{
Linus Torvaldsabb139e2012-10-03 15:58:32 -0700452 /* Loaded directly? */
453 if (!fw->priv) {
454 vfree(fw->data);
455 return;
456 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800457 fw_free_buf(fw->priv);
David Woodhousedd336c52010-05-02 11:21:21 +0300458}
459
David Woodhouse6e03a202009-04-09 22:04:07 -0700460/* Some architectures don't have PAGE_KERNEL_RO */
461#ifndef PAGE_KERNEL_RO
462#define PAGE_KERNEL_RO PAGE_KERNEL
463#endif
Ming Lei253c9242012-10-09 12:01:02 +0800464
465/* one pages buffer should be mapped/unmapped only once */
466static int fw_map_pages_buf(struct firmware_buf *buf)
467{
Ming Lei746058f2012-10-09 12:01:03 +0800468 if (buf->fmt != PAGE_BUF)
469 return 0;
470
Ming Lei253c9242012-10-09 12:01:02 +0800471 if (buf->data)
472 vunmap(buf->data);
473 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
474 if (!buf->data)
475 return -ENOMEM;
476 return 0;
477}
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800480 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700481 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800482 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800483 * @buf: buffer to scan for loading control value
484 * @count: number of bytes in @buf
485 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 * The relevant values are:
487 *
488 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800489 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 * -1: Conclude the load with an error and discard any written data.
491 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700492static ssize_t firmware_loading_store(struct device *dev,
493 struct device_attribute *attr,
494 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700496 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800497 struct firmware_buf *fw_buf = fw_priv->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700499 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Neil Hormaneea915b2012-01-02 15:31:23 -0500501 mutex_lock(&fw_lock);
502
Ming Lei12446912012-08-04 12:01:20 +0800503 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500504 goto out;
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 switch (loading) {
507 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800508 /* discarding any previous partial load */
Ming Lei12446912012-08-04 12:01:20 +0800509 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
510 for (i = 0; i < fw_buf->nr_pages; i++)
511 __free_page(fw_buf->pages[i]);
512 kfree(fw_buf->pages);
513 fw_buf->pages = NULL;
514 fw_buf->page_array_size = 0;
515 fw_buf->nr_pages = 0;
516 set_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei28eefa72012-08-04 12:01:17 +0800517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 break;
519 case 0:
Ming Lei12446912012-08-04 12:01:20 +0800520 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
521 set_bit(FW_STATUS_DONE, &fw_buf->status);
522 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei253c9242012-10-09 12:01:02 +0800523
524 /*
525 * Several loading requests may be pending on
526 * one same firmware buf, so let all requests
527 * see the mapped 'buf->data' once the loading
528 * is completed.
529 * */
530 fw_map_pages_buf(fw_buf);
Ming Lei1f2b7952012-08-04 12:01:21 +0800531 complete_all(&fw_buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 break;
533 }
534 /* fallthrough */
535 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700536 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /* fallthrough */
538 case -1:
539 fw_load_abort(fw_priv);
540 break;
541 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500542out:
543 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return count;
545}
546
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700547static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700549static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
550 struct bin_attribute *bin_attr,
551 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200553 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700554 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800555 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700556 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Laura Garciacad1e552006-05-23 23:22:38 +0200558 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800559 buf = fw_priv->buf;
560 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 ret_count = -ENODEV;
562 goto out;
563 }
Ming Lei12446912012-08-04 12:01:20 +0800564 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200565 ret_count = 0;
566 goto out;
567 }
Ming Lei12446912012-08-04 12:01:20 +0800568 if (count > buf->size - offset)
569 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700570
571 ret_count = count;
572
573 while (count) {
574 void *page_data;
575 int page_nr = offset >> PAGE_SHIFT;
576 int page_ofs = offset & (PAGE_SIZE-1);
577 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
578
Ming Lei12446912012-08-04 12:01:20 +0800579 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700580
581 memcpy(buffer, page_data + page_ofs, page_cnt);
582
Ming Lei12446912012-08-04 12:01:20 +0800583 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700584 buffer += page_cnt;
585 offset += page_cnt;
586 count -= page_cnt;
587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588out:
Laura Garciacad1e552006-05-23 23:22:38 +0200589 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return ret_count;
591}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800592
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700593static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Ming Lei12446912012-08-04 12:01:20 +0800595 struct firmware_buf *buf = fw_priv->buf;
David Woodhouse6e03a202009-04-09 22:04:07 -0700596 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
David Woodhouse6e03a202009-04-09 22:04:07 -0700598 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800599 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700600 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800601 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700602 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
David Woodhouse6e03a202009-04-09 22:04:07 -0700604 new_pages = kmalloc(new_array_size * sizeof(void *),
605 GFP_KERNEL);
606 if (!new_pages) {
607 fw_load_abort(fw_priv);
608 return -ENOMEM;
609 }
Ming Lei12446912012-08-04 12:01:20 +0800610 memcpy(new_pages, buf->pages,
611 buf->page_array_size * sizeof(void *));
612 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
613 (new_array_size - buf->page_array_size));
614 kfree(buf->pages);
615 buf->pages = new_pages;
616 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700618
Ming Lei12446912012-08-04 12:01:20 +0800619 while (buf->nr_pages < pages_needed) {
620 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700621 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
622
Ming Lei12446912012-08-04 12:01:20 +0800623 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700624 fw_load_abort(fw_priv);
625 return -ENOMEM;
626 }
Ming Lei12446912012-08-04 12:01:20 +0800627 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return 0;
630}
631
632/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800633 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700634 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700635 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700636 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800637 * @buffer: buffer being written
638 * @offset: buffer offset for write in total data store area
639 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800641 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 * the driver as a firmware image.
643 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700644static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
645 struct bin_attribute *bin_attr,
646 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200648 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700649 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800650 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 ssize_t retval;
652
653 if (!capable(CAP_SYS_RAWIO))
654 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700655
Laura Garciacad1e552006-05-23 23:22:38 +0200656 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800657 buf = fw_priv->buf;
658 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 retval = -ENODEV;
660 goto out;
661 }
Ming Lei65710cb2012-08-04 12:01:16 +0800662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 retval = fw_realloc_buffer(fw_priv, offset + count);
664 if (retval)
665 goto out;
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700668
669 while (count) {
670 void *page_data;
671 int page_nr = offset >> PAGE_SHIFT;
672 int page_ofs = offset & (PAGE_SIZE - 1);
673 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
674
Ming Lei12446912012-08-04 12:01:20 +0800675 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700676
677 memcpy(page_data + page_ofs, buffer, page_cnt);
678
Ming Lei12446912012-08-04 12:01:20 +0800679 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700680 buffer += page_cnt;
681 offset += page_cnt;
682 count -= page_cnt;
683 }
684
Ming Lei12446912012-08-04 12:01:20 +0800685 buf->size = max_t(size_t, offset, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686out:
Laura Garciacad1e552006-05-23 23:22:38 +0200687 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return retval;
689}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800690
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700691static struct bin_attribute firmware_attr_data = {
692 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 .size = 0,
694 .read = firmware_data_read,
695 .write = firmware_data_write,
696};
697
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800698static void firmware_class_timeout_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800700 struct firmware_priv *fw_priv = container_of(work,
701 struct firmware_priv, timeout_work.work);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700702
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800703 mutex_lock(&fw_lock);
704 if (test_bit(FW_STATUS_DONE, &(fw_priv->buf->status))) {
705 mutex_unlock(&fw_lock);
706 return;
707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 fw_load_abort(fw_priv);
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800709 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700712static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200713fw_create_instance(struct firmware *firmware, const char *fw_name,
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700714 struct device *device, bool uevent, bool nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700716 struct firmware_priv *fw_priv;
717 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Ming Lei12446912012-08-04 12:01:20 +0800719 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700720 if (!fw_priv) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700721 dev_err(device, "%s: kmalloc failed\n", __func__);
Ming Lei12446912012-08-04 12:01:20 +0800722 fw_priv = ERR_PTR(-ENOMEM);
723 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700726 fw_priv->nowait = nowait;
Ming Lei1f2b7952012-08-04 12:01:21 +0800727 fw_priv->fw = firmware;
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800728 INIT_DELAYED_WORK(&fw_priv->timeout_work,
729 firmware_class_timeout_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700731 f_dev = &fw_priv->dev;
732
733 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800734 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700735 f_dev->parent = device;
736 f_dev->class = &firmware_class;
Ming Lei12446912012-08-04 12:01:20 +0800737exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700738 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
Ming Lei1f2b7952012-08-04 12:01:21 +0800741/* store the pages buffer info firmware from buf */
742static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
743{
744 fw->priv = buf;
745 fw->pages = buf->pages;
746 fw->size = buf->size;
747 fw->data = buf->data;
748
749 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
750 __func__, buf->fw_id, buf, buf->data,
751 (unsigned int)buf->size);
752}
753
Ming Leicfe016b2012-09-08 17:32:30 +0800754#ifdef CONFIG_PM_SLEEP
Ming Leif531f05a2012-08-04 12:01:25 +0800755static void fw_name_devm_release(struct device *dev, void *res)
756{
757 struct fw_name_devm *fwn = res;
758
759 if (fwn->magic == (unsigned long)&fw_cache)
760 pr_debug("%s: fw_name-%s devm-%p released\n",
761 __func__, fwn->name, res);
762}
763
764static int fw_devm_match(struct device *dev, void *res,
765 void *match_data)
766{
767 struct fw_name_devm *fwn = res;
768
769 return (fwn->magic == (unsigned long)&fw_cache) &&
770 !strcmp(fwn->name, match_data);
771}
772
773static struct fw_name_devm *fw_find_devm_name(struct device *dev,
774 const char *name)
775{
776 struct fw_name_devm *fwn;
777
778 fwn = devres_find(dev, fw_name_devm_release,
779 fw_devm_match, (void *)name);
780 return fwn;
781}
782
783/* add firmware name into devres list */
784static int fw_add_devm_name(struct device *dev, const char *name)
785{
786 struct fw_name_devm *fwn;
787
788 fwn = fw_find_devm_name(dev, name);
789 if (fwn)
790 return 1;
791
792 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
793 strlen(name) + 1, GFP_KERNEL);
794 if (!fwn)
795 return -ENOMEM;
796
797 fwn->magic = (unsigned long)&fw_cache;
798 strcpy(fwn->name, name);
799 devres_add(dev, fwn);
800
801 return 0;
802}
Ming Leicfe016b2012-09-08 17:32:30 +0800803#else
804static int fw_add_devm_name(struct device *dev, const char *name)
805{
806 return 0;
807}
808#endif
Ming Leif531f05a2012-08-04 12:01:25 +0800809
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100810/* wait until the shared firmware_buf becomes ready (or error) */
811static int sync_cached_firmware_buf(struct firmware_buf *buf)
Ming Lei1f2b7952012-08-04 12:01:21 +0800812{
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100813 int ret = 0;
814
815 mutex_lock(&fw_lock);
816 while (!test_bit(FW_STATUS_DONE, &buf->status)) {
817 if (test_bit(FW_STATUS_ABORT, &buf->status)) {
818 ret = -ENOENT;
819 break;
820 }
821 mutex_unlock(&fw_lock);
822 wait_for_completion(&buf->completion);
823 mutex_lock(&fw_lock);
824 }
825 mutex_unlock(&fw_lock);
826 return ret;
Ming Lei1f2b7952012-08-04 12:01:21 +0800827}
828
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100829/* prepare firmware and firmware_buf structs;
830 * return 0 if a firmware is already assigned, 1 if need to load one,
831 * or a negative error code
832 */
833static int
834_request_firmware_prepare(struct firmware **firmware_p, const char *name,
835 struct device *device)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700836{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +0800838 struct firmware_buf *buf;
839 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Jiri Slaby4aed0642005-09-13 01:25:01 -0700841 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700843 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
844 __func__);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100845 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800848 if (fw_get_builtin_firmware(firmware, name)) {
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100849 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100850 return 0; /* assigned */
David Woodhouse5658c762008-05-23 13:52:42 +0100851 }
852
Ming Lei1f2b7952012-08-04 12:01:21 +0800853 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
Ming Lei1f2b7952012-08-04 12:01:21 +0800854
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100855 /*
856 * bind with 'buf' now to avoid warning in failure path
857 * of requesting firmware.
858 */
859 firmware->priv = buf;
Ming Lei1f2b7952012-08-04 12:01:21 +0800860
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100861 if (ret > 0) {
862 ret = sync_cached_firmware_buf(buf);
863 if (!ret) {
864 fw_set_page_data(buf, firmware);
865 return 0; /* assigned */
866 }
Stephen Boyddddb5542012-03-28 23:30:43 +0200867 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800868
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100869 if (ret < 0)
870 return ret;
871 return 1; /* need to load */
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200872}
873
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100874static int assign_firmware_buf(struct firmware *fw, struct device *device)
875{
876 struct firmware_buf *buf = fw->priv;
877
878 mutex_lock(&fw_lock);
879 if (!buf->size || test_bit(FW_STATUS_ABORT, &buf->status)) {
880 mutex_unlock(&fw_lock);
881 return -ENOENT;
882 }
883
884 /*
885 * add firmware name into devres list so that we can auto cache
886 * and uncache firmware for device.
887 *
888 * device may has been deleted already, but the problem
889 * should be fixed in devres or driver core.
890 */
891 if (device)
892 fw_add_devm_name(device, buf->fw_id);
893
894 /*
895 * After caching firmware image is started, let it piggyback
896 * on request firmware.
897 */
898 if (buf->fwc->state == FW_LOADER_START_CACHE) {
899 if (fw_cache_piggyback_on_request(buf->fw_id))
900 kref_get(&buf->ref);
901 }
902
903 /* pass the pages buffer to driver at the last minute */
904 fw_set_page_data(buf, fw);
905 mutex_unlock(&fw_lock);
906 return 0;
907}
908
909/* load a firmware via user helper */
Stephen Boyddddb5542012-03-28 23:30:43 +0200910static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
911 long timeout)
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200912{
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200913 int retval = 0;
Stephen Boyddddb5542012-03-28 23:30:43 +0200914 struct device *f_dev = &fw_priv->dev;
Ming Lei12446912012-08-04 12:01:20 +0800915 struct firmware_buf *buf = fw_priv->buf;
Ming Lei746058f2012-10-09 12:01:03 +0800916
917 /* fall back on userspace loading */
918 buf->fmt = PAGE_BUF;
Linus Torvaldscaca9512011-08-24 15:55:30 -0700919
Stephen Boyddddb5542012-03-28 23:30:43 +0200920 dev_set_uevent_suppress(f_dev, true);
David Woodhouse5658c762008-05-23 13:52:42 +0100921
Stephen Boyddddb5542012-03-28 23:30:43 +0200922 /* Need to pin this module until class device is destroyed */
923 __module_get(THIS_MODULE);
924
925 retval = device_add(f_dev);
926 if (retval) {
927 dev_err(f_dev, "%s: device_register failed\n", __func__);
928 goto err_put_dev;
929 }
930
931 retval = device_create_bin_file(f_dev, &firmware_attr_data);
932 if (retval) {
933 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
934 goto err_del_dev;
935 }
936
937 retval = device_create_file(f_dev, &dev_attr_loading);
938 if (retval) {
939 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
940 goto err_del_bin_attr;
941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Kay Sievers312c0042005-11-16 09:00:00 +0100943 if (uevent) {
Stephen Boyddddb5542012-03-28 23:30:43 +0200944 dev_set_uevent_suppress(f_dev, false);
Ming Lei12446912012-08-04 12:01:20 +0800945 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200946 if (timeout != MAX_SCHEDULE_TIMEOUT)
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800947 schedule_delayed_work(&fw_priv->timeout_work, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700949 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
950 }
951
Ming Lei12446912012-08-04 12:01:20 +0800952 wait_for_completion(&buf->completion);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700953
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800954 cancel_delayed_work_sync(&fw_priv->timeout_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Ming Lei12446912012-08-04 12:01:20 +0800956 fw_priv->buf = NULL;
Ming Lei746058f2012-10-09 12:01:03 +0800957
Stephen Boyddddb5542012-03-28 23:30:43 +0200958 device_remove_file(f_dev, &dev_attr_loading);
959err_del_bin_attr:
960 device_remove_bin_file(f_dev, &firmware_attr_data);
961err_del_dev:
962 device_del(f_dev);
963err_put_dev:
964 put_device(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return retval;
966}
967
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100968static int fw_load_from_user_helper(struct firmware *firmware,
969 const char *name, struct device *device,
970 bool uevent, bool nowait, long timeout)
971{
972 struct firmware_priv *fw_priv;
973
974 fw_priv = fw_create_instance(firmware, name, device, uevent, nowait);
975 if (IS_ERR(fw_priv))
976 return PTR_ERR(fw_priv);
977
978 fw_priv->buf = firmware->priv;
979 return _request_firmware_load(fw_priv, uevent, timeout);
980}
981
982/* called from request_firmware() and request_firmware_work_func() */
983static int
984_request_firmware(const struct firmware **firmware_p, const char *name,
985 struct device *device, bool uevent, bool nowait)
986{
987 struct firmware *fw;
988 long timeout;
989 int ret;
990
991 if (!firmware_p)
992 return -EINVAL;
993
994 ret = _request_firmware_prepare(&fw, name, device);
995 if (ret <= 0) /* error or already assigned */
996 goto out;
997
998 ret = 0;
999 timeout = firmware_loading_timeout();
1000 if (nowait) {
1001 timeout = usermodehelper_read_lock_wait(timeout);
1002 if (!timeout) {
1003 dev_dbg(device, "firmware: %s loading timed out\n",
1004 name);
1005 ret = -EBUSY;
1006 goto out;
1007 }
1008 } else {
1009 ret = usermodehelper_read_trylock();
1010 if (WARN_ON(ret)) {
1011 dev_err(device, "firmware: %s will not be loaded\n",
1012 name);
1013 goto out;
1014 }
1015 }
1016
1017 if (!fw_get_filesystem_firmware(device, fw->priv))
1018 ret = fw_load_from_user_helper(fw, name, device,
1019 uevent, nowait, timeout);
1020 if (!ret)
1021 ret = assign_firmware_buf(fw, device);
1022
1023 usermodehelper_read_unlock();
1024
1025 out:
1026 if (ret < 0) {
1027 release_firmware(fw);
1028 fw = NULL;
1029 }
1030
1031 *firmware_p = fw;
1032 return ret;
1033}
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035/**
Kay Sievers312c0042005-11-16 09:00:00 +01001036 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001037 * @firmware_p: pointer to firmware image
1038 * @name: name of firmware file
1039 * @device: device for which firmware is being loaded
1040 *
1041 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001042 * of @name for device @device.
1043 *
1044 * Should be called from user context where sleeping is allowed.
1045 *
Kay Sievers312c0042005-11-16 09:00:00 +01001046 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001047 * should be distinctive enough not to be confused with any other
1048 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +08001049 *
1050 * Caller must hold the reference count of @device.
Ming Lei6a927852012-11-03 17:48:16 +08001051 *
1052 * The function can be called safely inside device's suspend and
1053 * resume callback.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001054 **/
1055int
1056request_firmware(const struct firmware **firmware_p, const char *name,
1057 struct device *device)
1058{
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001059 return _request_firmware(firmware_p, name, device, true, false);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001060}
1061
1062/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001064 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001066void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
1068 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001069 if (!fw_is_builtin_firmware(fw))
1070 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 kfree(fw);
1072 }
1073}
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075/* Async support */
1076struct firmware_work {
1077 struct work_struct work;
1078 struct module *module;
1079 const char *name;
1080 struct device *device;
1081 void *context;
1082 void (*cont)(const struct firmware *fw, void *context);
Bob Liu072fc8f2011-01-26 18:33:32 +08001083 bool uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084};
1085
Stephen Boyda36cf842012-03-28 23:31:00 +02001086static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
Stephen Boyda36cf842012-03-28 23:31:00 +02001088 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 const struct firmware *fw;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001090
Stephen Boyda36cf842012-03-28 23:31:00 +02001091 fw_work = container_of(work, struct firmware_work, work);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001092
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001093 _request_firmware(&fw, fw_work->name, fw_work->device,
1094 fw_work->uevent, true);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001095 fw_work->cont(fw, fw_work->context);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001096 put_device(fw_work->device); /* taken in request_firmware_nowait() */
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 module_put(fw_work->module);
1099 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
1101
1102/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001103 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001104 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001105 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001106 * is non-zero else the firmware copy must be done manually.
1107 * @name: name of firmware file
1108 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001109 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001110 * @context: will be passed over to @cont, and
1111 * @fw may be %NULL if firmware request fails.
1112 * @cont: function will be called asynchronously when the firmware
1113 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001115 * Caller must hold the reference count of @device.
1116 *
Ming Lei6f21a622012-08-04 12:01:24 +08001117 * Asynchronous variant of request_firmware() for user contexts:
1118 * - sleep for as small periods as possible since it may
1119 * increase kernel boot time of built-in device drivers
1120 * requesting firmware in their ->probe() methods, if
1121 * @gfp is GFP_KERNEL.
1122 *
1123 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 **/
1125int
1126request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001127 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001128 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 void (*cont)(const struct firmware *fw, void *context))
1130{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001131 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001133 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (!fw_work)
1135 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001136
1137 fw_work->module = module;
1138 fw_work->name = name;
1139 fw_work->device = device;
1140 fw_work->context = context;
1141 fw_work->cont = cont;
1142 fw_work->uevent = uevent;
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (!try_module_get(module)) {
1145 kfree(fw_work);
1146 return -EFAULT;
1147 }
1148
Ming Lei0cfc1e12012-08-04 12:01:23 +08001149 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001150 INIT_WORK(&fw_work->work, request_firmware_work_func);
1151 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return 0;
1153}
1154
Ming Lei2887b392012-08-04 12:01:22 +08001155/**
1156 * cache_firmware - cache one firmware image in kernel memory space
1157 * @fw_name: the firmware image name
1158 *
1159 * Cache firmware in kernel memory so that drivers can use it when
1160 * system isn't ready for them to request firmware image from userspace.
1161 * Once it returns successfully, driver can use request_firmware or its
1162 * nowait version to get the cached firmware without any interacting
1163 * with userspace
1164 *
1165 * Return 0 if the firmware image has been cached successfully
1166 * Return !0 otherwise
1167 *
1168 */
1169int cache_firmware(const char *fw_name)
1170{
1171 int ret;
1172 const struct firmware *fw;
1173
1174 pr_debug("%s: %s\n", __func__, fw_name);
1175
1176 ret = request_firmware(&fw, fw_name, NULL);
1177 if (!ret)
1178 kfree(fw);
1179
1180 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1181
1182 return ret;
1183}
1184
1185/**
1186 * uncache_firmware - remove one cached firmware image
1187 * @fw_name: the firmware image name
1188 *
1189 * Uncache one firmware image which has been cached successfully
1190 * before.
1191 *
1192 * Return 0 if the firmware cache has been removed successfully
1193 * Return !0 otherwise
1194 *
1195 */
1196int uncache_firmware(const char *fw_name)
1197{
1198 struct firmware_buf *buf;
1199 struct firmware fw;
1200
1201 pr_debug("%s: %s\n", __func__, fw_name);
1202
1203 if (fw_get_builtin_firmware(&fw, fw_name))
1204 return 0;
1205
1206 buf = fw_lookup_buf(fw_name);
1207 if (buf) {
1208 fw_free_buf(buf);
1209 return 0;
1210 }
1211
1212 return -EINVAL;
1213}
1214
Ming Leicfe016b2012-09-08 17:32:30 +08001215#ifdef CONFIG_PM_SLEEP
Ming Leid28d3882012-10-09 12:01:04 +08001216static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1217
Ming Lei37276a52012-08-04 12:01:27 +08001218static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1219{
1220 struct fw_cache_entry *fce;
1221
1222 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1223 if (!fce)
1224 goto exit;
1225
1226 strcpy(fce->name, name);
1227exit:
1228 return fce;
1229}
1230
Ming Lei373304f2012-10-09 12:01:01 +08001231static int __fw_entry_found(const char *name)
1232{
1233 struct firmware_cache *fwc = &fw_cache;
1234 struct fw_cache_entry *fce;
1235
1236 list_for_each_entry(fce, &fwc->fw_names, list) {
1237 if (!strcmp(fce->name, name))
1238 return 1;
1239 }
1240 return 0;
1241}
1242
Ming Leiac39b3e2012-08-20 19:04:16 +08001243static int fw_cache_piggyback_on_request(const char *name)
1244{
1245 struct firmware_cache *fwc = &fw_cache;
1246 struct fw_cache_entry *fce;
1247 int ret = 0;
1248
1249 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001250 if (__fw_entry_found(name))
1251 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001252
1253 fce = alloc_fw_cache_entry(name);
1254 if (fce) {
1255 ret = 1;
1256 list_add(&fce->list, &fwc->fw_names);
1257 pr_debug("%s: fw: %s\n", __func__, name);
1258 }
1259found:
1260 spin_unlock(&fwc->name_lock);
1261 return ret;
1262}
1263
Ming Lei37276a52012-08-04 12:01:27 +08001264static void free_fw_cache_entry(struct fw_cache_entry *fce)
1265{
1266 kfree(fce);
1267}
1268
1269static void __async_dev_cache_fw_image(void *fw_entry,
1270 async_cookie_t cookie)
1271{
1272 struct fw_cache_entry *fce = fw_entry;
1273 struct firmware_cache *fwc = &fw_cache;
1274 int ret;
1275
1276 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001277 if (ret) {
1278 spin_lock(&fwc->name_lock);
1279 list_del(&fce->list);
1280 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001281
Ming Leiac39b3e2012-08-20 19:04:16 +08001282 free_fw_cache_entry(fce);
1283 }
Ming Lei37276a52012-08-04 12:01:27 +08001284}
1285
1286/* called with dev->devres_lock held */
1287static void dev_create_fw_entry(struct device *dev, void *res,
1288 void *data)
1289{
1290 struct fw_name_devm *fwn = res;
1291 const char *fw_name = fwn->name;
1292 struct list_head *head = data;
1293 struct fw_cache_entry *fce;
1294
1295 fce = alloc_fw_cache_entry(fw_name);
1296 if (fce)
1297 list_add(&fce->list, head);
1298}
1299
1300static int devm_name_match(struct device *dev, void *res,
1301 void *match_data)
1302{
1303 struct fw_name_devm *fwn = res;
1304 return (fwn->magic == (unsigned long)match_data);
1305}
1306
Ming Leiab6dd8e2012-08-17 22:07:00 +08001307static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001308{
1309 LIST_HEAD(todo);
1310 struct fw_cache_entry *fce;
1311 struct fw_cache_entry *fce_next;
1312 struct firmware_cache *fwc = &fw_cache;
1313
1314 devres_for_each_res(dev, fw_name_devm_release,
1315 devm_name_match, &fw_cache,
1316 dev_create_fw_entry, &todo);
1317
1318 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1319 list_del(&fce->list);
1320
1321 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001322 /* only one cache entry for one firmware */
1323 if (!__fw_entry_found(fce->name)) {
Ming Lei373304f2012-10-09 12:01:01 +08001324 list_add(&fce->list, &fwc->fw_names);
1325 } else {
1326 free_fw_cache_entry(fce);
1327 fce = NULL;
1328 }
Ming Lei37276a52012-08-04 12:01:27 +08001329 spin_unlock(&fwc->name_lock);
1330
Ming Lei373304f2012-10-09 12:01:01 +08001331 if (fce)
Ming Leid28d3882012-10-09 12:01:04 +08001332 async_schedule_domain(__async_dev_cache_fw_image,
1333 (void *)fce,
1334 &fw_cache_domain);
Ming Lei37276a52012-08-04 12:01:27 +08001335 }
1336}
1337
1338static void __device_uncache_fw_images(void)
1339{
1340 struct firmware_cache *fwc = &fw_cache;
1341 struct fw_cache_entry *fce;
1342
1343 spin_lock(&fwc->name_lock);
1344 while (!list_empty(&fwc->fw_names)) {
1345 fce = list_entry(fwc->fw_names.next,
1346 struct fw_cache_entry, list);
1347 list_del(&fce->list);
1348 spin_unlock(&fwc->name_lock);
1349
1350 uncache_firmware(fce->name);
1351 free_fw_cache_entry(fce);
1352
1353 spin_lock(&fwc->name_lock);
1354 }
1355 spin_unlock(&fwc->name_lock);
1356}
1357
1358/**
1359 * device_cache_fw_images - cache devices' firmware
1360 *
1361 * If one device called request_firmware or its nowait version
1362 * successfully before, the firmware names are recored into the
1363 * device's devres link list, so device_cache_fw_images can call
1364 * cache_firmware() to cache these firmwares for the device,
1365 * then the device driver can load its firmwares easily at
1366 * time when system is not ready to complete loading firmware.
1367 */
1368static void device_cache_fw_images(void)
1369{
1370 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001371 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001372 DEFINE_WAIT(wait);
1373
1374 pr_debug("%s\n", __func__);
1375
Ming Lei373304f2012-10-09 12:01:01 +08001376 /* cancel uncache work */
1377 cancel_delayed_work_sync(&fwc->work);
1378
Ming Leiffe53f62012-08-04 12:01:28 +08001379 /*
1380 * use small loading timeout for caching devices' firmware
1381 * because all these firmware images have been loaded
1382 * successfully at lease once, also system is ready for
1383 * completing firmware loading now. The maximum size of
1384 * firmware in current distributions is about 2M bytes,
1385 * so 10 secs should be enough.
1386 */
1387 old_timeout = loading_timeout;
1388 loading_timeout = 10;
1389
Ming Leiac39b3e2012-08-20 19:04:16 +08001390 mutex_lock(&fw_lock);
1391 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001392 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001393 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001394
1395 /* wait for completion of caching firmware for all devices */
Ming Leid28d3882012-10-09 12:01:04 +08001396 async_synchronize_full_domain(&fw_cache_domain);
Ming Leiffe53f62012-08-04 12:01:28 +08001397
1398 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001399}
1400
1401/**
1402 * device_uncache_fw_images - uncache devices' firmware
1403 *
1404 * uncache all firmwares which have been cached successfully
1405 * by device_uncache_fw_images earlier
1406 */
1407static void device_uncache_fw_images(void)
1408{
1409 pr_debug("%s\n", __func__);
1410 __device_uncache_fw_images();
1411}
1412
1413static void device_uncache_fw_images_work(struct work_struct *work)
1414{
1415 device_uncache_fw_images();
1416}
1417
1418/**
1419 * device_uncache_fw_images_delay - uncache devices firmwares
1420 * @delay: number of milliseconds to delay uncache device firmwares
1421 *
1422 * uncache all devices's firmwares which has been cached successfully
1423 * by device_cache_fw_images after @delay milliseconds.
1424 */
1425static void device_uncache_fw_images_delay(unsigned long delay)
1426{
1427 schedule_delayed_work(&fw_cache.work,
1428 msecs_to_jiffies(delay));
1429}
1430
Ming Lei07646d92012-08-04 12:01:29 +08001431static int fw_pm_notify(struct notifier_block *notify_block,
1432 unsigned long mode, void *unused)
1433{
1434 switch (mode) {
1435 case PM_HIBERNATION_PREPARE:
1436 case PM_SUSPEND_PREPARE:
1437 device_cache_fw_images();
1438 break;
1439
1440 case PM_POST_SUSPEND:
1441 case PM_POST_HIBERNATION:
1442 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001443 /*
1444 * In case that system sleep failed and syscore_suspend is
1445 * not called.
1446 */
1447 mutex_lock(&fw_lock);
1448 fw_cache.state = FW_LOADER_NO_CACHE;
1449 mutex_unlock(&fw_lock);
1450
Ming Lei07646d92012-08-04 12:01:29 +08001451 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1452 break;
1453 }
1454
1455 return 0;
1456}
Ming Lei07646d92012-08-04 12:01:29 +08001457
Ming Leiac39b3e2012-08-20 19:04:16 +08001458/* stop caching firmware once syscore_suspend is reached */
1459static int fw_suspend(void)
1460{
1461 fw_cache.state = FW_LOADER_NO_CACHE;
1462 return 0;
1463}
1464
1465static struct syscore_ops fw_syscore_ops = {
1466 .suspend = fw_suspend,
1467};
Ming Leicfe016b2012-09-08 17:32:30 +08001468#else
1469static int fw_cache_piggyback_on_request(const char *name)
1470{
1471 return 0;
1472}
1473#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001474
Ming Lei37276a52012-08-04 12:01:27 +08001475static void __init fw_cache_init(void)
1476{
1477 spin_lock_init(&fw_cache.lock);
1478 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001479 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001480
Ming Leicfe016b2012-09-08 17:32:30 +08001481#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +08001482 spin_lock_init(&fw_cache.name_lock);
1483 INIT_LIST_HEAD(&fw_cache.fw_names);
Ming Lei37276a52012-08-04 12:01:27 +08001484
Ming Lei37276a52012-08-04 12:01:27 +08001485 INIT_DELAYED_WORK(&fw_cache.work,
1486 device_uncache_fw_images_work);
Ming Lei07646d92012-08-04 12:01:29 +08001487
1488 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1489 register_pm_notifier(&fw_cache.pm_notify);
Ming Leiac39b3e2012-08-20 19:04:16 +08001490
1491 register_syscore_ops(&fw_syscore_ops);
Ming Leicfe016b2012-09-08 17:32:30 +08001492#endif
Ming Lei37276a52012-08-04 12:01:27 +08001493}
1494
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001495static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
Ming Lei1f2b7952012-08-04 12:01:21 +08001497 fw_cache_init();
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001498 return class_register(&firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001500
1501static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Ming Leicfe016b2012-09-08 17:32:30 +08001503#ifdef CONFIG_PM_SLEEP
Ming Leiac39b3e2012-08-20 19:04:16 +08001504 unregister_syscore_ops(&fw_syscore_ops);
Ming Lei07646d92012-08-04 12:01:29 +08001505 unregister_pm_notifier(&fw_cache.pm_notify);
Ming Leicfe016b2012-09-08 17:32:30 +08001506#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 class_unregister(&firmware_class);
1508}
1509
Shaohua Lia30a6a22006-09-27 01:50:52 -07001510fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511module_exit(firmware_class_exit);
1512
1513EXPORT_SYMBOL(release_firmware);
1514EXPORT_SYMBOL(request_firmware);
1515EXPORT_SYMBOL(request_firmware_nowait);
Ming Lei2887b392012-08-04 12:01:22 +08001516EXPORT_SYMBOL_GPL(cache_firmware);
1517EXPORT_SYMBOL_GPL(uncache_firmware);