blob: a095d84ddfd9cf4b41f8ec02827e933f81c8c0c6 [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
119 wait_queue_head_t wait_queue;
120 int cnt;
121 struct delayed_work work;
Ming Lei07646d92012-08-04 12:01:29 +0800122
123 struct notifier_block pm_notify;
Ming Leicfe016b2012-09-08 17:32:30 +0800124#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800125};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Ming Lei12446912012-08-04 12:01:20 +0800127struct firmware_buf {
Ming Lei1f2b7952012-08-04 12:01:21 +0800128 struct kref ref;
129 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 struct completion completion;
Ming Lei1f2b7952012-08-04 12:01:21 +0800131 struct firmware_cache *fwc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 unsigned long status;
Ming Lei746058f2012-10-09 12:01:03 +0800133 enum fw_buf_fmt fmt;
Ming Lei65710cb2012-08-04 12:01:16 +0800134 void *data;
135 size_t size;
David Woodhouse6e03a202009-04-09 22:04:07 -0700136 struct page **pages;
137 int nr_pages;
138 int page_array_size;
Dmitry Torokhove1771232010-03-13 23:49:23 -0800139 char fw_id[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140};
141
Ming Lei37276a52012-08-04 12:01:27 +0800142struct fw_cache_entry {
143 struct list_head list;
144 char name[];
145};
146
Ming Lei12446912012-08-04 12:01:20 +0800147struct firmware_priv {
148 struct timer_list timeout;
149 bool nowait;
150 struct device dev;
151 struct firmware_buf *buf;
Ming Lei1f2b7952012-08-04 12:01:21 +0800152 struct firmware *fw;
Ming Lei12446912012-08-04 12:01:20 +0800153};
154
Ming Leif531f05a2012-08-04 12:01:25 +0800155struct fw_name_devm {
156 unsigned long magic;
157 char name[];
158};
159
Ming Lei1f2b7952012-08-04 12:01:21 +0800160#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
161
Ming Leiac39b3e2012-08-20 19:04:16 +0800162#define FW_LOADER_NO_CACHE 0
163#define FW_LOADER_START_CACHE 1
164
165static int fw_cache_piggyback_on_request(const char *name);
166
Ming Lei1f2b7952012-08-04 12:01:21 +0800167/* fw_lock could be moved to 'struct firmware_priv' but since it is just
168 * guarding for corner cases a global lock should be OK */
169static DEFINE_MUTEX(fw_lock);
170
171static struct firmware_cache fw_cache;
172
173static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
174 struct firmware_cache *fwc)
175{
176 struct firmware_buf *buf;
177
178 buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
179
180 if (!buf)
181 return buf;
182
183 kref_init(&buf->ref);
184 strcpy(buf->fw_id, fw_name);
185 buf->fwc = fwc;
186 init_completion(&buf->completion);
Ming Lei746058f2012-10-09 12:01:03 +0800187 buf->fmt = VMALLOC_BUF;
Ming Lei1f2b7952012-08-04 12:01:21 +0800188
189 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
190
191 return buf;
192}
193
Ming Lei2887b392012-08-04 12:01:22 +0800194static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
195{
196 struct firmware_buf *tmp;
197 struct firmware_cache *fwc = &fw_cache;
198
199 list_for_each_entry(tmp, &fwc->head, list)
200 if (!strcmp(tmp->fw_id, fw_name))
201 return tmp;
202 return NULL;
203}
204
Ming Lei1f2b7952012-08-04 12:01:21 +0800205static int fw_lookup_and_allocate_buf(const char *fw_name,
206 struct firmware_cache *fwc,
207 struct firmware_buf **buf)
208{
209 struct firmware_buf *tmp;
210
211 spin_lock(&fwc->lock);
Ming Lei2887b392012-08-04 12:01:22 +0800212 tmp = __fw_lookup_buf(fw_name);
213 if (tmp) {
214 kref_get(&tmp->ref);
215 spin_unlock(&fwc->lock);
216 *buf = tmp;
217 return 1;
218 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800219 tmp = __allocate_fw_buf(fw_name, fwc);
220 if (tmp)
221 list_add(&tmp->list, &fwc->head);
222 spin_unlock(&fwc->lock);
223
224 *buf = tmp;
225
226 return tmp ? 0 : -ENOMEM;
227}
228
Ming Lei2887b392012-08-04 12:01:22 +0800229static struct firmware_buf *fw_lookup_buf(const char *fw_name)
230{
231 struct firmware_buf *tmp;
232 struct firmware_cache *fwc = &fw_cache;
233
234 spin_lock(&fwc->lock);
235 tmp = __fw_lookup_buf(fw_name);
236 spin_unlock(&fwc->lock);
237
238 return tmp;
239}
240
Ming Lei1f2b7952012-08-04 12:01:21 +0800241static void __fw_free_buf(struct kref *ref)
242{
243 struct firmware_buf *buf = to_fwbuf(ref);
244 struct firmware_cache *fwc = buf->fwc;
245 int i;
246
247 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
248 __func__, buf->fw_id, buf, buf->data,
249 (unsigned int)buf->size);
250
251 spin_lock(&fwc->lock);
252 list_del(&buf->list);
253 spin_unlock(&fwc->lock);
254
Ming Lei746058f2012-10-09 12:01:03 +0800255
256 if (buf->fmt == PAGE_BUF) {
257 vunmap(buf->data);
258 for (i = 0; i < buf->nr_pages; i++)
259 __free_page(buf->pages[i]);
260 kfree(buf->pages);
261 } else
262 vfree(buf->data);
Ming Lei1f2b7952012-08-04 12:01:21 +0800263 kfree(buf);
264}
265
266static void fw_free_buf(struct firmware_buf *buf)
267{
268 kref_put(&buf->ref, __fw_free_buf);
269}
270
Ming Lei746058f2012-10-09 12:01:03 +0800271/* direct firmware loading support */
272static const char *fw_path[] = {
273 "/lib/firmware/updates/" UTS_RELEASE,
274 "/lib/firmware/updates",
275 "/lib/firmware/" UTS_RELEASE,
276 "/lib/firmware"
277};
278
279/* Don't inline this: 'struct kstat' is biggish */
280static noinline long fw_file_size(struct file *file)
281{
282 struct kstat st;
283 if (vfs_getattr(file->f_path.mnt, file->f_path.dentry, &st))
284 return -1;
285 if (!S_ISREG(st.mode))
286 return -1;
287 if (st.size != (long)st.size)
288 return -1;
289 return st.size;
290}
291
292static bool fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf)
293{
294 long size;
295 char *buf;
296
297 size = fw_file_size(file);
298 if (size < 0)
299 return false;
300 buf = vmalloc(size);
301 if (!buf)
302 return false;
303 if (kernel_read(file, 0, buf, size) != size) {
304 vfree(buf);
305 return false;
306 }
307 fw_buf->data = buf;
308 fw_buf->size = size;
309 return true;
310}
311
312static bool fw_get_filesystem_firmware(struct firmware_buf *buf)
313{
314 int i;
315 bool success = false;
316 char *path = __getname();
317
318 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
319 struct file *file;
320 snprintf(path, PATH_MAX, "%s/%s", fw_path[i], buf->fw_id);
321
322 file = filp_open(path, O_RDONLY, 0);
323 if (IS_ERR(file))
324 continue;
325 success = fw_read_file_contents(file, buf);
326 fput(file);
327 if (success)
328 break;
329 }
330 __putname(path);
331 return success;
332}
333
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700334static struct firmware_priv *to_firmware_priv(struct device *dev)
335{
336 return container_of(dev, struct firmware_priv, dev);
337}
338
339static void fw_load_abort(struct firmware_priv *fw_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Ming Lei12446912012-08-04 12:01:20 +0800341 struct firmware_buf *buf = fw_priv->buf;
342
343 set_bit(FW_STATUS_ABORT, &buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800344 complete_all(&buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700347static ssize_t firmware_timeout_show(struct class *class,
348 struct class_attribute *attr,
349 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 return sprintf(buf, "%d\n", loading_timeout);
352}
353
354/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800355 * firmware_timeout_store - set number of seconds to wait for firmware
356 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800357 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800358 * @buf: buffer to scan for timeout value
359 * @count: number of bytes in @buf
360 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800362 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 * firmware will be provided.
364 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800365 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700367static ssize_t firmware_timeout_store(struct class *class,
368 struct class_attribute *attr,
369 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700372 if (loading_timeout < 0)
373 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return count;
376}
377
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800378static struct class_attribute firmware_class_attrs[] = {
379 __ATTR(timeout, S_IWUSR | S_IRUGO,
380 firmware_timeout_show, firmware_timeout_store),
381 __ATTR_NULL
382};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800384static void fw_dev_release(struct device *dev)
385{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700386 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800387
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800388 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800389
390 module_put(THIS_MODULE);
391}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Kay Sievers7eff2e72007-08-14 15:15:12 +0200393static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700395 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Ming Lei12446912012-08-04 12:01:20 +0800397 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200399 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700400 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200401 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
402 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 return 0;
405}
406
Adrian Bunk1b81d662006-05-20 15:00:16 -0700407static struct class firmware_class = {
408 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800409 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700410 .dev_uevent = firmware_uevent,
411 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700412};
413
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700414static ssize_t firmware_loading_show(struct device *dev,
415 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700417 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800418 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return sprintf(buf, "%d\n", loading);
421}
422
Ming Lei65710cb2012-08-04 12:01:16 +0800423/* firmware holds the ownership of pages */
David Woodhousedd336c52010-05-02 11:21:21 +0300424static void firmware_free_data(const struct firmware *fw)
425{
Linus Torvaldsabb139e2012-10-03 15:58:32 -0700426 /* Loaded directly? */
427 if (!fw->priv) {
428 vfree(fw->data);
429 return;
430 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800431 fw_free_buf(fw->priv);
David Woodhousedd336c52010-05-02 11:21:21 +0300432}
433
David Woodhouse6e03a202009-04-09 22:04:07 -0700434/* Some architectures don't have PAGE_KERNEL_RO */
435#ifndef PAGE_KERNEL_RO
436#define PAGE_KERNEL_RO PAGE_KERNEL
437#endif
Ming Lei253c9242012-10-09 12:01:02 +0800438
439/* one pages buffer should be mapped/unmapped only once */
440static int fw_map_pages_buf(struct firmware_buf *buf)
441{
Ming Lei746058f2012-10-09 12:01:03 +0800442 if (buf->fmt != PAGE_BUF)
443 return 0;
444
Ming Lei253c9242012-10-09 12:01:02 +0800445 if (buf->data)
446 vunmap(buf->data);
447 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
448 if (!buf->data)
449 return -ENOMEM;
450 return 0;
451}
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800454 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700455 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800456 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800457 * @buf: buffer to scan for loading control value
458 * @count: number of bytes in @buf
459 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 * The relevant values are:
461 *
462 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800463 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 * -1: Conclude the load with an error and discard any written data.
465 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700466static ssize_t firmware_loading_store(struct device *dev,
467 struct device_attribute *attr,
468 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700470 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800471 struct firmware_buf *fw_buf = fw_priv->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700473 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Neil Hormaneea915b2012-01-02 15:31:23 -0500475 mutex_lock(&fw_lock);
476
Ming Lei12446912012-08-04 12:01:20 +0800477 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500478 goto out;
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 switch (loading) {
481 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800482 /* discarding any previous partial load */
Ming Lei12446912012-08-04 12:01:20 +0800483 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
484 for (i = 0; i < fw_buf->nr_pages; i++)
485 __free_page(fw_buf->pages[i]);
486 kfree(fw_buf->pages);
487 fw_buf->pages = NULL;
488 fw_buf->page_array_size = 0;
489 fw_buf->nr_pages = 0;
490 set_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei28eefa72012-08-04 12:01:17 +0800491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 break;
493 case 0:
Ming Lei12446912012-08-04 12:01:20 +0800494 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
495 set_bit(FW_STATUS_DONE, &fw_buf->status);
496 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei253c9242012-10-09 12:01:02 +0800497
498 /*
499 * Several loading requests may be pending on
500 * one same firmware buf, so let all requests
501 * see the mapped 'buf->data' once the loading
502 * is completed.
503 * */
504 fw_map_pages_buf(fw_buf);
Ming Lei1f2b7952012-08-04 12:01:21 +0800505 complete_all(&fw_buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 break;
507 }
508 /* fallthrough */
509 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700510 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 /* fallthrough */
512 case -1:
513 fw_load_abort(fw_priv);
514 break;
515 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500516out:
517 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return count;
519}
520
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700521static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700523static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
524 struct bin_attribute *bin_attr,
525 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200527 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700528 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800529 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700530 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Laura Garciacad1e552006-05-23 23:22:38 +0200532 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800533 buf = fw_priv->buf;
534 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 ret_count = -ENODEV;
536 goto out;
537 }
Ming Lei12446912012-08-04 12:01:20 +0800538 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200539 ret_count = 0;
540 goto out;
541 }
Ming Lei12446912012-08-04 12:01:20 +0800542 if (count > buf->size - offset)
543 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700544
545 ret_count = count;
546
547 while (count) {
548 void *page_data;
549 int page_nr = offset >> PAGE_SHIFT;
550 int page_ofs = offset & (PAGE_SIZE-1);
551 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
552
Ming Lei12446912012-08-04 12:01:20 +0800553 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700554
555 memcpy(buffer, page_data + page_ofs, page_cnt);
556
Ming Lei12446912012-08-04 12:01:20 +0800557 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700558 buffer += page_cnt;
559 offset += page_cnt;
560 count -= page_cnt;
561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562out:
Laura Garciacad1e552006-05-23 23:22:38 +0200563 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return ret_count;
565}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800566
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700567static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Ming Lei12446912012-08-04 12:01:20 +0800569 struct firmware_buf *buf = fw_priv->buf;
David Woodhouse6e03a202009-04-09 22:04:07 -0700570 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
David Woodhouse6e03a202009-04-09 22:04:07 -0700572 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800573 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700574 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800575 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700576 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
David Woodhouse6e03a202009-04-09 22:04:07 -0700578 new_pages = kmalloc(new_array_size * sizeof(void *),
579 GFP_KERNEL);
580 if (!new_pages) {
581 fw_load_abort(fw_priv);
582 return -ENOMEM;
583 }
Ming Lei12446912012-08-04 12:01:20 +0800584 memcpy(new_pages, buf->pages,
585 buf->page_array_size * sizeof(void *));
586 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
587 (new_array_size - buf->page_array_size));
588 kfree(buf->pages);
589 buf->pages = new_pages;
590 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700592
Ming Lei12446912012-08-04 12:01:20 +0800593 while (buf->nr_pages < pages_needed) {
594 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700595 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
596
Ming Lei12446912012-08-04 12:01:20 +0800597 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700598 fw_load_abort(fw_priv);
599 return -ENOMEM;
600 }
Ming Lei12446912012-08-04 12:01:20 +0800601 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return 0;
604}
605
606/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800607 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700608 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700609 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700610 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800611 * @buffer: buffer being written
612 * @offset: buffer offset for write in total data store area
613 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800615 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 * the driver as a firmware image.
617 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700618static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
619 struct bin_attribute *bin_attr,
620 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200622 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700623 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800624 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 ssize_t retval;
626
627 if (!capable(CAP_SYS_RAWIO))
628 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700629
Laura Garciacad1e552006-05-23 23:22:38 +0200630 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800631 buf = fw_priv->buf;
632 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 retval = -ENODEV;
634 goto out;
635 }
Ming Lei65710cb2012-08-04 12:01:16 +0800636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 retval = fw_realloc_buffer(fw_priv, offset + count);
638 if (retval)
639 goto out;
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700642
643 while (count) {
644 void *page_data;
645 int page_nr = offset >> PAGE_SHIFT;
646 int page_ofs = offset & (PAGE_SIZE - 1);
647 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
648
Ming Lei12446912012-08-04 12:01:20 +0800649 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700650
651 memcpy(page_data + page_ofs, buffer, page_cnt);
652
Ming Lei12446912012-08-04 12:01:20 +0800653 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700654 buffer += page_cnt;
655 offset += page_cnt;
656 count -= page_cnt;
657 }
658
Ming Lei12446912012-08-04 12:01:20 +0800659 buf->size = max_t(size_t, offset, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660out:
Laura Garciacad1e552006-05-23 23:22:38 +0200661 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return retval;
663}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800664
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700665static struct bin_attribute firmware_attr_data = {
666 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 .size = 0,
668 .read = firmware_data_read,
669 .write = firmware_data_write,
670};
671
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700672static void firmware_class_timeout(u_long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
674 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 fw_load_abort(fw_priv);
677}
678
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700679static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200680fw_create_instance(struct firmware *firmware, const char *fw_name,
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700681 struct device *device, bool uevent, bool nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700683 struct firmware_priv *fw_priv;
684 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Ming Lei12446912012-08-04 12:01:20 +0800686 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700687 if (!fw_priv) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700688 dev_err(device, "%s: kmalloc failed\n", __func__);
Ming Lei12446912012-08-04 12:01:20 +0800689 fw_priv = ERR_PTR(-ENOMEM);
690 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700693 fw_priv->nowait = nowait;
Ming Lei1f2b7952012-08-04 12:01:21 +0800694 fw_priv->fw = firmware;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700695 setup_timer(&fw_priv->timeout,
696 firmware_class_timeout, (u_long) fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700698 f_dev = &fw_priv->dev;
699
700 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800701 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700702 f_dev->parent = device;
703 f_dev->class = &firmware_class;
Ming Lei12446912012-08-04 12:01:20 +0800704exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700705 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
Ming Lei1f2b7952012-08-04 12:01:21 +0800708/* store the pages buffer info firmware from buf */
709static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
710{
711 fw->priv = buf;
712 fw->pages = buf->pages;
713 fw->size = buf->size;
714 fw->data = buf->data;
715
716 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
717 __func__, buf->fw_id, buf, buf->data,
718 (unsigned int)buf->size);
719}
720
Ming Leicfe016b2012-09-08 17:32:30 +0800721#ifdef CONFIG_PM_SLEEP
Ming Leif531f05a2012-08-04 12:01:25 +0800722static void fw_name_devm_release(struct device *dev, void *res)
723{
724 struct fw_name_devm *fwn = res;
725
726 if (fwn->magic == (unsigned long)&fw_cache)
727 pr_debug("%s: fw_name-%s devm-%p released\n",
728 __func__, fwn->name, res);
729}
730
731static int fw_devm_match(struct device *dev, void *res,
732 void *match_data)
733{
734 struct fw_name_devm *fwn = res;
735
736 return (fwn->magic == (unsigned long)&fw_cache) &&
737 !strcmp(fwn->name, match_data);
738}
739
740static struct fw_name_devm *fw_find_devm_name(struct device *dev,
741 const char *name)
742{
743 struct fw_name_devm *fwn;
744
745 fwn = devres_find(dev, fw_name_devm_release,
746 fw_devm_match, (void *)name);
747 return fwn;
748}
749
750/* add firmware name into devres list */
751static int fw_add_devm_name(struct device *dev, const char *name)
752{
753 struct fw_name_devm *fwn;
754
755 fwn = fw_find_devm_name(dev, name);
756 if (fwn)
757 return 1;
758
759 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
760 strlen(name) + 1, GFP_KERNEL);
761 if (!fwn)
762 return -ENOMEM;
763
764 fwn->magic = (unsigned long)&fw_cache;
765 strcpy(fwn->name, name);
766 devres_add(dev, fwn);
767
768 return 0;
769}
Ming Leicfe016b2012-09-08 17:32:30 +0800770#else
771static int fw_add_devm_name(struct device *dev, const char *name)
772{
773 return 0;
774}
775#endif
Ming Leif531f05a2012-08-04 12:01:25 +0800776
Ming Lei1f2b7952012-08-04 12:01:21 +0800777static void _request_firmware_cleanup(const struct firmware **firmware_p)
778{
779 release_firmware(*firmware_p);
780 *firmware_p = NULL;
781}
782
Stephen Boyddddb5542012-03-28 23:30:43 +0200783static struct firmware_priv *
784_request_firmware_prepare(const struct firmware **firmware_p, const char *name,
785 struct device *device, bool uevent, bool nowait)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700786{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +0800788 struct firmware_priv *fw_priv = NULL;
789 struct firmware_buf *buf;
790 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 if (!firmware_p)
Stephen Boyddddb5542012-03-28 23:30:43 +0200793 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Jiri Slaby4aed0642005-09-13 01:25:01 -0700795 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700797 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
798 __func__);
Stephen Boyddddb5542012-03-28 23:30:43 +0200799 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800802 if (fw_get_builtin_firmware(firmware, name)) {
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100803 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
Stephen Boyddddb5542012-03-28 23:30:43 +0200804 return NULL;
David Woodhouse5658c762008-05-23 13:52:42 +0100805 }
806
Ming Lei1f2b7952012-08-04 12:01:21 +0800807 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
808 if (!ret)
809 fw_priv = fw_create_instance(firmware, name, device,
810 uevent, nowait);
811
812 if (IS_ERR(fw_priv) || ret < 0) {
813 kfree(firmware);
Stephen Boyddddb5542012-03-28 23:30:43 +0200814 *firmware_p = NULL;
Ming Lei1f2b7952012-08-04 12:01:21 +0800815 return ERR_PTR(-ENOMEM);
816 } else if (fw_priv) {
817 fw_priv->buf = buf;
818
819 /*
820 * bind with 'buf' now to avoid warning in failure path
821 * of requesting firmware.
822 */
823 firmware->priv = buf;
824 return fw_priv;
Stephen Boyddddb5542012-03-28 23:30:43 +0200825 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800826
827 /* share the cached buf, which is inprogessing or completed */
828 check_status:
829 mutex_lock(&fw_lock);
830 if (test_bit(FW_STATUS_ABORT, &buf->status)) {
831 fw_priv = ERR_PTR(-ENOENT);
Ming Leief40bb12012-08-20 19:04:15 +0800832 firmware->priv = buf;
Ming Lei1f2b7952012-08-04 12:01:21 +0800833 _request_firmware_cleanup(firmware_p);
834 goto exit;
835 } else if (test_bit(FW_STATUS_DONE, &buf->status)) {
836 fw_priv = NULL;
837 fw_set_page_data(buf, firmware);
838 goto exit;
839 }
840 mutex_unlock(&fw_lock);
841 wait_for_completion(&buf->completion);
842 goto check_status;
843
844exit:
845 mutex_unlock(&fw_lock);
Stephen Boyddddb5542012-03-28 23:30:43 +0200846 return fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200847}
848
Stephen Boyddddb5542012-03-28 23:30:43 +0200849static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
850 long timeout)
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200851{
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200852 int retval = 0;
Stephen Boyddddb5542012-03-28 23:30:43 +0200853 struct device *f_dev = &fw_priv->dev;
Ming Lei12446912012-08-04 12:01:20 +0800854 struct firmware_buf *buf = fw_priv->buf;
Ming Leiac39b3e2012-08-20 19:04:16 +0800855 struct firmware_cache *fwc = &fw_cache;
Ming Lei746058f2012-10-09 12:01:03 +0800856 int direct_load = 0;
857
858 /* try direct loading from fs first */
859 if (fw_get_filesystem_firmware(buf)) {
860 dev_dbg(f_dev->parent, "firmware: direct-loading"
861 " firmware %s\n", buf->fw_id);
862
863 set_bit(FW_STATUS_DONE, &buf->status);
864 complete_all(&buf->completion);
865 direct_load = 1;
866 goto handle_fw;
867 }
868
869 /* fall back on userspace loading */
870 buf->fmt = PAGE_BUF;
Linus Torvaldscaca9512011-08-24 15:55:30 -0700871
Stephen Boyddddb5542012-03-28 23:30:43 +0200872 dev_set_uevent_suppress(f_dev, true);
David Woodhouse5658c762008-05-23 13:52:42 +0100873
Stephen Boyddddb5542012-03-28 23:30:43 +0200874 /* Need to pin this module until class device is destroyed */
875 __module_get(THIS_MODULE);
876
877 retval = device_add(f_dev);
878 if (retval) {
879 dev_err(f_dev, "%s: device_register failed\n", __func__);
880 goto err_put_dev;
881 }
882
883 retval = device_create_bin_file(f_dev, &firmware_attr_data);
884 if (retval) {
885 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
886 goto err_del_dev;
887 }
888
889 retval = device_create_file(f_dev, &dev_attr_loading);
890 if (retval) {
891 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
892 goto err_del_bin_attr;
893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Kay Sievers312c0042005-11-16 09:00:00 +0100895 if (uevent) {
Stephen Boyddddb5542012-03-28 23:30:43 +0200896 dev_set_uevent_suppress(f_dev, false);
Ming Lei12446912012-08-04 12:01:20 +0800897 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200898 if (timeout != MAX_SCHEDULE_TIMEOUT)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700899 mod_timer(&fw_priv->timeout,
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200900 round_jiffies_up(jiffies + timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700902 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
903 }
904
Ming Lei12446912012-08-04 12:01:20 +0800905 wait_for_completion(&buf->completion);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700906
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700907 del_timer_sync(&fw_priv->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Ming Lei746058f2012-10-09 12:01:03 +0800909handle_fw:
Laura Garciacad1e552006-05-23 23:22:38 +0200910 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800911 if (!buf->size || test_bit(FW_STATUS_ABORT, &buf->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 retval = -ENOENT;
Ming Lei65710cb2012-08-04 12:01:16 +0800913
Ming Leif531f05a2012-08-04 12:01:25 +0800914 /*
915 * add firmware name into devres list so that we can auto cache
916 * and uncache firmware for device.
917 *
918 * f_dev->parent may has been deleted already, but the problem
919 * should be fixed in devres or driver core.
920 */
921 if (!retval && f_dev->parent)
922 fw_add_devm_name(f_dev->parent, buf->fw_id);
923
Ming Leiac39b3e2012-08-20 19:04:16 +0800924 /*
925 * After caching firmware image is started, let it piggyback
926 * on request firmware.
927 */
928 if (!retval && fwc->state == FW_LOADER_START_CACHE) {
929 if (fw_cache_piggyback_on_request(buf->fw_id))
930 kref_get(&buf->ref);
931 }
932
Ming Lei1f2b7952012-08-04 12:01:21 +0800933 /* pass the pages buffer to driver at the last minute */
934 fw_set_page_data(buf, fw_priv->fw);
935
Ming Lei12446912012-08-04 12:01:20 +0800936 fw_priv->buf = NULL;
Laura Garciacad1e552006-05-23 23:22:38 +0200937 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Ming Lei746058f2012-10-09 12:01:03 +0800939 if (direct_load)
940 goto err_put_dev;
941
Stephen Boyddddb5542012-03-28 23:30:43 +0200942 device_remove_file(f_dev, &dev_attr_loading);
943err_del_bin_attr:
944 device_remove_bin_file(f_dev, &firmware_attr_data);
945err_del_dev:
946 device_del(f_dev);
947err_put_dev:
948 put_device(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return retval;
950}
951
952/**
Kay Sievers312c0042005-11-16 09:00:00 +0100953 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800954 * @firmware_p: pointer to firmware image
955 * @name: name of firmware file
956 * @device: device for which firmware is being loaded
957 *
958 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700959 * of @name for device @device.
960 *
961 * Should be called from user context where sleeping is allowed.
962 *
Kay Sievers312c0042005-11-16 09:00:00 +0100963 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700964 * should be distinctive enough not to be confused with any other
965 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +0800966 *
967 * Caller must hold the reference count of @device.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700968 **/
969int
970request_firmware(const struct firmware **firmware_p, const char *name,
971 struct device *device)
972{
Stephen Boyddddb5542012-03-28 23:30:43 +0200973 struct firmware_priv *fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200974 int ret;
975
Stephen Boyddddb5542012-03-28 23:30:43 +0200976 fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
977 false);
978 if (IS_ERR_OR_NULL(fw_priv))
979 return PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200980
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200981 ret = usermodehelper_read_trylock();
982 if (WARN_ON(ret)) {
983 dev_err(device, "firmware: %s will not be loaded\n", name);
984 } else {
Stephen Boyddddb5542012-03-28 23:30:43 +0200985 ret = _request_firmware_load(fw_priv, true,
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200986 firmware_loading_timeout());
987 usermodehelper_read_unlock();
988 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200989 if (ret)
990 _request_firmware_cleanup(firmware_p);
991
992 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700993}
994
995/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800997 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800999void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
1001 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001002 if (!fw_is_builtin_firmware(fw))
1003 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 kfree(fw);
1005 }
1006}
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008/* Async support */
1009struct firmware_work {
1010 struct work_struct work;
1011 struct module *module;
1012 const char *name;
1013 struct device *device;
1014 void *context;
1015 void (*cont)(const struct firmware *fw, void *context);
Bob Liu072fc8f2011-01-26 18:33:32 +08001016 bool uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017};
1018
Stephen Boyda36cf842012-03-28 23:31:00 +02001019static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
Stephen Boyda36cf842012-03-28 23:31:00 +02001021 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 const struct firmware *fw;
Stephen Boyddddb5542012-03-28 23:30:43 +02001023 struct firmware_priv *fw_priv;
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +02001024 long timeout;
matthieu castet113fab12005-11-13 16:07:39 -08001025 int ret;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001026
Stephen Boyda36cf842012-03-28 23:31:00 +02001027 fw_work = container_of(work, struct firmware_work, work);
Stephen Boyddddb5542012-03-28 23:30:43 +02001028 fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
1029 fw_work->uevent, true);
1030 if (IS_ERR_OR_NULL(fw_priv)) {
1031 ret = PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001032 goto out;
Stephen Boyddddb5542012-03-28 23:30:43 +02001033 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001034
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +02001035 timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
1036 if (timeout) {
Stephen Boyddddb5542012-03-28 23:30:43 +02001037 ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +02001038 usermodehelper_read_unlock();
1039 } else {
1040 dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
1041 fw_work->name);
1042 ret = -EAGAIN;
1043 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001044 if (ret)
1045 _request_firmware_cleanup(&fw);
1046
1047 out:
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001048 fw_work->cont(fw, fw_work->context);
Ming Lei0cfc1e12012-08-04 12:01:23 +08001049 put_device(fw_work->device);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 module_put(fw_work->module);
1052 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053}
1054
1055/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001056 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001057 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001058 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001059 * is non-zero else the firmware copy must be done manually.
1060 * @name: name of firmware file
1061 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001062 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001063 * @context: will be passed over to @cont, and
1064 * @fw may be %NULL if firmware request fails.
1065 * @cont: function will be called asynchronously when the firmware
1066 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001068 * Caller must hold the reference count of @device.
1069 *
Ming Lei6f21a622012-08-04 12:01:24 +08001070 * Asynchronous variant of request_firmware() for user contexts:
1071 * - sleep for as small periods as possible since it may
1072 * increase kernel boot time of built-in device drivers
1073 * requesting firmware in their ->probe() methods, if
1074 * @gfp is GFP_KERNEL.
1075 *
1076 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 **/
1078int
1079request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001080 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001081 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 void (*cont)(const struct firmware *fw, void *context))
1083{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001084 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001086 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (!fw_work)
1088 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001089
1090 fw_work->module = module;
1091 fw_work->name = name;
1092 fw_work->device = device;
1093 fw_work->context = context;
1094 fw_work->cont = cont;
1095 fw_work->uevent = uevent;
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (!try_module_get(module)) {
1098 kfree(fw_work);
1099 return -EFAULT;
1100 }
1101
Ming Lei0cfc1e12012-08-04 12:01:23 +08001102 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001103 INIT_WORK(&fw_work->work, request_firmware_work_func);
1104 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return 0;
1106}
1107
Ming Lei2887b392012-08-04 12:01:22 +08001108/**
1109 * cache_firmware - cache one firmware image in kernel memory space
1110 * @fw_name: the firmware image name
1111 *
1112 * Cache firmware in kernel memory so that drivers can use it when
1113 * system isn't ready for them to request firmware image from userspace.
1114 * Once it returns successfully, driver can use request_firmware or its
1115 * nowait version to get the cached firmware without any interacting
1116 * with userspace
1117 *
1118 * Return 0 if the firmware image has been cached successfully
1119 * Return !0 otherwise
1120 *
1121 */
1122int cache_firmware(const char *fw_name)
1123{
1124 int ret;
1125 const struct firmware *fw;
1126
1127 pr_debug("%s: %s\n", __func__, fw_name);
1128
1129 ret = request_firmware(&fw, fw_name, NULL);
1130 if (!ret)
1131 kfree(fw);
1132
1133 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1134
1135 return ret;
1136}
1137
1138/**
1139 * uncache_firmware - remove one cached firmware image
1140 * @fw_name: the firmware image name
1141 *
1142 * Uncache one firmware image which has been cached successfully
1143 * before.
1144 *
1145 * Return 0 if the firmware cache has been removed successfully
1146 * Return !0 otherwise
1147 *
1148 */
1149int uncache_firmware(const char *fw_name)
1150{
1151 struct firmware_buf *buf;
1152 struct firmware fw;
1153
1154 pr_debug("%s: %s\n", __func__, fw_name);
1155
1156 if (fw_get_builtin_firmware(&fw, fw_name))
1157 return 0;
1158
1159 buf = fw_lookup_buf(fw_name);
1160 if (buf) {
1161 fw_free_buf(buf);
1162 return 0;
1163 }
1164
1165 return -EINVAL;
1166}
1167
Ming Leicfe016b2012-09-08 17:32:30 +08001168#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +08001169static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1170{
1171 struct fw_cache_entry *fce;
1172
1173 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1174 if (!fce)
1175 goto exit;
1176
1177 strcpy(fce->name, name);
1178exit:
1179 return fce;
1180}
1181
Ming Lei373304f2012-10-09 12:01:01 +08001182static int __fw_entry_found(const char *name)
1183{
1184 struct firmware_cache *fwc = &fw_cache;
1185 struct fw_cache_entry *fce;
1186
1187 list_for_each_entry(fce, &fwc->fw_names, list) {
1188 if (!strcmp(fce->name, name))
1189 return 1;
1190 }
1191 return 0;
1192}
1193
Ming Leiac39b3e2012-08-20 19:04:16 +08001194static int fw_cache_piggyback_on_request(const char *name)
1195{
1196 struct firmware_cache *fwc = &fw_cache;
1197 struct fw_cache_entry *fce;
1198 int ret = 0;
1199
1200 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001201 if (__fw_entry_found(name))
1202 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001203
1204 fce = alloc_fw_cache_entry(name);
1205 if (fce) {
1206 ret = 1;
1207 list_add(&fce->list, &fwc->fw_names);
1208 pr_debug("%s: fw: %s\n", __func__, name);
1209 }
1210found:
1211 spin_unlock(&fwc->name_lock);
1212 return ret;
1213}
1214
Ming Lei37276a52012-08-04 12:01:27 +08001215static void free_fw_cache_entry(struct fw_cache_entry *fce)
1216{
1217 kfree(fce);
1218}
1219
1220static void __async_dev_cache_fw_image(void *fw_entry,
1221 async_cookie_t cookie)
1222{
1223 struct fw_cache_entry *fce = fw_entry;
1224 struct firmware_cache *fwc = &fw_cache;
1225 int ret;
1226
1227 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001228 if (ret) {
1229 spin_lock(&fwc->name_lock);
1230 list_del(&fce->list);
1231 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001232
Ming Leiac39b3e2012-08-20 19:04:16 +08001233 free_fw_cache_entry(fce);
1234 }
Ming Lei37276a52012-08-04 12:01:27 +08001235
Ming Lei37276a52012-08-04 12:01:27 +08001236 spin_lock(&fwc->name_lock);
1237 fwc->cnt--;
1238 spin_unlock(&fwc->name_lock);
1239
1240 wake_up(&fwc->wait_queue);
1241}
1242
1243/* called with dev->devres_lock held */
1244static void dev_create_fw_entry(struct device *dev, void *res,
1245 void *data)
1246{
1247 struct fw_name_devm *fwn = res;
1248 const char *fw_name = fwn->name;
1249 struct list_head *head = data;
1250 struct fw_cache_entry *fce;
1251
1252 fce = alloc_fw_cache_entry(fw_name);
1253 if (fce)
1254 list_add(&fce->list, head);
1255}
1256
1257static int devm_name_match(struct device *dev, void *res,
1258 void *match_data)
1259{
1260 struct fw_name_devm *fwn = res;
1261 return (fwn->magic == (unsigned long)match_data);
1262}
1263
Ming Leiab6dd8e2012-08-17 22:07:00 +08001264static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001265{
1266 LIST_HEAD(todo);
1267 struct fw_cache_entry *fce;
1268 struct fw_cache_entry *fce_next;
1269 struct firmware_cache *fwc = &fw_cache;
1270
1271 devres_for_each_res(dev, fw_name_devm_release,
1272 devm_name_match, &fw_cache,
1273 dev_create_fw_entry, &todo);
1274
1275 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1276 list_del(&fce->list);
1277
1278 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001279 /* only one cache entry for one firmware */
1280 if (!__fw_entry_found(fce->name)) {
1281 fwc->cnt++;
1282 list_add(&fce->list, &fwc->fw_names);
1283 } else {
1284 free_fw_cache_entry(fce);
1285 fce = NULL;
1286 }
Ming Lei37276a52012-08-04 12:01:27 +08001287 spin_unlock(&fwc->name_lock);
1288
Ming Lei373304f2012-10-09 12:01:01 +08001289 if (fce)
1290 async_schedule(__async_dev_cache_fw_image,
1291 (void *)fce);
Ming Lei37276a52012-08-04 12:01:27 +08001292 }
1293}
1294
1295static void __device_uncache_fw_images(void)
1296{
1297 struct firmware_cache *fwc = &fw_cache;
1298 struct fw_cache_entry *fce;
1299
1300 spin_lock(&fwc->name_lock);
1301 while (!list_empty(&fwc->fw_names)) {
1302 fce = list_entry(fwc->fw_names.next,
1303 struct fw_cache_entry, list);
1304 list_del(&fce->list);
1305 spin_unlock(&fwc->name_lock);
1306
1307 uncache_firmware(fce->name);
1308 free_fw_cache_entry(fce);
1309
1310 spin_lock(&fwc->name_lock);
1311 }
1312 spin_unlock(&fwc->name_lock);
1313}
1314
1315/**
1316 * device_cache_fw_images - cache devices' firmware
1317 *
1318 * If one device called request_firmware or its nowait version
1319 * successfully before, the firmware names are recored into the
1320 * device's devres link list, so device_cache_fw_images can call
1321 * cache_firmware() to cache these firmwares for the device,
1322 * then the device driver can load its firmwares easily at
1323 * time when system is not ready to complete loading firmware.
1324 */
1325static void device_cache_fw_images(void)
1326{
1327 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001328 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001329 DEFINE_WAIT(wait);
1330
1331 pr_debug("%s\n", __func__);
1332
Ming Lei373304f2012-10-09 12:01:01 +08001333 /* cancel uncache work */
1334 cancel_delayed_work_sync(&fwc->work);
1335
Ming Leiffe53f62012-08-04 12:01:28 +08001336 /*
1337 * use small loading timeout for caching devices' firmware
1338 * because all these firmware images have been loaded
1339 * successfully at lease once, also system is ready for
1340 * completing firmware loading now. The maximum size of
1341 * firmware in current distributions is about 2M bytes,
1342 * so 10 secs should be enough.
1343 */
1344 old_timeout = loading_timeout;
1345 loading_timeout = 10;
1346
Ming Leiac39b3e2012-08-20 19:04:16 +08001347 mutex_lock(&fw_lock);
1348 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001349 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001350 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001351
1352 /* wait for completion of caching firmware for all devices */
1353 spin_lock(&fwc->name_lock);
1354 for (;;) {
1355 prepare_to_wait(&fwc->wait_queue, &wait,
1356 TASK_UNINTERRUPTIBLE);
1357 if (!fwc->cnt)
1358 break;
1359
1360 spin_unlock(&fwc->name_lock);
1361
1362 schedule();
1363
1364 spin_lock(&fwc->name_lock);
1365 }
1366 spin_unlock(&fwc->name_lock);
1367 finish_wait(&fwc->wait_queue, &wait);
Ming Leiffe53f62012-08-04 12:01:28 +08001368
1369 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001370}
1371
1372/**
1373 * device_uncache_fw_images - uncache devices' firmware
1374 *
1375 * uncache all firmwares which have been cached successfully
1376 * by device_uncache_fw_images earlier
1377 */
1378static void device_uncache_fw_images(void)
1379{
1380 pr_debug("%s\n", __func__);
1381 __device_uncache_fw_images();
1382}
1383
1384static void device_uncache_fw_images_work(struct work_struct *work)
1385{
1386 device_uncache_fw_images();
1387}
1388
1389/**
1390 * device_uncache_fw_images_delay - uncache devices firmwares
1391 * @delay: number of milliseconds to delay uncache device firmwares
1392 *
1393 * uncache all devices's firmwares which has been cached successfully
1394 * by device_cache_fw_images after @delay milliseconds.
1395 */
1396static void device_uncache_fw_images_delay(unsigned long delay)
1397{
1398 schedule_delayed_work(&fw_cache.work,
1399 msecs_to_jiffies(delay));
1400}
1401
Ming Lei07646d92012-08-04 12:01:29 +08001402static int fw_pm_notify(struct notifier_block *notify_block,
1403 unsigned long mode, void *unused)
1404{
1405 switch (mode) {
1406 case PM_HIBERNATION_PREPARE:
1407 case PM_SUSPEND_PREPARE:
1408 device_cache_fw_images();
1409 break;
1410
1411 case PM_POST_SUSPEND:
1412 case PM_POST_HIBERNATION:
1413 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001414 /*
1415 * In case that system sleep failed and syscore_suspend is
1416 * not called.
1417 */
1418 mutex_lock(&fw_lock);
1419 fw_cache.state = FW_LOADER_NO_CACHE;
1420 mutex_unlock(&fw_lock);
1421
Ming Lei07646d92012-08-04 12:01:29 +08001422 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1423 break;
1424 }
1425
1426 return 0;
1427}
Ming Lei07646d92012-08-04 12:01:29 +08001428
Ming Leiac39b3e2012-08-20 19:04:16 +08001429/* stop caching firmware once syscore_suspend is reached */
1430static int fw_suspend(void)
1431{
1432 fw_cache.state = FW_LOADER_NO_CACHE;
1433 return 0;
1434}
1435
1436static struct syscore_ops fw_syscore_ops = {
1437 .suspend = fw_suspend,
1438};
Ming Leicfe016b2012-09-08 17:32:30 +08001439#else
1440static int fw_cache_piggyback_on_request(const char *name)
1441{
1442 return 0;
1443}
1444#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001445
Ming Lei37276a52012-08-04 12:01:27 +08001446static void __init fw_cache_init(void)
1447{
1448 spin_lock_init(&fw_cache.lock);
1449 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001450 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001451
Ming Leicfe016b2012-09-08 17:32:30 +08001452#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +08001453 spin_lock_init(&fw_cache.name_lock);
1454 INIT_LIST_HEAD(&fw_cache.fw_names);
1455 fw_cache.cnt = 0;
1456
1457 init_waitqueue_head(&fw_cache.wait_queue);
1458 INIT_DELAYED_WORK(&fw_cache.work,
1459 device_uncache_fw_images_work);
Ming Lei07646d92012-08-04 12:01:29 +08001460
1461 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1462 register_pm_notifier(&fw_cache.pm_notify);
Ming Leiac39b3e2012-08-20 19:04:16 +08001463
1464 register_syscore_ops(&fw_syscore_ops);
Ming Leicfe016b2012-09-08 17:32:30 +08001465#endif
Ming Lei37276a52012-08-04 12:01:27 +08001466}
1467
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001468static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
Ming Lei1f2b7952012-08-04 12:01:21 +08001470 fw_cache_init();
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001471 return class_register(&firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001473
1474static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
Ming Leicfe016b2012-09-08 17:32:30 +08001476#ifdef CONFIG_PM_SLEEP
Ming Leiac39b3e2012-08-20 19:04:16 +08001477 unregister_syscore_ops(&fw_syscore_ops);
Ming Lei07646d92012-08-04 12:01:29 +08001478 unregister_pm_notifier(&fw_cache.pm_notify);
Ming Leicfe016b2012-09-08 17:32:30 +08001479#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 class_unregister(&firmware_class);
1481}
1482
Shaohua Lia30a6a22006-09-27 01:50:52 -07001483fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484module_exit(firmware_class_exit);
1485
1486EXPORT_SYMBOL(release_firmware);
1487EXPORT_SYMBOL(request_firmware);
1488EXPORT_SYMBOL(request_firmware_nowait);
Ming Lei2887b392012-08-04 12:01:22 +08001489EXPORT_SYMBOL_GPL(cache_firmware);
1490EXPORT_SYMBOL_GPL(uncache_firmware);