blob: d81460309182fde4177865a922f740c278c7d40c [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);
308 if (size < 0)
309 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
322static bool fw_get_filesystem_firmware(struct firmware_buf *buf)
323{
324 int i;
325 bool success = false;
326 char *path = __getname();
327
328 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
329 struct file *file;
Ming Lei27602842012-11-03 17:47:58 +0800330
331 /* skip the unset customized path */
332 if (!fw_path[i][0])
333 continue;
334
Ming Lei746058f2012-10-09 12:01:03 +0800335 snprintf(path, PATH_MAX, "%s/%s", fw_path[i], buf->fw_id);
336
337 file = filp_open(path, O_RDONLY, 0);
338 if (IS_ERR(file))
339 continue;
340 success = fw_read_file_contents(file, buf);
341 fput(file);
342 if (success)
343 break;
344 }
345 __putname(path);
346 return success;
347}
348
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700349static struct firmware_priv *to_firmware_priv(struct device *dev)
350{
351 return container_of(dev, struct firmware_priv, dev);
352}
353
354static void fw_load_abort(struct firmware_priv *fw_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Ming Lei12446912012-08-04 12:01:20 +0800356 struct firmware_buf *buf = fw_priv->buf;
357
358 set_bit(FW_STATUS_ABORT, &buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800359 complete_all(&buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700362static ssize_t firmware_timeout_show(struct class *class,
363 struct class_attribute *attr,
364 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
366 return sprintf(buf, "%d\n", loading_timeout);
367}
368
369/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800370 * firmware_timeout_store - set number of seconds to wait for firmware
371 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800372 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800373 * @buf: buffer to scan for timeout value
374 * @count: number of bytes in @buf
375 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800377 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 * firmware will be provided.
379 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800380 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700382static ssize_t firmware_timeout_store(struct class *class,
383 struct class_attribute *attr,
384 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700387 if (loading_timeout < 0)
388 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return count;
391}
392
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800393static struct class_attribute firmware_class_attrs[] = {
394 __ATTR(timeout, S_IWUSR | S_IRUGO,
395 firmware_timeout_show, firmware_timeout_store),
396 __ATTR_NULL
397};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800399static void fw_dev_release(struct device *dev)
400{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700401 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800402
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800403 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800404
405 module_put(THIS_MODULE);
406}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Kay Sievers7eff2e72007-08-14 15:15:12 +0200408static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700410 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Ming Lei12446912012-08-04 12:01:20 +0800412 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200414 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700415 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200416 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
417 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 return 0;
420}
421
Adrian Bunk1b81d662006-05-20 15:00:16 -0700422static struct class firmware_class = {
423 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800424 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700425 .dev_uevent = firmware_uevent,
426 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700427};
428
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700429static ssize_t firmware_loading_show(struct device *dev,
430 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700432 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800433 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return sprintf(buf, "%d\n", loading);
436}
437
Ming Lei65710cb2012-08-04 12:01:16 +0800438/* firmware holds the ownership of pages */
David Woodhousedd336c52010-05-02 11:21:21 +0300439static void firmware_free_data(const struct firmware *fw)
440{
Linus Torvaldsabb139e2012-10-03 15:58:32 -0700441 /* Loaded directly? */
442 if (!fw->priv) {
443 vfree(fw->data);
444 return;
445 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800446 fw_free_buf(fw->priv);
David Woodhousedd336c52010-05-02 11:21:21 +0300447}
448
David Woodhouse6e03a202009-04-09 22:04:07 -0700449/* Some architectures don't have PAGE_KERNEL_RO */
450#ifndef PAGE_KERNEL_RO
451#define PAGE_KERNEL_RO PAGE_KERNEL
452#endif
Ming Lei253c9242012-10-09 12:01:02 +0800453
454/* one pages buffer should be mapped/unmapped only once */
455static int fw_map_pages_buf(struct firmware_buf *buf)
456{
Ming Lei746058f2012-10-09 12:01:03 +0800457 if (buf->fmt != PAGE_BUF)
458 return 0;
459
Ming Lei253c9242012-10-09 12:01:02 +0800460 if (buf->data)
461 vunmap(buf->data);
462 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
463 if (!buf->data)
464 return -ENOMEM;
465 return 0;
466}
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800469 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700470 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800471 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800472 * @buf: buffer to scan for loading control value
473 * @count: number of bytes in @buf
474 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 * The relevant values are:
476 *
477 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800478 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 * -1: Conclude the load with an error and discard any written data.
480 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700481static ssize_t firmware_loading_store(struct device *dev,
482 struct device_attribute *attr,
483 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700485 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800486 struct firmware_buf *fw_buf = fw_priv->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700488 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Neil Hormaneea915b2012-01-02 15:31:23 -0500490 mutex_lock(&fw_lock);
491
Ming Lei12446912012-08-04 12:01:20 +0800492 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500493 goto out;
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 switch (loading) {
496 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800497 /* discarding any previous partial load */
Ming Lei12446912012-08-04 12:01:20 +0800498 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
499 for (i = 0; i < fw_buf->nr_pages; i++)
500 __free_page(fw_buf->pages[i]);
501 kfree(fw_buf->pages);
502 fw_buf->pages = NULL;
503 fw_buf->page_array_size = 0;
504 fw_buf->nr_pages = 0;
505 set_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei28eefa72012-08-04 12:01:17 +0800506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 break;
508 case 0:
Ming Lei12446912012-08-04 12:01:20 +0800509 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
510 set_bit(FW_STATUS_DONE, &fw_buf->status);
511 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei253c9242012-10-09 12:01:02 +0800512
513 /*
514 * Several loading requests may be pending on
515 * one same firmware buf, so let all requests
516 * see the mapped 'buf->data' once the loading
517 * is completed.
518 * */
519 fw_map_pages_buf(fw_buf);
Ming Lei1f2b7952012-08-04 12:01:21 +0800520 complete_all(&fw_buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 break;
522 }
523 /* fallthrough */
524 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700525 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /* fallthrough */
527 case -1:
528 fw_load_abort(fw_priv);
529 break;
530 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500531out:
532 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return count;
534}
535
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700536static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700538static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
539 struct bin_attribute *bin_attr,
540 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200542 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700543 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800544 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700545 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Laura Garciacad1e552006-05-23 23:22:38 +0200547 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800548 buf = fw_priv->buf;
549 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 ret_count = -ENODEV;
551 goto out;
552 }
Ming Lei12446912012-08-04 12:01:20 +0800553 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200554 ret_count = 0;
555 goto out;
556 }
Ming Lei12446912012-08-04 12:01:20 +0800557 if (count > buf->size - offset)
558 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700559
560 ret_count = count;
561
562 while (count) {
563 void *page_data;
564 int page_nr = offset >> PAGE_SHIFT;
565 int page_ofs = offset & (PAGE_SIZE-1);
566 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
567
Ming Lei12446912012-08-04 12:01:20 +0800568 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700569
570 memcpy(buffer, page_data + page_ofs, page_cnt);
571
Ming Lei12446912012-08-04 12:01:20 +0800572 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700573 buffer += page_cnt;
574 offset += page_cnt;
575 count -= page_cnt;
576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577out:
Laura Garciacad1e552006-05-23 23:22:38 +0200578 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return ret_count;
580}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800581
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700582static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Ming Lei12446912012-08-04 12:01:20 +0800584 struct firmware_buf *buf = fw_priv->buf;
David Woodhouse6e03a202009-04-09 22:04:07 -0700585 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
David Woodhouse6e03a202009-04-09 22:04:07 -0700587 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800588 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700589 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800590 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700591 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
David Woodhouse6e03a202009-04-09 22:04:07 -0700593 new_pages = kmalloc(new_array_size * sizeof(void *),
594 GFP_KERNEL);
595 if (!new_pages) {
596 fw_load_abort(fw_priv);
597 return -ENOMEM;
598 }
Ming Lei12446912012-08-04 12:01:20 +0800599 memcpy(new_pages, buf->pages,
600 buf->page_array_size * sizeof(void *));
601 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
602 (new_array_size - buf->page_array_size));
603 kfree(buf->pages);
604 buf->pages = new_pages;
605 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700607
Ming Lei12446912012-08-04 12:01:20 +0800608 while (buf->nr_pages < pages_needed) {
609 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700610 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
611
Ming Lei12446912012-08-04 12:01:20 +0800612 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700613 fw_load_abort(fw_priv);
614 return -ENOMEM;
615 }
Ming Lei12446912012-08-04 12:01:20 +0800616 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return 0;
619}
620
621/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800622 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700623 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700624 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700625 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800626 * @buffer: buffer being written
627 * @offset: buffer offset for write in total data store area
628 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800630 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 * the driver as a firmware image.
632 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700633static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
634 struct bin_attribute *bin_attr,
635 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200637 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700638 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800639 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 ssize_t retval;
641
642 if (!capable(CAP_SYS_RAWIO))
643 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700644
Laura Garciacad1e552006-05-23 23:22:38 +0200645 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800646 buf = fw_priv->buf;
647 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 retval = -ENODEV;
649 goto out;
650 }
Ming Lei65710cb2012-08-04 12:01:16 +0800651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 retval = fw_realloc_buffer(fw_priv, offset + count);
653 if (retval)
654 goto out;
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700657
658 while (count) {
659 void *page_data;
660 int page_nr = offset >> PAGE_SHIFT;
661 int page_ofs = offset & (PAGE_SIZE - 1);
662 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
663
Ming Lei12446912012-08-04 12:01:20 +0800664 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700665
666 memcpy(page_data + page_ofs, buffer, page_cnt);
667
Ming Lei12446912012-08-04 12:01:20 +0800668 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700669 buffer += page_cnt;
670 offset += page_cnt;
671 count -= page_cnt;
672 }
673
Ming Lei12446912012-08-04 12:01:20 +0800674 buf->size = max_t(size_t, offset, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675out:
Laura Garciacad1e552006-05-23 23:22:38 +0200676 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return retval;
678}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800679
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700680static struct bin_attribute firmware_attr_data = {
681 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 .size = 0,
683 .read = firmware_data_read,
684 .write = firmware_data_write,
685};
686
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800687static void firmware_class_timeout_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800689 struct firmware_priv *fw_priv = container_of(work,
690 struct firmware_priv, timeout_work.work);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700691
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800692 mutex_lock(&fw_lock);
693 if (test_bit(FW_STATUS_DONE, &(fw_priv->buf->status))) {
694 mutex_unlock(&fw_lock);
695 return;
696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 fw_load_abort(fw_priv);
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800698 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700701static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200702fw_create_instance(struct firmware *firmware, const char *fw_name,
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700703 struct device *device, bool uevent, bool nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700705 struct firmware_priv *fw_priv;
706 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Ming Lei12446912012-08-04 12:01:20 +0800708 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700709 if (!fw_priv) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700710 dev_err(device, "%s: kmalloc failed\n", __func__);
Ming Lei12446912012-08-04 12:01:20 +0800711 fw_priv = ERR_PTR(-ENOMEM);
712 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700715 fw_priv->nowait = nowait;
Ming Lei1f2b7952012-08-04 12:01:21 +0800716 fw_priv->fw = firmware;
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800717 INIT_DELAYED_WORK(&fw_priv->timeout_work,
718 firmware_class_timeout_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700720 f_dev = &fw_priv->dev;
721
722 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800723 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700724 f_dev->parent = device;
725 f_dev->class = &firmware_class;
Ming Lei12446912012-08-04 12:01:20 +0800726exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700727 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
Ming Lei1f2b7952012-08-04 12:01:21 +0800730/* store the pages buffer info firmware from buf */
731static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
732{
733 fw->priv = buf;
734 fw->pages = buf->pages;
735 fw->size = buf->size;
736 fw->data = buf->data;
737
738 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
739 __func__, buf->fw_id, buf, buf->data,
740 (unsigned int)buf->size);
741}
742
Ming Leicfe016b2012-09-08 17:32:30 +0800743#ifdef CONFIG_PM_SLEEP
Ming Leif531f05a2012-08-04 12:01:25 +0800744static void fw_name_devm_release(struct device *dev, void *res)
745{
746 struct fw_name_devm *fwn = res;
747
748 if (fwn->magic == (unsigned long)&fw_cache)
749 pr_debug("%s: fw_name-%s devm-%p released\n",
750 __func__, fwn->name, res);
751}
752
753static int fw_devm_match(struct device *dev, void *res,
754 void *match_data)
755{
756 struct fw_name_devm *fwn = res;
757
758 return (fwn->magic == (unsigned long)&fw_cache) &&
759 !strcmp(fwn->name, match_data);
760}
761
762static struct fw_name_devm *fw_find_devm_name(struct device *dev,
763 const char *name)
764{
765 struct fw_name_devm *fwn;
766
767 fwn = devres_find(dev, fw_name_devm_release,
768 fw_devm_match, (void *)name);
769 return fwn;
770}
771
772/* add firmware name into devres list */
773static int fw_add_devm_name(struct device *dev, const char *name)
774{
775 struct fw_name_devm *fwn;
776
777 fwn = fw_find_devm_name(dev, name);
778 if (fwn)
779 return 1;
780
781 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
782 strlen(name) + 1, GFP_KERNEL);
783 if (!fwn)
784 return -ENOMEM;
785
786 fwn->magic = (unsigned long)&fw_cache;
787 strcpy(fwn->name, name);
788 devres_add(dev, fwn);
789
790 return 0;
791}
Ming Leicfe016b2012-09-08 17:32:30 +0800792#else
793static int fw_add_devm_name(struct device *dev, const char *name)
794{
795 return 0;
796}
797#endif
Ming Leif531f05a2012-08-04 12:01:25 +0800798
Ming Lei1f2b7952012-08-04 12:01:21 +0800799static void _request_firmware_cleanup(const struct firmware **firmware_p)
800{
801 release_firmware(*firmware_p);
802 *firmware_p = NULL;
803}
804
Stephen Boyddddb5542012-03-28 23:30:43 +0200805static struct firmware_priv *
806_request_firmware_prepare(const struct firmware **firmware_p, const char *name,
807 struct device *device, bool uevent, bool nowait)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700808{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +0800810 struct firmware_priv *fw_priv = NULL;
811 struct firmware_buf *buf;
812 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 if (!firmware_p)
Stephen Boyddddb5542012-03-28 23:30:43 +0200815 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Jiri Slaby4aed0642005-09-13 01:25:01 -0700817 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700819 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
820 __func__);
Stephen Boyddddb5542012-03-28 23:30:43 +0200821 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800824 if (fw_get_builtin_firmware(firmware, name)) {
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100825 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
Stephen Boyddddb5542012-03-28 23:30:43 +0200826 return NULL;
David Woodhouse5658c762008-05-23 13:52:42 +0100827 }
828
Ming Lei1f2b7952012-08-04 12:01:21 +0800829 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
830 if (!ret)
831 fw_priv = fw_create_instance(firmware, name, device,
832 uevent, nowait);
833
834 if (IS_ERR(fw_priv) || ret < 0) {
835 kfree(firmware);
Stephen Boyddddb5542012-03-28 23:30:43 +0200836 *firmware_p = NULL;
Ming Lei1f2b7952012-08-04 12:01:21 +0800837 return ERR_PTR(-ENOMEM);
838 } else if (fw_priv) {
839 fw_priv->buf = buf;
840
841 /*
842 * bind with 'buf' now to avoid warning in failure path
843 * of requesting firmware.
844 */
845 firmware->priv = buf;
846 return fw_priv;
Stephen Boyddddb5542012-03-28 23:30:43 +0200847 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800848
849 /* share the cached buf, which is inprogessing or completed */
850 check_status:
851 mutex_lock(&fw_lock);
852 if (test_bit(FW_STATUS_ABORT, &buf->status)) {
853 fw_priv = ERR_PTR(-ENOENT);
Ming Leief40bb12012-08-20 19:04:15 +0800854 firmware->priv = buf;
Ming Lei1f2b7952012-08-04 12:01:21 +0800855 _request_firmware_cleanup(firmware_p);
856 goto exit;
857 } else if (test_bit(FW_STATUS_DONE, &buf->status)) {
858 fw_priv = NULL;
859 fw_set_page_data(buf, firmware);
860 goto exit;
861 }
862 mutex_unlock(&fw_lock);
863 wait_for_completion(&buf->completion);
864 goto check_status;
865
866exit:
867 mutex_unlock(&fw_lock);
Stephen Boyddddb5542012-03-28 23:30:43 +0200868 return fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200869}
870
Stephen Boyddddb5542012-03-28 23:30:43 +0200871static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
872 long timeout)
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200873{
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200874 int retval = 0;
Stephen Boyddddb5542012-03-28 23:30:43 +0200875 struct device *f_dev = &fw_priv->dev;
Ming Lei12446912012-08-04 12:01:20 +0800876 struct firmware_buf *buf = fw_priv->buf;
Ming Leiac39b3e2012-08-20 19:04:16 +0800877 struct firmware_cache *fwc = &fw_cache;
Ming Lei746058f2012-10-09 12:01:03 +0800878 int direct_load = 0;
879
880 /* try direct loading from fs first */
881 if (fw_get_filesystem_firmware(buf)) {
882 dev_dbg(f_dev->parent, "firmware: direct-loading"
883 " firmware %s\n", buf->fw_id);
884
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800885 mutex_lock(&fw_lock);
Ming Lei746058f2012-10-09 12:01:03 +0800886 set_bit(FW_STATUS_DONE, &buf->status);
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800887 mutex_unlock(&fw_lock);
Ming Lei746058f2012-10-09 12:01:03 +0800888 complete_all(&buf->completion);
889 direct_load = 1;
890 goto handle_fw;
891 }
892
893 /* fall back on userspace loading */
894 buf->fmt = PAGE_BUF;
Linus Torvaldscaca9512011-08-24 15:55:30 -0700895
Stephen Boyddddb5542012-03-28 23:30:43 +0200896 dev_set_uevent_suppress(f_dev, true);
David Woodhouse5658c762008-05-23 13:52:42 +0100897
Stephen Boyddddb5542012-03-28 23:30:43 +0200898 /* Need to pin this module until class device is destroyed */
899 __module_get(THIS_MODULE);
900
901 retval = device_add(f_dev);
902 if (retval) {
903 dev_err(f_dev, "%s: device_register failed\n", __func__);
904 goto err_put_dev;
905 }
906
907 retval = device_create_bin_file(f_dev, &firmware_attr_data);
908 if (retval) {
909 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
910 goto err_del_dev;
911 }
912
913 retval = device_create_file(f_dev, &dev_attr_loading);
914 if (retval) {
915 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
916 goto err_del_bin_attr;
917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Kay Sievers312c0042005-11-16 09:00:00 +0100919 if (uevent) {
Stephen Boyddddb5542012-03-28 23:30:43 +0200920 dev_set_uevent_suppress(f_dev, false);
Ming Lei12446912012-08-04 12:01:20 +0800921 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200922 if (timeout != MAX_SCHEDULE_TIMEOUT)
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800923 schedule_delayed_work(&fw_priv->timeout_work, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700925 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
926 }
927
Ming Lei12446912012-08-04 12:01:20 +0800928 wait_for_completion(&buf->completion);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700929
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800930 cancel_delayed_work_sync(&fw_priv->timeout_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Ming Lei746058f2012-10-09 12:01:03 +0800932handle_fw:
Laura Garciacad1e552006-05-23 23:22:38 +0200933 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800934 if (!buf->size || test_bit(FW_STATUS_ABORT, &buf->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 retval = -ENOENT;
Ming Lei65710cb2012-08-04 12:01:16 +0800936
Ming Leif531f05a2012-08-04 12:01:25 +0800937 /*
938 * add firmware name into devres list so that we can auto cache
939 * and uncache firmware for device.
940 *
941 * f_dev->parent may has been deleted already, but the problem
942 * should be fixed in devres or driver core.
943 */
944 if (!retval && f_dev->parent)
945 fw_add_devm_name(f_dev->parent, buf->fw_id);
946
Ming Leiac39b3e2012-08-20 19:04:16 +0800947 /*
948 * After caching firmware image is started, let it piggyback
949 * on request firmware.
950 */
951 if (!retval && fwc->state == FW_LOADER_START_CACHE) {
952 if (fw_cache_piggyback_on_request(buf->fw_id))
953 kref_get(&buf->ref);
954 }
955
Ming Lei1f2b7952012-08-04 12:01:21 +0800956 /* pass the pages buffer to driver at the last minute */
957 fw_set_page_data(buf, fw_priv->fw);
958
Ming Lei12446912012-08-04 12:01:20 +0800959 fw_priv->buf = NULL;
Laura Garciacad1e552006-05-23 23:22:38 +0200960 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Ming Lei746058f2012-10-09 12:01:03 +0800962 if (direct_load)
963 goto err_put_dev;
964
Stephen Boyddddb5542012-03-28 23:30:43 +0200965 device_remove_file(f_dev, &dev_attr_loading);
966err_del_bin_attr:
967 device_remove_bin_file(f_dev, &firmware_attr_data);
968err_del_dev:
969 device_del(f_dev);
970err_put_dev:
971 put_device(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return retval;
973}
974
975/**
Kay Sievers312c0042005-11-16 09:00:00 +0100976 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800977 * @firmware_p: pointer to firmware image
978 * @name: name of firmware file
979 * @device: device for which firmware is being loaded
980 *
981 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700982 * of @name for device @device.
983 *
984 * Should be called from user context where sleeping is allowed.
985 *
Kay Sievers312c0042005-11-16 09:00:00 +0100986 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700987 * should be distinctive enough not to be confused with any other
988 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +0800989 *
990 * Caller must hold the reference count of @device.
Ming Lei6a927852012-11-03 17:48:16 +0800991 *
992 * The function can be called safely inside device's suspend and
993 * resume callback.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700994 **/
995int
996request_firmware(const struct firmware **firmware_p, const char *name,
997 struct device *device)
998{
Stephen Boyddddb5542012-03-28 23:30:43 +0200999 struct firmware_priv *fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001000 int ret;
1001
Stephen Boyddddb5542012-03-28 23:30:43 +02001002 fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
1003 false);
1004 if (IS_ERR_OR_NULL(fw_priv))
1005 return PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001006
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +02001007 ret = usermodehelper_read_trylock();
1008 if (WARN_ON(ret)) {
1009 dev_err(device, "firmware: %s will not be loaded\n", name);
1010 } else {
Stephen Boyddddb5542012-03-28 23:30:43 +02001011 ret = _request_firmware_load(fw_priv, true,
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +02001012 firmware_loading_timeout());
1013 usermodehelper_read_unlock();
1014 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001015 if (ret)
1016 _request_firmware_cleanup(firmware_p);
1017
1018 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001019}
1020
1021/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001023 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001025void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
1027 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001028 if (!fw_is_builtin_firmware(fw))
1029 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 kfree(fw);
1031 }
1032}
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034/* Async support */
1035struct firmware_work {
1036 struct work_struct work;
1037 struct module *module;
1038 const char *name;
1039 struct device *device;
1040 void *context;
1041 void (*cont)(const struct firmware *fw, void *context);
Bob Liu072fc8f2011-01-26 18:33:32 +08001042 bool uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043};
1044
Stephen Boyda36cf842012-03-28 23:31:00 +02001045static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
Stephen Boyda36cf842012-03-28 23:31:00 +02001047 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 const struct firmware *fw;
Stephen Boyddddb5542012-03-28 23:30:43 +02001049 struct firmware_priv *fw_priv;
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +02001050 long timeout;
matthieu castet113fab12005-11-13 16:07:39 -08001051 int ret;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001052
Stephen Boyda36cf842012-03-28 23:31:00 +02001053 fw_work = container_of(work, struct firmware_work, work);
Stephen Boyddddb5542012-03-28 23:30:43 +02001054 fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
1055 fw_work->uevent, true);
1056 if (IS_ERR_OR_NULL(fw_priv)) {
1057 ret = PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001058 goto out;
Stephen Boyddddb5542012-03-28 23:30:43 +02001059 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001060
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +02001061 timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
1062 if (timeout) {
Stephen Boyddddb5542012-03-28 23:30:43 +02001063 ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +02001064 usermodehelper_read_unlock();
1065 } else {
1066 dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
1067 fw_work->name);
1068 ret = -EAGAIN;
1069 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001070 if (ret)
1071 _request_firmware_cleanup(&fw);
1072
1073 out:
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001074 fw_work->cont(fw, fw_work->context);
Ming Lei0cfc1e12012-08-04 12:01:23 +08001075 put_device(fw_work->device);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 module_put(fw_work->module);
1078 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
1080
1081/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001082 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001083 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001084 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001085 * is non-zero else the firmware copy must be done manually.
1086 * @name: name of firmware file
1087 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001088 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001089 * @context: will be passed over to @cont, and
1090 * @fw may be %NULL if firmware request fails.
1091 * @cont: function will be called asynchronously when the firmware
1092 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001094 * Caller must hold the reference count of @device.
1095 *
Ming Lei6f21a622012-08-04 12:01:24 +08001096 * Asynchronous variant of request_firmware() for user contexts:
1097 * - sleep for as small periods as possible since it may
1098 * increase kernel boot time of built-in device drivers
1099 * requesting firmware in their ->probe() methods, if
1100 * @gfp is GFP_KERNEL.
1101 *
1102 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 **/
1104int
1105request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001106 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001107 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 void (*cont)(const struct firmware *fw, void *context))
1109{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001110 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001112 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (!fw_work)
1114 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001115
1116 fw_work->module = module;
1117 fw_work->name = name;
1118 fw_work->device = device;
1119 fw_work->context = context;
1120 fw_work->cont = cont;
1121 fw_work->uevent = uevent;
1122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (!try_module_get(module)) {
1124 kfree(fw_work);
1125 return -EFAULT;
1126 }
1127
Ming Lei0cfc1e12012-08-04 12:01:23 +08001128 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001129 INIT_WORK(&fw_work->work, request_firmware_work_func);
1130 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return 0;
1132}
1133
Ming Lei2887b392012-08-04 12:01:22 +08001134/**
1135 * cache_firmware - cache one firmware image in kernel memory space
1136 * @fw_name: the firmware image name
1137 *
1138 * Cache firmware in kernel memory so that drivers can use it when
1139 * system isn't ready for them to request firmware image from userspace.
1140 * Once it returns successfully, driver can use request_firmware or its
1141 * nowait version to get the cached firmware without any interacting
1142 * with userspace
1143 *
1144 * Return 0 if the firmware image has been cached successfully
1145 * Return !0 otherwise
1146 *
1147 */
1148int cache_firmware(const char *fw_name)
1149{
1150 int ret;
1151 const struct firmware *fw;
1152
1153 pr_debug("%s: %s\n", __func__, fw_name);
1154
1155 ret = request_firmware(&fw, fw_name, NULL);
1156 if (!ret)
1157 kfree(fw);
1158
1159 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1160
1161 return ret;
1162}
1163
1164/**
1165 * uncache_firmware - remove one cached firmware image
1166 * @fw_name: the firmware image name
1167 *
1168 * Uncache one firmware image which has been cached successfully
1169 * before.
1170 *
1171 * Return 0 if the firmware cache has been removed successfully
1172 * Return !0 otherwise
1173 *
1174 */
1175int uncache_firmware(const char *fw_name)
1176{
1177 struct firmware_buf *buf;
1178 struct firmware fw;
1179
1180 pr_debug("%s: %s\n", __func__, fw_name);
1181
1182 if (fw_get_builtin_firmware(&fw, fw_name))
1183 return 0;
1184
1185 buf = fw_lookup_buf(fw_name);
1186 if (buf) {
1187 fw_free_buf(buf);
1188 return 0;
1189 }
1190
1191 return -EINVAL;
1192}
1193
Ming Leicfe016b2012-09-08 17:32:30 +08001194#ifdef CONFIG_PM_SLEEP
Ming Leid28d3882012-10-09 12:01:04 +08001195static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1196
Ming Lei37276a52012-08-04 12:01:27 +08001197static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1198{
1199 struct fw_cache_entry *fce;
1200
1201 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1202 if (!fce)
1203 goto exit;
1204
1205 strcpy(fce->name, name);
1206exit:
1207 return fce;
1208}
1209
Ming Lei373304f2012-10-09 12:01:01 +08001210static int __fw_entry_found(const char *name)
1211{
1212 struct firmware_cache *fwc = &fw_cache;
1213 struct fw_cache_entry *fce;
1214
1215 list_for_each_entry(fce, &fwc->fw_names, list) {
1216 if (!strcmp(fce->name, name))
1217 return 1;
1218 }
1219 return 0;
1220}
1221
Ming Leiac39b3e2012-08-20 19:04:16 +08001222static int fw_cache_piggyback_on_request(const char *name)
1223{
1224 struct firmware_cache *fwc = &fw_cache;
1225 struct fw_cache_entry *fce;
1226 int ret = 0;
1227
1228 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001229 if (__fw_entry_found(name))
1230 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001231
1232 fce = alloc_fw_cache_entry(name);
1233 if (fce) {
1234 ret = 1;
1235 list_add(&fce->list, &fwc->fw_names);
1236 pr_debug("%s: fw: %s\n", __func__, name);
1237 }
1238found:
1239 spin_unlock(&fwc->name_lock);
1240 return ret;
1241}
1242
Ming Lei37276a52012-08-04 12:01:27 +08001243static void free_fw_cache_entry(struct fw_cache_entry *fce)
1244{
1245 kfree(fce);
1246}
1247
1248static void __async_dev_cache_fw_image(void *fw_entry,
1249 async_cookie_t cookie)
1250{
1251 struct fw_cache_entry *fce = fw_entry;
1252 struct firmware_cache *fwc = &fw_cache;
1253 int ret;
1254
1255 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001256 if (ret) {
1257 spin_lock(&fwc->name_lock);
1258 list_del(&fce->list);
1259 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001260
Ming Leiac39b3e2012-08-20 19:04:16 +08001261 free_fw_cache_entry(fce);
1262 }
Ming Lei37276a52012-08-04 12:01:27 +08001263}
1264
1265/* called with dev->devres_lock held */
1266static void dev_create_fw_entry(struct device *dev, void *res,
1267 void *data)
1268{
1269 struct fw_name_devm *fwn = res;
1270 const char *fw_name = fwn->name;
1271 struct list_head *head = data;
1272 struct fw_cache_entry *fce;
1273
1274 fce = alloc_fw_cache_entry(fw_name);
1275 if (fce)
1276 list_add(&fce->list, head);
1277}
1278
1279static int devm_name_match(struct device *dev, void *res,
1280 void *match_data)
1281{
1282 struct fw_name_devm *fwn = res;
1283 return (fwn->magic == (unsigned long)match_data);
1284}
1285
Ming Leiab6dd8e2012-08-17 22:07:00 +08001286static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001287{
1288 LIST_HEAD(todo);
1289 struct fw_cache_entry *fce;
1290 struct fw_cache_entry *fce_next;
1291 struct firmware_cache *fwc = &fw_cache;
1292
1293 devres_for_each_res(dev, fw_name_devm_release,
1294 devm_name_match, &fw_cache,
1295 dev_create_fw_entry, &todo);
1296
1297 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1298 list_del(&fce->list);
1299
1300 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001301 /* only one cache entry for one firmware */
1302 if (!__fw_entry_found(fce->name)) {
Ming Lei373304f2012-10-09 12:01:01 +08001303 list_add(&fce->list, &fwc->fw_names);
1304 } else {
1305 free_fw_cache_entry(fce);
1306 fce = NULL;
1307 }
Ming Lei37276a52012-08-04 12:01:27 +08001308 spin_unlock(&fwc->name_lock);
1309
Ming Lei373304f2012-10-09 12:01:01 +08001310 if (fce)
Ming Leid28d3882012-10-09 12:01:04 +08001311 async_schedule_domain(__async_dev_cache_fw_image,
1312 (void *)fce,
1313 &fw_cache_domain);
Ming Lei37276a52012-08-04 12:01:27 +08001314 }
1315}
1316
1317static void __device_uncache_fw_images(void)
1318{
1319 struct firmware_cache *fwc = &fw_cache;
1320 struct fw_cache_entry *fce;
1321
1322 spin_lock(&fwc->name_lock);
1323 while (!list_empty(&fwc->fw_names)) {
1324 fce = list_entry(fwc->fw_names.next,
1325 struct fw_cache_entry, list);
1326 list_del(&fce->list);
1327 spin_unlock(&fwc->name_lock);
1328
1329 uncache_firmware(fce->name);
1330 free_fw_cache_entry(fce);
1331
1332 spin_lock(&fwc->name_lock);
1333 }
1334 spin_unlock(&fwc->name_lock);
1335}
1336
1337/**
1338 * device_cache_fw_images - cache devices' firmware
1339 *
1340 * If one device called request_firmware or its nowait version
1341 * successfully before, the firmware names are recored into the
1342 * device's devres link list, so device_cache_fw_images can call
1343 * cache_firmware() to cache these firmwares for the device,
1344 * then the device driver can load its firmwares easily at
1345 * time when system is not ready to complete loading firmware.
1346 */
1347static void device_cache_fw_images(void)
1348{
1349 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001350 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001351 DEFINE_WAIT(wait);
1352
1353 pr_debug("%s\n", __func__);
1354
Ming Lei373304f2012-10-09 12:01:01 +08001355 /* cancel uncache work */
1356 cancel_delayed_work_sync(&fwc->work);
1357
Ming Leiffe53f62012-08-04 12:01:28 +08001358 /*
1359 * use small loading timeout for caching devices' firmware
1360 * because all these firmware images have been loaded
1361 * successfully at lease once, also system is ready for
1362 * completing firmware loading now. The maximum size of
1363 * firmware in current distributions is about 2M bytes,
1364 * so 10 secs should be enough.
1365 */
1366 old_timeout = loading_timeout;
1367 loading_timeout = 10;
1368
Ming Leiac39b3e2012-08-20 19:04:16 +08001369 mutex_lock(&fw_lock);
1370 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001371 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001372 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001373
1374 /* wait for completion of caching firmware for all devices */
Ming Leid28d3882012-10-09 12:01:04 +08001375 async_synchronize_full_domain(&fw_cache_domain);
Ming Leiffe53f62012-08-04 12:01:28 +08001376
1377 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001378}
1379
1380/**
1381 * device_uncache_fw_images - uncache devices' firmware
1382 *
1383 * uncache all firmwares which have been cached successfully
1384 * by device_uncache_fw_images earlier
1385 */
1386static void device_uncache_fw_images(void)
1387{
1388 pr_debug("%s\n", __func__);
1389 __device_uncache_fw_images();
1390}
1391
1392static void device_uncache_fw_images_work(struct work_struct *work)
1393{
1394 device_uncache_fw_images();
1395}
1396
1397/**
1398 * device_uncache_fw_images_delay - uncache devices firmwares
1399 * @delay: number of milliseconds to delay uncache device firmwares
1400 *
1401 * uncache all devices's firmwares which has been cached successfully
1402 * by device_cache_fw_images after @delay milliseconds.
1403 */
1404static void device_uncache_fw_images_delay(unsigned long delay)
1405{
1406 schedule_delayed_work(&fw_cache.work,
1407 msecs_to_jiffies(delay));
1408}
1409
Ming Lei07646d92012-08-04 12:01:29 +08001410static int fw_pm_notify(struct notifier_block *notify_block,
1411 unsigned long mode, void *unused)
1412{
1413 switch (mode) {
1414 case PM_HIBERNATION_PREPARE:
1415 case PM_SUSPEND_PREPARE:
1416 device_cache_fw_images();
1417 break;
1418
1419 case PM_POST_SUSPEND:
1420 case PM_POST_HIBERNATION:
1421 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001422 /*
1423 * In case that system sleep failed and syscore_suspend is
1424 * not called.
1425 */
1426 mutex_lock(&fw_lock);
1427 fw_cache.state = FW_LOADER_NO_CACHE;
1428 mutex_unlock(&fw_lock);
1429
Ming Lei07646d92012-08-04 12:01:29 +08001430 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1431 break;
1432 }
1433
1434 return 0;
1435}
Ming Lei07646d92012-08-04 12:01:29 +08001436
Ming Leiac39b3e2012-08-20 19:04:16 +08001437/* stop caching firmware once syscore_suspend is reached */
1438static int fw_suspend(void)
1439{
1440 fw_cache.state = FW_LOADER_NO_CACHE;
1441 return 0;
1442}
1443
1444static struct syscore_ops fw_syscore_ops = {
1445 .suspend = fw_suspend,
1446};
Ming Leicfe016b2012-09-08 17:32:30 +08001447#else
1448static int fw_cache_piggyback_on_request(const char *name)
1449{
1450 return 0;
1451}
1452#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001453
Ming Lei37276a52012-08-04 12:01:27 +08001454static void __init fw_cache_init(void)
1455{
1456 spin_lock_init(&fw_cache.lock);
1457 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001458 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001459
Ming Leicfe016b2012-09-08 17:32:30 +08001460#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +08001461 spin_lock_init(&fw_cache.name_lock);
1462 INIT_LIST_HEAD(&fw_cache.fw_names);
Ming Lei37276a52012-08-04 12:01:27 +08001463
Ming Lei37276a52012-08-04 12:01:27 +08001464 INIT_DELAYED_WORK(&fw_cache.work,
1465 device_uncache_fw_images_work);
Ming Lei07646d92012-08-04 12:01:29 +08001466
1467 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1468 register_pm_notifier(&fw_cache.pm_notify);
Ming Leiac39b3e2012-08-20 19:04:16 +08001469
1470 register_syscore_ops(&fw_syscore_ops);
Ming Leicfe016b2012-09-08 17:32:30 +08001471#endif
Ming Lei37276a52012-08-04 12:01:27 +08001472}
1473
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001474static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
Ming Lei1f2b7952012-08-04 12:01:21 +08001476 fw_cache_init();
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001477 return class_register(&firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001479
1480static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
Ming Leicfe016b2012-09-08 17:32:30 +08001482#ifdef CONFIG_PM_SLEEP
Ming Leiac39b3e2012-08-20 19:04:16 +08001483 unregister_syscore_ops(&fw_syscore_ops);
Ming Lei07646d92012-08-04 12:01:29 +08001484 unregister_pm_notifier(&fw_cache.pm_notify);
Ming Leicfe016b2012-09-08 17:32:30 +08001485#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 class_unregister(&firmware_class);
1487}
1488
Shaohua Lia30a6a22006-09-27 01:50:52 -07001489fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490module_exit(firmware_class_exit);
1491
1492EXPORT_SYMBOL(release_firmware);
1493EXPORT_SYMBOL(request_firmware);
1494EXPORT_SYMBOL(request_firmware_nowait);
Ming Lei2887b392012-08-04 12:01:22 +08001495EXPORT_SYMBOL_GPL(cache_firmware);
1496EXPORT_SYMBOL_GPL(uncache_firmware);