blob: d06a8d0534bfcfaab828d5ca16a298181fce282d [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
Linus Torvaldsabb139e2012-10-03 15:58:32 -070039static const char *fw_path[] = {
40 "/lib/firmware/updates/" UTS_RELEASE,
41 "/lib/firmware/updates",
42 "/lib/firmware/" UTS_RELEASE,
43 "/lib/firmware"
44};
45
46/* Don't inline this: 'struct kstat' is biggish */
47static noinline long fw_file_size(struct file *file)
48{
49 struct kstat st;
50 if (vfs_getattr(file->f_path.mnt, file->f_path.dentry, &st))
51 return -1;
52 if (!S_ISREG(st.mode))
53 return -1;
54 if (st.size != (long)st.size)
55 return -1;
56 return st.size;
57}
58
59static bool fw_read_file_contents(struct file *file, struct firmware *fw)
60{
Linus Torvaldsabb139e2012-10-03 15:58:32 -070061 long size;
62 char *buf;
63
64 size = fw_file_size(file);
65 if (size < 0)
66 return false;
67 buf = vmalloc(size);
68 if (!buf)
69 return false;
Linus Torvaldsce57e982012-10-04 09:19:02 -070070 if (kernel_read(file, 0, buf, size) != size) {
Linus Torvaldsabb139e2012-10-03 15:58:32 -070071 vfree(buf);
72 return false;
73 }
74 fw->data = buf;
75 fw->size = size;
76 return true;
77}
78
79static bool fw_get_filesystem_firmware(struct firmware *fw, const char *name)
80{
81 int i;
82 bool success = false;
83 char *path = __getname();
84
85 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
86 struct file *file;
87 snprintf(path, PATH_MAX, "%s/%s", fw_path[i], name);
88
89 file = filp_open(path, O_RDONLY, 0);
90 if (IS_ERR(file))
91 continue;
92 success = fw_read_file_contents(file, fw);
93 fput(file);
94 if (success)
95 break;
96 }
97 __putname(path);
98 return success;
99}
100
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800101/* Builtin firmware support */
102
103#ifdef CONFIG_FW_LOADER
104
105extern struct builtin_fw __start_builtin_fw[];
106extern struct builtin_fw __end_builtin_fw[];
107
108static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
109{
110 struct builtin_fw *b_fw;
111
112 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
113 if (strcmp(name, b_fw->name) == 0) {
114 fw->size = b_fw->size;
115 fw->data = b_fw->data;
116 return true;
117 }
118 }
119
120 return false;
121}
122
123static bool fw_is_builtin_firmware(const struct firmware *fw)
124{
125 struct builtin_fw *b_fw;
126
127 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
128 if (fw->data == b_fw->data)
129 return true;
130
131 return false;
132}
133
134#else /* Module case - no builtin firmware support */
135
136static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
137{
138 return false;
139}
140
141static inline bool fw_is_builtin_firmware(const struct firmware *fw)
142{
143 return false;
144}
145#endif
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147enum {
148 FW_STATUS_LOADING,
149 FW_STATUS_DONE,
150 FW_STATUS_ABORT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
Dave Jones2f651682007-01-25 15:56:15 -0500153static int loading_timeout = 60; /* In seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200155static inline long firmware_loading_timeout(void)
156{
157 return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
158}
159
Ming Lei1f2b7952012-08-04 12:01:21 +0800160struct firmware_cache {
161 /* firmware_buf instance will be added into the below list */
162 spinlock_t lock;
163 struct list_head head;
Ming Leicfe016b2012-09-08 17:32:30 +0800164 int state;
Ming Lei37276a52012-08-04 12:01:27 +0800165
Ming Leicfe016b2012-09-08 17:32:30 +0800166#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +0800167 /*
168 * Names of firmware images which have been cached successfully
169 * will be added into the below list so that device uncache
170 * helper can trace which firmware images have been cached
171 * before.
172 */
173 spinlock_t name_lock;
174 struct list_head fw_names;
175
176 wait_queue_head_t wait_queue;
177 int cnt;
178 struct delayed_work work;
Ming Lei07646d92012-08-04 12:01:29 +0800179
180 struct notifier_block pm_notify;
Ming Leicfe016b2012-09-08 17:32:30 +0800181#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800182};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Ming Lei12446912012-08-04 12:01:20 +0800184struct firmware_buf {
Ming Lei1f2b7952012-08-04 12:01:21 +0800185 struct kref ref;
186 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 struct completion completion;
Ming Lei1f2b7952012-08-04 12:01:21 +0800188 struct firmware_cache *fwc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 unsigned long status;
Ming Lei65710cb2012-08-04 12:01:16 +0800190 void *data;
191 size_t size;
David Woodhouse6e03a202009-04-09 22:04:07 -0700192 struct page **pages;
193 int nr_pages;
194 int page_array_size;
Dmitry Torokhove1771232010-03-13 23:49:23 -0800195 char fw_id[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196};
197
Ming Lei37276a52012-08-04 12:01:27 +0800198struct fw_cache_entry {
199 struct list_head list;
200 char name[];
201};
202
Ming Lei12446912012-08-04 12:01:20 +0800203struct firmware_priv {
204 struct timer_list timeout;
205 bool nowait;
206 struct device dev;
207 struct firmware_buf *buf;
Ming Lei1f2b7952012-08-04 12:01:21 +0800208 struct firmware *fw;
Ming Lei12446912012-08-04 12:01:20 +0800209};
210
Ming Leif531f05a2012-08-04 12:01:25 +0800211struct fw_name_devm {
212 unsigned long magic;
213 char name[];
214};
215
Ming Lei1f2b7952012-08-04 12:01:21 +0800216#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
217
Ming Leiac39b3e2012-08-20 19:04:16 +0800218#define FW_LOADER_NO_CACHE 0
219#define FW_LOADER_START_CACHE 1
220
221static int fw_cache_piggyback_on_request(const char *name);
222
Ming Lei1f2b7952012-08-04 12:01:21 +0800223/* fw_lock could be moved to 'struct firmware_priv' but since it is just
224 * guarding for corner cases a global lock should be OK */
225static DEFINE_MUTEX(fw_lock);
226
227static struct firmware_cache fw_cache;
228
229static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
230 struct firmware_cache *fwc)
231{
232 struct firmware_buf *buf;
233
234 buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
235
236 if (!buf)
237 return buf;
238
239 kref_init(&buf->ref);
240 strcpy(buf->fw_id, fw_name);
241 buf->fwc = fwc;
242 init_completion(&buf->completion);
243
244 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
245
246 return buf;
247}
248
Ming Lei2887b392012-08-04 12:01:22 +0800249static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
250{
251 struct firmware_buf *tmp;
252 struct firmware_cache *fwc = &fw_cache;
253
254 list_for_each_entry(tmp, &fwc->head, list)
255 if (!strcmp(tmp->fw_id, fw_name))
256 return tmp;
257 return NULL;
258}
259
Ming Lei1f2b7952012-08-04 12:01:21 +0800260static int fw_lookup_and_allocate_buf(const char *fw_name,
261 struct firmware_cache *fwc,
262 struct firmware_buf **buf)
263{
264 struct firmware_buf *tmp;
265
266 spin_lock(&fwc->lock);
Ming Lei2887b392012-08-04 12:01:22 +0800267 tmp = __fw_lookup_buf(fw_name);
268 if (tmp) {
269 kref_get(&tmp->ref);
270 spin_unlock(&fwc->lock);
271 *buf = tmp;
272 return 1;
273 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800274 tmp = __allocate_fw_buf(fw_name, fwc);
275 if (tmp)
276 list_add(&tmp->list, &fwc->head);
277 spin_unlock(&fwc->lock);
278
279 *buf = tmp;
280
281 return tmp ? 0 : -ENOMEM;
282}
283
Ming Lei2887b392012-08-04 12:01:22 +0800284static struct firmware_buf *fw_lookup_buf(const char *fw_name)
285{
286 struct firmware_buf *tmp;
287 struct firmware_cache *fwc = &fw_cache;
288
289 spin_lock(&fwc->lock);
290 tmp = __fw_lookup_buf(fw_name);
291 spin_unlock(&fwc->lock);
292
293 return tmp;
294}
295
Ming Lei1f2b7952012-08-04 12:01:21 +0800296static void __fw_free_buf(struct kref *ref)
297{
298 struct firmware_buf *buf = to_fwbuf(ref);
299 struct firmware_cache *fwc = buf->fwc;
300 int i;
301
302 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
303 __func__, buf->fw_id, buf, buf->data,
304 (unsigned int)buf->size);
305
306 spin_lock(&fwc->lock);
307 list_del(&buf->list);
308 spin_unlock(&fwc->lock);
309
310 vunmap(buf->data);
311 for (i = 0; i < buf->nr_pages; i++)
312 __free_page(buf->pages[i]);
313 kfree(buf->pages);
314 kfree(buf);
315}
316
317static void fw_free_buf(struct firmware_buf *buf)
318{
319 kref_put(&buf->ref, __fw_free_buf);
320}
321
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700322static struct firmware_priv *to_firmware_priv(struct device *dev)
323{
324 return container_of(dev, struct firmware_priv, dev);
325}
326
327static void fw_load_abort(struct firmware_priv *fw_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Ming Lei12446912012-08-04 12:01:20 +0800329 struct firmware_buf *buf = fw_priv->buf;
330
331 set_bit(FW_STATUS_ABORT, &buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800332 complete_all(&buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700335static ssize_t firmware_timeout_show(struct class *class,
336 struct class_attribute *attr,
337 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 return sprintf(buf, "%d\n", loading_timeout);
340}
341
342/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800343 * firmware_timeout_store - set number of seconds to wait for firmware
344 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800345 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800346 * @buf: buffer to scan for timeout value
347 * @count: number of bytes in @buf
348 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800350 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 * firmware will be provided.
352 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800353 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700355static ssize_t firmware_timeout_store(struct class *class,
356 struct class_attribute *attr,
357 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700360 if (loading_timeout < 0)
361 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return count;
364}
365
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800366static struct class_attribute firmware_class_attrs[] = {
367 __ATTR(timeout, S_IWUSR | S_IRUGO,
368 firmware_timeout_show, firmware_timeout_store),
369 __ATTR_NULL
370};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800372static void fw_dev_release(struct device *dev)
373{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700374 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800375
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800376 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800377
378 module_put(THIS_MODULE);
379}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Kay Sievers7eff2e72007-08-14 15:15:12 +0200381static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700383 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Ming Lei12446912012-08-04 12:01:20 +0800385 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200387 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700388 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200389 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
390 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 return 0;
393}
394
Adrian Bunk1b81d662006-05-20 15:00:16 -0700395static struct class firmware_class = {
396 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800397 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700398 .dev_uevent = firmware_uevent,
399 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700400};
401
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700402static ssize_t firmware_loading_show(struct device *dev,
403 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700405 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800406 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return sprintf(buf, "%d\n", loading);
409}
410
Ming Lei65710cb2012-08-04 12:01:16 +0800411/* firmware holds the ownership of pages */
David Woodhousedd336c52010-05-02 11:21:21 +0300412static void firmware_free_data(const struct firmware *fw)
413{
Linus Torvaldsabb139e2012-10-03 15:58:32 -0700414 /* Loaded directly? */
415 if (!fw->priv) {
416 vfree(fw->data);
417 return;
418 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800419 fw_free_buf(fw->priv);
David Woodhousedd336c52010-05-02 11:21:21 +0300420}
421
David Woodhouse6e03a202009-04-09 22:04:07 -0700422/* Some architectures don't have PAGE_KERNEL_RO */
423#ifndef PAGE_KERNEL_RO
424#define PAGE_KERNEL_RO PAGE_KERNEL
425#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800427 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700428 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800429 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800430 * @buf: buffer to scan for loading control value
431 * @count: number of bytes in @buf
432 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 * The relevant values are:
434 *
435 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800436 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 * -1: Conclude the load with an error and discard any written data.
438 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700439static ssize_t firmware_loading_store(struct device *dev,
440 struct device_attribute *attr,
441 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700443 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800444 struct firmware_buf *fw_buf = fw_priv->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700446 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Neil Hormaneea915b2012-01-02 15:31:23 -0500448 mutex_lock(&fw_lock);
449
Ming Lei12446912012-08-04 12:01:20 +0800450 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500451 goto out;
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 switch (loading) {
454 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800455 /* discarding any previous partial load */
Ming Lei12446912012-08-04 12:01:20 +0800456 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
457 for (i = 0; i < fw_buf->nr_pages; i++)
458 __free_page(fw_buf->pages[i]);
459 kfree(fw_buf->pages);
460 fw_buf->pages = NULL;
461 fw_buf->page_array_size = 0;
462 fw_buf->nr_pages = 0;
463 set_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei28eefa72012-08-04 12:01:17 +0800464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 break;
466 case 0:
Ming Lei12446912012-08-04 12:01:20 +0800467 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
468 set_bit(FW_STATUS_DONE, &fw_buf->status);
469 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800470 complete_all(&fw_buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 break;
472 }
473 /* fallthrough */
474 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700475 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 /* fallthrough */
477 case -1:
478 fw_load_abort(fw_priv);
479 break;
480 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500481out:
482 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return count;
484}
485
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700486static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700488static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
489 struct bin_attribute *bin_attr,
490 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200492 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700493 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800494 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700495 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Laura Garciacad1e552006-05-23 23:22:38 +0200497 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800498 buf = fw_priv->buf;
499 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 ret_count = -ENODEV;
501 goto out;
502 }
Ming Lei12446912012-08-04 12:01:20 +0800503 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200504 ret_count = 0;
505 goto out;
506 }
Ming Lei12446912012-08-04 12:01:20 +0800507 if (count > buf->size - offset)
508 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700509
510 ret_count = count;
511
512 while (count) {
513 void *page_data;
514 int page_nr = offset >> PAGE_SHIFT;
515 int page_ofs = offset & (PAGE_SIZE-1);
516 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
517
Ming Lei12446912012-08-04 12:01:20 +0800518 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700519
520 memcpy(buffer, page_data + page_ofs, page_cnt);
521
Ming Lei12446912012-08-04 12:01:20 +0800522 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700523 buffer += page_cnt;
524 offset += page_cnt;
525 count -= page_cnt;
526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527out:
Laura Garciacad1e552006-05-23 23:22:38 +0200528 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return ret_count;
530}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800531
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700532static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Ming Lei12446912012-08-04 12:01:20 +0800534 struct firmware_buf *buf = fw_priv->buf;
David Woodhouse6e03a202009-04-09 22:04:07 -0700535 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
David Woodhouse6e03a202009-04-09 22:04:07 -0700537 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800538 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700539 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800540 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700541 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
David Woodhouse6e03a202009-04-09 22:04:07 -0700543 new_pages = kmalloc(new_array_size * sizeof(void *),
544 GFP_KERNEL);
545 if (!new_pages) {
546 fw_load_abort(fw_priv);
547 return -ENOMEM;
548 }
Ming Lei12446912012-08-04 12:01:20 +0800549 memcpy(new_pages, buf->pages,
550 buf->page_array_size * sizeof(void *));
551 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
552 (new_array_size - buf->page_array_size));
553 kfree(buf->pages);
554 buf->pages = new_pages;
555 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700557
Ming Lei12446912012-08-04 12:01:20 +0800558 while (buf->nr_pages < pages_needed) {
559 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700560 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
561
Ming Lei12446912012-08-04 12:01:20 +0800562 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700563 fw_load_abort(fw_priv);
564 return -ENOMEM;
565 }
Ming Lei12446912012-08-04 12:01:20 +0800566 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return 0;
569}
570
571/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800572 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700573 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700574 * @kobj: kobject for the device
Randy Dunlap42e61f4a2007-07-23 21:42:11 -0700575 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800576 * @buffer: buffer being written
577 * @offset: buffer offset for write in total data store area
578 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800580 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 * the driver as a firmware image.
582 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700583static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
584 struct bin_attribute *bin_attr,
585 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200587 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700588 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800589 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 ssize_t retval;
591
592 if (!capable(CAP_SYS_RAWIO))
593 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700594
Laura Garciacad1e552006-05-23 23:22:38 +0200595 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800596 buf = fw_priv->buf;
597 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 retval = -ENODEV;
599 goto out;
600 }
Ming Lei65710cb2012-08-04 12:01:16 +0800601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 retval = fw_realloc_buffer(fw_priv, offset + count);
603 if (retval)
604 goto out;
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700607
608 while (count) {
609 void *page_data;
610 int page_nr = offset >> PAGE_SHIFT;
611 int page_ofs = offset & (PAGE_SIZE - 1);
612 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
613
Ming Lei12446912012-08-04 12:01:20 +0800614 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700615
616 memcpy(page_data + page_ofs, buffer, page_cnt);
617
Ming Lei12446912012-08-04 12:01:20 +0800618 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700619 buffer += page_cnt;
620 offset += page_cnt;
621 count -= page_cnt;
622 }
623
Ming Lei12446912012-08-04 12:01:20 +0800624 buf->size = max_t(size_t, offset, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625out:
Laura Garciacad1e552006-05-23 23:22:38 +0200626 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return retval;
628}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800629
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700630static struct bin_attribute firmware_attr_data = {
631 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 .size = 0,
633 .read = firmware_data_read,
634 .write = firmware_data_write,
635};
636
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700637static void firmware_class_timeout(u_long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 fw_load_abort(fw_priv);
642}
643
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700644static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200645fw_create_instance(struct firmware *firmware, const char *fw_name,
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700646 struct device *device, bool uevent, bool nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700648 struct firmware_priv *fw_priv;
649 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Ming Lei12446912012-08-04 12:01:20 +0800651 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700652 if (!fw_priv) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700653 dev_err(device, "%s: kmalloc failed\n", __func__);
Ming Lei12446912012-08-04 12:01:20 +0800654 fw_priv = ERR_PTR(-ENOMEM);
655 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700658 fw_priv->nowait = nowait;
Ming Lei1f2b7952012-08-04 12:01:21 +0800659 fw_priv->fw = firmware;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700660 setup_timer(&fw_priv->timeout,
661 firmware_class_timeout, (u_long) fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700663 f_dev = &fw_priv->dev;
664
665 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800666 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700667 f_dev->parent = device;
668 f_dev->class = &firmware_class;
Ming Lei12446912012-08-04 12:01:20 +0800669exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700670 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
Ming Lei1f2b7952012-08-04 12:01:21 +0800673/* one pages buffer is mapped/unmapped only once */
674static int fw_map_pages_buf(struct firmware_buf *buf)
675{
676 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
677 if (!buf->data)
678 return -ENOMEM;
679 return 0;
680}
681
682/* store the pages buffer info firmware from buf */
683static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
684{
685 fw->priv = buf;
686 fw->pages = buf->pages;
687 fw->size = buf->size;
688 fw->data = buf->data;
689
690 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
691 __func__, buf->fw_id, buf, buf->data,
692 (unsigned int)buf->size);
693}
694
Ming Leicfe016b2012-09-08 17:32:30 +0800695#ifdef CONFIG_PM_SLEEP
Ming Leif531f05a2012-08-04 12:01:25 +0800696static void fw_name_devm_release(struct device *dev, void *res)
697{
698 struct fw_name_devm *fwn = res;
699
700 if (fwn->magic == (unsigned long)&fw_cache)
701 pr_debug("%s: fw_name-%s devm-%p released\n",
702 __func__, fwn->name, res);
703}
704
705static int fw_devm_match(struct device *dev, void *res,
706 void *match_data)
707{
708 struct fw_name_devm *fwn = res;
709
710 return (fwn->magic == (unsigned long)&fw_cache) &&
711 !strcmp(fwn->name, match_data);
712}
713
714static struct fw_name_devm *fw_find_devm_name(struct device *dev,
715 const char *name)
716{
717 struct fw_name_devm *fwn;
718
719 fwn = devres_find(dev, fw_name_devm_release,
720 fw_devm_match, (void *)name);
721 return fwn;
722}
723
724/* add firmware name into devres list */
725static int fw_add_devm_name(struct device *dev, const char *name)
726{
727 struct fw_name_devm *fwn;
728
729 fwn = fw_find_devm_name(dev, name);
730 if (fwn)
731 return 1;
732
733 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
734 strlen(name) + 1, GFP_KERNEL);
735 if (!fwn)
736 return -ENOMEM;
737
738 fwn->magic = (unsigned long)&fw_cache;
739 strcpy(fwn->name, name);
740 devres_add(dev, fwn);
741
742 return 0;
743}
Ming Leicfe016b2012-09-08 17:32:30 +0800744#else
745static int fw_add_devm_name(struct device *dev, const char *name)
746{
747 return 0;
748}
749#endif
Ming Leif531f05a2012-08-04 12:01:25 +0800750
Ming Lei1f2b7952012-08-04 12:01:21 +0800751static void _request_firmware_cleanup(const struct firmware **firmware_p)
752{
753 release_firmware(*firmware_p);
754 *firmware_p = NULL;
755}
756
Stephen Boyddddb5542012-03-28 23:30:43 +0200757static struct firmware_priv *
758_request_firmware_prepare(const struct firmware **firmware_p, const char *name,
759 struct device *device, bool uevent, bool nowait)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700760{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +0800762 struct firmware_priv *fw_priv = NULL;
763 struct firmware_buf *buf;
764 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 if (!firmware_p)
Stephen Boyddddb5542012-03-28 23:30:43 +0200767 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Jiri Slaby4aed0642005-09-13 01:25:01 -0700769 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700771 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
772 __func__);
Stephen Boyddddb5542012-03-28 23:30:43 +0200773 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800776 if (fw_get_builtin_firmware(firmware, name)) {
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100777 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
Stephen Boyddddb5542012-03-28 23:30:43 +0200778 return NULL;
David Woodhouse5658c762008-05-23 13:52:42 +0100779 }
780
Linus Torvaldsabb139e2012-10-03 15:58:32 -0700781 if (fw_get_filesystem_firmware(firmware, name)) {
782 dev_dbg(device, "firmware: direct-loading firmware %s\n", name);
783 return NULL;
784 }
785
Ming Lei1f2b7952012-08-04 12:01:21 +0800786 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
787 if (!ret)
788 fw_priv = fw_create_instance(firmware, name, device,
789 uevent, nowait);
790
791 if (IS_ERR(fw_priv) || ret < 0) {
792 kfree(firmware);
Stephen Boyddddb5542012-03-28 23:30:43 +0200793 *firmware_p = NULL;
Ming Lei1f2b7952012-08-04 12:01:21 +0800794 return ERR_PTR(-ENOMEM);
795 } else if (fw_priv) {
796 fw_priv->buf = buf;
797
798 /*
799 * bind with 'buf' now to avoid warning in failure path
800 * of requesting firmware.
801 */
802 firmware->priv = buf;
803 return fw_priv;
Stephen Boyddddb5542012-03-28 23:30:43 +0200804 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800805
806 /* share the cached buf, which is inprogessing or completed */
807 check_status:
808 mutex_lock(&fw_lock);
809 if (test_bit(FW_STATUS_ABORT, &buf->status)) {
810 fw_priv = ERR_PTR(-ENOENT);
Ming Leief40bb12012-08-20 19:04:15 +0800811 firmware->priv = buf;
Ming Lei1f2b7952012-08-04 12:01:21 +0800812 _request_firmware_cleanup(firmware_p);
813 goto exit;
814 } else if (test_bit(FW_STATUS_DONE, &buf->status)) {
815 fw_priv = NULL;
816 fw_set_page_data(buf, firmware);
817 goto exit;
818 }
819 mutex_unlock(&fw_lock);
820 wait_for_completion(&buf->completion);
821 goto check_status;
822
823exit:
824 mutex_unlock(&fw_lock);
Stephen Boyddddb5542012-03-28 23:30:43 +0200825 return fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200826}
827
Stephen Boyddddb5542012-03-28 23:30:43 +0200828static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
829 long timeout)
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200830{
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200831 int retval = 0;
Stephen Boyddddb5542012-03-28 23:30:43 +0200832 struct device *f_dev = &fw_priv->dev;
Ming Lei12446912012-08-04 12:01:20 +0800833 struct firmware_buf *buf = fw_priv->buf;
Ming Leiac39b3e2012-08-20 19:04:16 +0800834 struct firmware_cache *fwc = &fw_cache;
Linus Torvaldscaca9512011-08-24 15:55:30 -0700835
Stephen Boyddddb5542012-03-28 23:30:43 +0200836 dev_set_uevent_suppress(f_dev, true);
David Woodhouse5658c762008-05-23 13:52:42 +0100837
Stephen Boyddddb5542012-03-28 23:30:43 +0200838 /* Need to pin this module until class device is destroyed */
839 __module_get(THIS_MODULE);
840
841 retval = device_add(f_dev);
842 if (retval) {
843 dev_err(f_dev, "%s: device_register failed\n", __func__);
844 goto err_put_dev;
845 }
846
847 retval = device_create_bin_file(f_dev, &firmware_attr_data);
848 if (retval) {
849 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
850 goto err_del_dev;
851 }
852
853 retval = device_create_file(f_dev, &dev_attr_loading);
854 if (retval) {
855 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
856 goto err_del_bin_attr;
857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Kay Sievers312c0042005-11-16 09:00:00 +0100859 if (uevent) {
Stephen Boyddddb5542012-03-28 23:30:43 +0200860 dev_set_uevent_suppress(f_dev, false);
Ming Lei12446912012-08-04 12:01:20 +0800861 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200862 if (timeout != MAX_SCHEDULE_TIMEOUT)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700863 mod_timer(&fw_priv->timeout,
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200864 round_jiffies_up(jiffies + timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700866 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
867 }
868
Ming Lei12446912012-08-04 12:01:20 +0800869 wait_for_completion(&buf->completion);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700870
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700871 del_timer_sync(&fw_priv->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Laura Garciacad1e552006-05-23 23:22:38 +0200873 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800874 if (!buf->size || test_bit(FW_STATUS_ABORT, &buf->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 retval = -ENOENT;
Ming Lei65710cb2012-08-04 12:01:16 +0800876
Ming Leif531f05a2012-08-04 12:01:25 +0800877 /*
878 * add firmware name into devres list so that we can auto cache
879 * and uncache firmware for device.
880 *
881 * f_dev->parent may has been deleted already, but the problem
882 * should be fixed in devres or driver core.
883 */
884 if (!retval && f_dev->parent)
885 fw_add_devm_name(f_dev->parent, buf->fw_id);
886
Ming Lei65710cb2012-08-04 12:01:16 +0800887 if (!retval)
Ming Lei1f2b7952012-08-04 12:01:21 +0800888 retval = fw_map_pages_buf(buf);
Ming Lei12446912012-08-04 12:01:20 +0800889
Ming Leiac39b3e2012-08-20 19:04:16 +0800890 /*
891 * After caching firmware image is started, let it piggyback
892 * on request firmware.
893 */
894 if (!retval && fwc->state == FW_LOADER_START_CACHE) {
895 if (fw_cache_piggyback_on_request(buf->fw_id))
896 kref_get(&buf->ref);
897 }
898
Ming Lei1f2b7952012-08-04 12:01:21 +0800899 /* pass the pages buffer to driver at the last minute */
900 fw_set_page_data(buf, fw_priv->fw);
901
Ming Lei12446912012-08-04 12:01:20 +0800902 fw_priv->buf = NULL;
Laura Garciacad1e552006-05-23 23:22:38 +0200903 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Stephen Boyddddb5542012-03-28 23:30:43 +0200905 device_remove_file(f_dev, &dev_attr_loading);
906err_del_bin_attr:
907 device_remove_bin_file(f_dev, &firmware_attr_data);
908err_del_dev:
909 device_del(f_dev);
910err_put_dev:
911 put_device(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return retval;
913}
914
915/**
Kay Sievers312c0042005-11-16 09:00:00 +0100916 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800917 * @firmware_p: pointer to firmware image
918 * @name: name of firmware file
919 * @device: device for which firmware is being loaded
920 *
921 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700922 * of @name for device @device.
923 *
924 * Should be called from user context where sleeping is allowed.
925 *
Kay Sievers312c0042005-11-16 09:00:00 +0100926 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700927 * should be distinctive enough not to be confused with any other
928 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +0800929 *
930 * Caller must hold the reference count of @device.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700931 **/
932int
933request_firmware(const struct firmware **firmware_p, const char *name,
934 struct device *device)
935{
Stephen Boyddddb5542012-03-28 23:30:43 +0200936 struct firmware_priv *fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200937 int ret;
938
Stephen Boyddddb5542012-03-28 23:30:43 +0200939 fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
940 false);
941 if (IS_ERR_OR_NULL(fw_priv))
942 return PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200943
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200944 ret = usermodehelper_read_trylock();
945 if (WARN_ON(ret)) {
946 dev_err(device, "firmware: %s will not be loaded\n", name);
947 } else {
Stephen Boyddddb5542012-03-28 23:30:43 +0200948 ret = _request_firmware_load(fw_priv, true,
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200949 firmware_loading_timeout());
950 usermodehelper_read_unlock();
951 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200952 if (ret)
953 _request_firmware_cleanup(firmware_p);
954
955 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700956}
957
958/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800960 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800962void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
964 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800965 if (!fw_is_builtin_firmware(fw))
966 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 kfree(fw);
968 }
969}
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971/* Async support */
972struct firmware_work {
973 struct work_struct work;
974 struct module *module;
975 const char *name;
976 struct device *device;
977 void *context;
978 void (*cont)(const struct firmware *fw, void *context);
Bob Liu072fc8f2011-01-26 18:33:32 +0800979 bool uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980};
981
Stephen Boyda36cf842012-03-28 23:31:00 +0200982static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Stephen Boyda36cf842012-03-28 23:31:00 +0200984 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 const struct firmware *fw;
Stephen Boyddddb5542012-03-28 23:30:43 +0200986 struct firmware_priv *fw_priv;
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200987 long timeout;
matthieu castet113fab12005-11-13 16:07:39 -0800988 int ret;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700989
Stephen Boyda36cf842012-03-28 23:31:00 +0200990 fw_work = container_of(work, struct firmware_work, work);
Stephen Boyddddb5542012-03-28 23:30:43 +0200991 fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
992 fw_work->uevent, true);
993 if (IS_ERR_OR_NULL(fw_priv)) {
994 ret = PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200995 goto out;
Stephen Boyddddb5542012-03-28 23:30:43 +0200996 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200997
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200998 timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
999 if (timeout) {
Stephen Boyddddb5542012-03-28 23:30:43 +02001000 ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +02001001 usermodehelper_read_unlock();
1002 } else {
1003 dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
1004 fw_work->name);
1005 ret = -EAGAIN;
1006 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001007 if (ret)
1008 _request_firmware_cleanup(&fw);
1009
1010 out:
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001011 fw_work->cont(fw, fw_work->context);
Ming Lei0cfc1e12012-08-04 12:01:23 +08001012 put_device(fw_work->device);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 module_put(fw_work->module);
1015 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
1017
1018/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001019 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001020 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001021 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001022 * is non-zero else the firmware copy must be done manually.
1023 * @name: name of firmware file
1024 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001025 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001026 * @context: will be passed over to @cont, and
1027 * @fw may be %NULL if firmware request fails.
1028 * @cont: function will be called asynchronously when the firmware
1029 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001031 * Caller must hold the reference count of @device.
1032 *
Ming Lei6f21a622012-08-04 12:01:24 +08001033 * Asynchronous variant of request_firmware() for user contexts:
1034 * - sleep for as small periods as possible since it may
1035 * increase kernel boot time of built-in device drivers
1036 * requesting firmware in their ->probe() methods, if
1037 * @gfp is GFP_KERNEL.
1038 *
1039 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 **/
1041int
1042request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001043 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001044 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 void (*cont)(const struct firmware *fw, void *context))
1046{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001047 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001049 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (!fw_work)
1051 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001052
1053 fw_work->module = module;
1054 fw_work->name = name;
1055 fw_work->device = device;
1056 fw_work->context = context;
1057 fw_work->cont = cont;
1058 fw_work->uevent = uevent;
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 if (!try_module_get(module)) {
1061 kfree(fw_work);
1062 return -EFAULT;
1063 }
1064
Ming Lei0cfc1e12012-08-04 12:01:23 +08001065 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001066 INIT_WORK(&fw_work->work, request_firmware_work_func);
1067 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return 0;
1069}
1070
Ming Lei2887b392012-08-04 12:01:22 +08001071/**
1072 * cache_firmware - cache one firmware image in kernel memory space
1073 * @fw_name: the firmware image name
1074 *
1075 * Cache firmware in kernel memory so that drivers can use it when
1076 * system isn't ready for them to request firmware image from userspace.
1077 * Once it returns successfully, driver can use request_firmware or its
1078 * nowait version to get the cached firmware without any interacting
1079 * with userspace
1080 *
1081 * Return 0 if the firmware image has been cached successfully
1082 * Return !0 otherwise
1083 *
1084 */
1085int cache_firmware(const char *fw_name)
1086{
1087 int ret;
1088 const struct firmware *fw;
1089
1090 pr_debug("%s: %s\n", __func__, fw_name);
1091
1092 ret = request_firmware(&fw, fw_name, NULL);
1093 if (!ret)
1094 kfree(fw);
1095
1096 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1097
1098 return ret;
1099}
1100
1101/**
1102 * uncache_firmware - remove one cached firmware image
1103 * @fw_name: the firmware image name
1104 *
1105 * Uncache one firmware image which has been cached successfully
1106 * before.
1107 *
1108 * Return 0 if the firmware cache has been removed successfully
1109 * Return !0 otherwise
1110 *
1111 */
1112int uncache_firmware(const char *fw_name)
1113{
1114 struct firmware_buf *buf;
1115 struct firmware fw;
1116
1117 pr_debug("%s: %s\n", __func__, fw_name);
1118
1119 if (fw_get_builtin_firmware(&fw, fw_name))
1120 return 0;
1121
1122 buf = fw_lookup_buf(fw_name);
1123 if (buf) {
1124 fw_free_buf(buf);
1125 return 0;
1126 }
1127
1128 return -EINVAL;
1129}
1130
Ming Leicfe016b2012-09-08 17:32:30 +08001131#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +08001132static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1133{
1134 struct fw_cache_entry *fce;
1135
1136 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1137 if (!fce)
1138 goto exit;
1139
1140 strcpy(fce->name, name);
1141exit:
1142 return fce;
1143}
1144
Ming Lei373304f2012-10-09 12:01:01 +08001145static int __fw_entry_found(const char *name)
1146{
1147 struct firmware_cache *fwc = &fw_cache;
1148 struct fw_cache_entry *fce;
1149
1150 list_for_each_entry(fce, &fwc->fw_names, list) {
1151 if (!strcmp(fce->name, name))
1152 return 1;
1153 }
1154 return 0;
1155}
1156
Ming Leiac39b3e2012-08-20 19:04:16 +08001157static int fw_cache_piggyback_on_request(const char *name)
1158{
1159 struct firmware_cache *fwc = &fw_cache;
1160 struct fw_cache_entry *fce;
1161 int ret = 0;
1162
1163 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001164 if (__fw_entry_found(name))
1165 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001166
1167 fce = alloc_fw_cache_entry(name);
1168 if (fce) {
1169 ret = 1;
1170 list_add(&fce->list, &fwc->fw_names);
1171 pr_debug("%s: fw: %s\n", __func__, name);
1172 }
1173found:
1174 spin_unlock(&fwc->name_lock);
1175 return ret;
1176}
1177
Ming Lei37276a52012-08-04 12:01:27 +08001178static void free_fw_cache_entry(struct fw_cache_entry *fce)
1179{
1180 kfree(fce);
1181}
1182
1183static void __async_dev_cache_fw_image(void *fw_entry,
1184 async_cookie_t cookie)
1185{
1186 struct fw_cache_entry *fce = fw_entry;
1187 struct firmware_cache *fwc = &fw_cache;
1188 int ret;
1189
1190 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001191 if (ret) {
1192 spin_lock(&fwc->name_lock);
1193 list_del(&fce->list);
1194 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001195
Ming Leiac39b3e2012-08-20 19:04:16 +08001196 free_fw_cache_entry(fce);
1197 }
Ming Lei37276a52012-08-04 12:01:27 +08001198
Ming Lei37276a52012-08-04 12:01:27 +08001199 spin_lock(&fwc->name_lock);
1200 fwc->cnt--;
1201 spin_unlock(&fwc->name_lock);
1202
1203 wake_up(&fwc->wait_queue);
1204}
1205
1206/* called with dev->devres_lock held */
1207static void dev_create_fw_entry(struct device *dev, void *res,
1208 void *data)
1209{
1210 struct fw_name_devm *fwn = res;
1211 const char *fw_name = fwn->name;
1212 struct list_head *head = data;
1213 struct fw_cache_entry *fce;
1214
1215 fce = alloc_fw_cache_entry(fw_name);
1216 if (fce)
1217 list_add(&fce->list, head);
1218}
1219
1220static int devm_name_match(struct device *dev, void *res,
1221 void *match_data)
1222{
1223 struct fw_name_devm *fwn = res;
1224 return (fwn->magic == (unsigned long)match_data);
1225}
1226
Ming Leiab6dd8e2012-08-17 22:07:00 +08001227static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001228{
1229 LIST_HEAD(todo);
1230 struct fw_cache_entry *fce;
1231 struct fw_cache_entry *fce_next;
1232 struct firmware_cache *fwc = &fw_cache;
1233
1234 devres_for_each_res(dev, fw_name_devm_release,
1235 devm_name_match, &fw_cache,
1236 dev_create_fw_entry, &todo);
1237
1238 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1239 list_del(&fce->list);
1240
1241 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001242 /* only one cache entry for one firmware */
1243 if (!__fw_entry_found(fce->name)) {
1244 fwc->cnt++;
1245 list_add(&fce->list, &fwc->fw_names);
1246 } else {
1247 free_fw_cache_entry(fce);
1248 fce = NULL;
1249 }
Ming Lei37276a52012-08-04 12:01:27 +08001250 spin_unlock(&fwc->name_lock);
1251
Ming Lei373304f2012-10-09 12:01:01 +08001252 if (fce)
1253 async_schedule(__async_dev_cache_fw_image,
1254 (void *)fce);
Ming Lei37276a52012-08-04 12:01:27 +08001255 }
1256}
1257
1258static void __device_uncache_fw_images(void)
1259{
1260 struct firmware_cache *fwc = &fw_cache;
1261 struct fw_cache_entry *fce;
1262
1263 spin_lock(&fwc->name_lock);
1264 while (!list_empty(&fwc->fw_names)) {
1265 fce = list_entry(fwc->fw_names.next,
1266 struct fw_cache_entry, list);
1267 list_del(&fce->list);
1268 spin_unlock(&fwc->name_lock);
1269
1270 uncache_firmware(fce->name);
1271 free_fw_cache_entry(fce);
1272
1273 spin_lock(&fwc->name_lock);
1274 }
1275 spin_unlock(&fwc->name_lock);
1276}
1277
1278/**
1279 * device_cache_fw_images - cache devices' firmware
1280 *
1281 * If one device called request_firmware or its nowait version
1282 * successfully before, the firmware names are recored into the
1283 * device's devres link list, so device_cache_fw_images can call
1284 * cache_firmware() to cache these firmwares for the device,
1285 * then the device driver can load its firmwares easily at
1286 * time when system is not ready to complete loading firmware.
1287 */
1288static void device_cache_fw_images(void)
1289{
1290 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001291 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001292 DEFINE_WAIT(wait);
1293
1294 pr_debug("%s\n", __func__);
1295
Ming Lei373304f2012-10-09 12:01:01 +08001296 /* cancel uncache work */
1297 cancel_delayed_work_sync(&fwc->work);
1298
Ming Leiffe53f62012-08-04 12:01:28 +08001299 /*
1300 * use small loading timeout for caching devices' firmware
1301 * because all these firmware images have been loaded
1302 * successfully at lease once, also system is ready for
1303 * completing firmware loading now. The maximum size of
1304 * firmware in current distributions is about 2M bytes,
1305 * so 10 secs should be enough.
1306 */
1307 old_timeout = loading_timeout;
1308 loading_timeout = 10;
1309
Ming Leiac39b3e2012-08-20 19:04:16 +08001310 mutex_lock(&fw_lock);
1311 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001312 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001313 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001314
1315 /* wait for completion of caching firmware for all devices */
1316 spin_lock(&fwc->name_lock);
1317 for (;;) {
1318 prepare_to_wait(&fwc->wait_queue, &wait,
1319 TASK_UNINTERRUPTIBLE);
1320 if (!fwc->cnt)
1321 break;
1322
1323 spin_unlock(&fwc->name_lock);
1324
1325 schedule();
1326
1327 spin_lock(&fwc->name_lock);
1328 }
1329 spin_unlock(&fwc->name_lock);
1330 finish_wait(&fwc->wait_queue, &wait);
Ming Leiffe53f62012-08-04 12:01:28 +08001331
1332 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001333}
1334
1335/**
1336 * device_uncache_fw_images - uncache devices' firmware
1337 *
1338 * uncache all firmwares which have been cached successfully
1339 * by device_uncache_fw_images earlier
1340 */
1341static void device_uncache_fw_images(void)
1342{
1343 pr_debug("%s\n", __func__);
1344 __device_uncache_fw_images();
1345}
1346
1347static void device_uncache_fw_images_work(struct work_struct *work)
1348{
1349 device_uncache_fw_images();
1350}
1351
1352/**
1353 * device_uncache_fw_images_delay - uncache devices firmwares
1354 * @delay: number of milliseconds to delay uncache device firmwares
1355 *
1356 * uncache all devices's firmwares which has been cached successfully
1357 * by device_cache_fw_images after @delay milliseconds.
1358 */
1359static void device_uncache_fw_images_delay(unsigned long delay)
1360{
1361 schedule_delayed_work(&fw_cache.work,
1362 msecs_to_jiffies(delay));
1363}
1364
Ming Lei07646d92012-08-04 12:01:29 +08001365static int fw_pm_notify(struct notifier_block *notify_block,
1366 unsigned long mode, void *unused)
1367{
1368 switch (mode) {
1369 case PM_HIBERNATION_PREPARE:
1370 case PM_SUSPEND_PREPARE:
1371 device_cache_fw_images();
1372 break;
1373
1374 case PM_POST_SUSPEND:
1375 case PM_POST_HIBERNATION:
1376 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001377 /*
1378 * In case that system sleep failed and syscore_suspend is
1379 * not called.
1380 */
1381 mutex_lock(&fw_lock);
1382 fw_cache.state = FW_LOADER_NO_CACHE;
1383 mutex_unlock(&fw_lock);
1384
Ming Lei07646d92012-08-04 12:01:29 +08001385 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1386 break;
1387 }
1388
1389 return 0;
1390}
Ming Lei07646d92012-08-04 12:01:29 +08001391
Ming Leiac39b3e2012-08-20 19:04:16 +08001392/* stop caching firmware once syscore_suspend is reached */
1393static int fw_suspend(void)
1394{
1395 fw_cache.state = FW_LOADER_NO_CACHE;
1396 return 0;
1397}
1398
1399static struct syscore_ops fw_syscore_ops = {
1400 .suspend = fw_suspend,
1401};
Ming Leicfe016b2012-09-08 17:32:30 +08001402#else
1403static int fw_cache_piggyback_on_request(const char *name)
1404{
1405 return 0;
1406}
1407#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001408
Ming Lei37276a52012-08-04 12:01:27 +08001409static void __init fw_cache_init(void)
1410{
1411 spin_lock_init(&fw_cache.lock);
1412 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001413 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001414
Ming Leicfe016b2012-09-08 17:32:30 +08001415#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +08001416 spin_lock_init(&fw_cache.name_lock);
1417 INIT_LIST_HEAD(&fw_cache.fw_names);
1418 fw_cache.cnt = 0;
1419
1420 init_waitqueue_head(&fw_cache.wait_queue);
1421 INIT_DELAYED_WORK(&fw_cache.work,
1422 device_uncache_fw_images_work);
Ming Lei07646d92012-08-04 12:01:29 +08001423
1424 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1425 register_pm_notifier(&fw_cache.pm_notify);
Ming Leiac39b3e2012-08-20 19:04:16 +08001426
1427 register_syscore_ops(&fw_syscore_ops);
Ming Leicfe016b2012-09-08 17:32:30 +08001428#endif
Ming Lei37276a52012-08-04 12:01:27 +08001429}
1430
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001431static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
Ming Lei1f2b7952012-08-04 12:01:21 +08001433 fw_cache_init();
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001434 return class_register(&firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001436
1437static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
Ming Leicfe016b2012-09-08 17:32:30 +08001439#ifdef CONFIG_PM_SLEEP
Ming Leiac39b3e2012-08-20 19:04:16 +08001440 unregister_syscore_ops(&fw_syscore_ops);
Ming Lei07646d92012-08-04 12:01:29 +08001441 unregister_pm_notifier(&fw_cache.pm_notify);
Ming Leicfe016b2012-09-08 17:32:30 +08001442#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 class_unregister(&firmware_class);
1444}
1445
Shaohua Lia30a6a22006-09-27 01:50:52 -07001446fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447module_exit(firmware_class_exit);
1448
1449EXPORT_SYMBOL(release_firmware);
1450EXPORT_SYMBOL(request_firmware);
1451EXPORT_SYMBOL(request_firmware_nowait);
Ming Lei2887b392012-08-04 12:01:22 +08001452EXPORT_SYMBOL_GPL(cache_firmware);
1453EXPORT_SYMBOL_GPL(uncache_firmware);