blob: ed0510a912c8ac459065f8e7cccf64b07c9973ed [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>
Ming Lei1f2b7952012-08-04 12:01:21 +080024#include <linux/list.h>
Ming Lei37276a52012-08-04 12:01:27 +080025#include <linux/async.h>
26#include <linux/pm.h>
Ming Lei07646d92012-08-04 12:01:29 +080027#include <linux/suspend.h>
Ming Lei37276a52012-08-04 12:01:27 +080028
29#include "base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Markus Rechberger87d37a42007-06-04 18:45:44 +020031MODULE_AUTHOR("Manuel Estrada Sainz");
Linus Torvalds1da177e2005-04-16 15:20:36 -070032MODULE_DESCRIPTION("Multi purpose firmware loading support");
33MODULE_LICENSE("GPL");
34
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080035/* Builtin firmware support */
36
37#ifdef CONFIG_FW_LOADER
38
39extern struct builtin_fw __start_builtin_fw[];
40extern struct builtin_fw __end_builtin_fw[];
41
42static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
43{
44 struct builtin_fw *b_fw;
45
46 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
47 if (strcmp(name, b_fw->name) == 0) {
48 fw->size = b_fw->size;
49 fw->data = b_fw->data;
50 return true;
51 }
52 }
53
54 return false;
55}
56
57static bool fw_is_builtin_firmware(const struct firmware *fw)
58{
59 struct builtin_fw *b_fw;
60
61 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
62 if (fw->data == b_fw->data)
63 return true;
64
65 return false;
66}
67
68#else /* Module case - no builtin firmware support */
69
70static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
71{
72 return false;
73}
74
75static inline bool fw_is_builtin_firmware(const struct firmware *fw)
76{
77 return false;
78}
79#endif
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081enum {
82 FW_STATUS_LOADING,
83 FW_STATUS_DONE,
84 FW_STATUS_ABORT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085};
86
Dave Jones2f651682007-01-25 15:56:15 -050087static int loading_timeout = 60; /* In seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +020089static inline long firmware_loading_timeout(void)
90{
91 return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
92}
93
Ming Lei1f2b7952012-08-04 12:01:21 +080094struct firmware_cache {
95 /* firmware_buf instance will be added into the below list */
96 spinlock_t lock;
97 struct list_head head;
Ming Lei37276a52012-08-04 12:01:27 +080098
99 /*
100 * Names of firmware images which have been cached successfully
101 * will be added into the below list so that device uncache
102 * helper can trace which firmware images have been cached
103 * before.
104 */
105 spinlock_t name_lock;
106 struct list_head fw_names;
107
108 wait_queue_head_t wait_queue;
109 int cnt;
110 struct delayed_work work;
Ming Lei07646d92012-08-04 12:01:29 +0800111
112 struct notifier_block pm_notify;
Ming Lei1f2b7952012-08-04 12:01:21 +0800113};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Ming Lei12446912012-08-04 12:01:20 +0800115struct firmware_buf {
Ming Lei1f2b7952012-08-04 12:01:21 +0800116 struct kref ref;
117 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 struct completion completion;
Ming Lei1f2b7952012-08-04 12:01:21 +0800119 struct firmware_cache *fwc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 unsigned long status;
Ming Lei65710cb2012-08-04 12:01:16 +0800121 void *data;
122 size_t size;
David Woodhouse6e03a202009-04-09 22:04:07 -0700123 struct page **pages;
124 int nr_pages;
125 int page_array_size;
Dmitry Torokhove1771232010-03-13 23:49:23 -0800126 char fw_id[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
Ming Lei37276a52012-08-04 12:01:27 +0800129struct fw_cache_entry {
130 struct list_head list;
131 char name[];
132};
133
Ming Lei12446912012-08-04 12:01:20 +0800134struct firmware_priv {
135 struct timer_list timeout;
136 bool nowait;
137 struct device dev;
138 struct firmware_buf *buf;
Ming Lei1f2b7952012-08-04 12:01:21 +0800139 struct firmware *fw;
Ming Lei12446912012-08-04 12:01:20 +0800140};
141
Ming Leif531f05a2012-08-04 12:01:25 +0800142struct fw_name_devm {
143 unsigned long magic;
144 char name[];
145};
146
Ming Lei1f2b7952012-08-04 12:01:21 +0800147#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
148
149/* fw_lock could be moved to 'struct firmware_priv' but since it is just
150 * guarding for corner cases a global lock should be OK */
151static DEFINE_MUTEX(fw_lock);
152
153static struct firmware_cache fw_cache;
154
155static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
156 struct firmware_cache *fwc)
157{
158 struct firmware_buf *buf;
159
160 buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
161
162 if (!buf)
163 return buf;
164
165 kref_init(&buf->ref);
166 strcpy(buf->fw_id, fw_name);
167 buf->fwc = fwc;
168 init_completion(&buf->completion);
169
170 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
171
172 return buf;
173}
174
Ming Lei2887b392012-08-04 12:01:22 +0800175static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
176{
177 struct firmware_buf *tmp;
178 struct firmware_cache *fwc = &fw_cache;
179
180 list_for_each_entry(tmp, &fwc->head, list)
181 if (!strcmp(tmp->fw_id, fw_name))
182 return tmp;
183 return NULL;
184}
185
Ming Lei1f2b7952012-08-04 12:01:21 +0800186static int fw_lookup_and_allocate_buf(const char *fw_name,
187 struct firmware_cache *fwc,
188 struct firmware_buf **buf)
189{
190 struct firmware_buf *tmp;
191
192 spin_lock(&fwc->lock);
Ming Lei2887b392012-08-04 12:01:22 +0800193 tmp = __fw_lookup_buf(fw_name);
194 if (tmp) {
195 kref_get(&tmp->ref);
196 spin_unlock(&fwc->lock);
197 *buf = tmp;
198 return 1;
199 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800200 tmp = __allocate_fw_buf(fw_name, fwc);
201 if (tmp)
202 list_add(&tmp->list, &fwc->head);
203 spin_unlock(&fwc->lock);
204
205 *buf = tmp;
206
207 return tmp ? 0 : -ENOMEM;
208}
209
Ming Lei2887b392012-08-04 12:01:22 +0800210static struct firmware_buf *fw_lookup_buf(const char *fw_name)
211{
212 struct firmware_buf *tmp;
213 struct firmware_cache *fwc = &fw_cache;
214
215 spin_lock(&fwc->lock);
216 tmp = __fw_lookup_buf(fw_name);
217 spin_unlock(&fwc->lock);
218
219 return tmp;
220}
221
Ming Lei1f2b7952012-08-04 12:01:21 +0800222static void __fw_free_buf(struct kref *ref)
223{
224 struct firmware_buf *buf = to_fwbuf(ref);
225 struct firmware_cache *fwc = buf->fwc;
226 int i;
227
228 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
229 __func__, buf->fw_id, buf, buf->data,
230 (unsigned int)buf->size);
231
232 spin_lock(&fwc->lock);
233 list_del(&buf->list);
234 spin_unlock(&fwc->lock);
235
236 vunmap(buf->data);
237 for (i = 0; i < buf->nr_pages; i++)
238 __free_page(buf->pages[i]);
239 kfree(buf->pages);
240 kfree(buf);
241}
242
243static void fw_free_buf(struct firmware_buf *buf)
244{
245 kref_put(&buf->ref, __fw_free_buf);
246}
247
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700248static struct firmware_priv *to_firmware_priv(struct device *dev)
249{
250 return container_of(dev, struct firmware_priv, dev);
251}
252
253static void fw_load_abort(struct firmware_priv *fw_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Ming Lei12446912012-08-04 12:01:20 +0800255 struct firmware_buf *buf = fw_priv->buf;
256
257 set_bit(FW_STATUS_ABORT, &buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800258 complete_all(&buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700261static ssize_t firmware_timeout_show(struct class *class,
262 struct class_attribute *attr,
263 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 return sprintf(buf, "%d\n", loading_timeout);
266}
267
268/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800269 * firmware_timeout_store - set number of seconds to wait for firmware
270 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800271 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800272 * @buf: buffer to scan for timeout value
273 * @count: number of bytes in @buf
274 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800276 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 * firmware will be provided.
278 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800279 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700281static ssize_t firmware_timeout_store(struct class *class,
282 struct class_attribute *attr,
283 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700286 if (loading_timeout < 0)
287 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return count;
290}
291
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800292static struct class_attribute firmware_class_attrs[] = {
293 __ATTR(timeout, S_IWUSR | S_IRUGO,
294 firmware_timeout_show, firmware_timeout_store),
295 __ATTR_NULL
296};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800298static void fw_dev_release(struct device *dev)
299{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700300 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800301
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800302 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800303
304 module_put(THIS_MODULE);
305}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Kay Sievers7eff2e72007-08-14 15:15:12 +0200307static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700309 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Ming Lei12446912012-08-04 12:01:20 +0800311 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200313 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700314 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200315 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
316 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 return 0;
319}
320
Adrian Bunk1b81d662006-05-20 15:00:16 -0700321static struct class firmware_class = {
322 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800323 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700324 .dev_uevent = firmware_uevent,
325 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700326};
327
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700328static ssize_t firmware_loading_show(struct device *dev,
329 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700331 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800332 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return sprintf(buf, "%d\n", loading);
335}
336
Ming Lei65710cb2012-08-04 12:01:16 +0800337/* firmware holds the ownership of pages */
David Woodhousedd336c52010-05-02 11:21:21 +0300338static void firmware_free_data(const struct firmware *fw)
339{
Ming Lei1f2b7952012-08-04 12:01:21 +0800340 WARN_ON(!fw->priv);
341 fw_free_buf(fw->priv);
David Woodhousedd336c52010-05-02 11:21:21 +0300342}
343
David Woodhouse6e03a202009-04-09 22:04:07 -0700344/* Some architectures don't have PAGE_KERNEL_RO */
345#ifndef PAGE_KERNEL_RO
346#define PAGE_KERNEL_RO PAGE_KERNEL
347#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800349 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700350 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800351 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800352 * @buf: buffer to scan for loading control value
353 * @count: number of bytes in @buf
354 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 * The relevant values are:
356 *
357 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800358 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 * -1: Conclude the load with an error and discard any written data.
360 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700361static ssize_t firmware_loading_store(struct device *dev,
362 struct device_attribute *attr,
363 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700365 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800366 struct firmware_buf *fw_buf = fw_priv->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700368 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Neil Hormaneea915b2012-01-02 15:31:23 -0500370 mutex_lock(&fw_lock);
371
Ming Lei12446912012-08-04 12:01:20 +0800372 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500373 goto out;
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 switch (loading) {
376 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800377 /* discarding any previous partial load */
Ming Lei12446912012-08-04 12:01:20 +0800378 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
379 for (i = 0; i < fw_buf->nr_pages; i++)
380 __free_page(fw_buf->pages[i]);
381 kfree(fw_buf->pages);
382 fw_buf->pages = NULL;
383 fw_buf->page_array_size = 0;
384 fw_buf->nr_pages = 0;
385 set_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei28eefa72012-08-04 12:01:17 +0800386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 break;
388 case 0:
Ming Lei12446912012-08-04 12:01:20 +0800389 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
390 set_bit(FW_STATUS_DONE, &fw_buf->status);
391 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800392 complete_all(&fw_buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 break;
394 }
395 /* fallthrough */
396 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700397 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /* fallthrough */
399 case -1:
400 fw_load_abort(fw_priv);
401 break;
402 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500403out:
404 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return count;
406}
407
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700408static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700410static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
411 struct bin_attribute *bin_attr,
412 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200414 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700415 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800416 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700417 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Laura Garciacad1e552006-05-23 23:22:38 +0200419 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800420 buf = fw_priv->buf;
421 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 ret_count = -ENODEV;
423 goto out;
424 }
Ming Lei12446912012-08-04 12:01:20 +0800425 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200426 ret_count = 0;
427 goto out;
428 }
Ming Lei12446912012-08-04 12:01:20 +0800429 if (count > buf->size - offset)
430 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700431
432 ret_count = count;
433
434 while (count) {
435 void *page_data;
436 int page_nr = offset >> PAGE_SHIFT;
437 int page_ofs = offset & (PAGE_SIZE-1);
438 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
439
Ming Lei12446912012-08-04 12:01:20 +0800440 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700441
442 memcpy(buffer, page_data + page_ofs, page_cnt);
443
Ming Lei12446912012-08-04 12:01:20 +0800444 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700445 buffer += page_cnt;
446 offset += page_cnt;
447 count -= page_cnt;
448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449out:
Laura Garciacad1e552006-05-23 23:22:38 +0200450 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return ret_count;
452}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800453
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700454static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Ming Lei12446912012-08-04 12:01:20 +0800456 struct firmware_buf *buf = fw_priv->buf;
David Woodhouse6e03a202009-04-09 22:04:07 -0700457 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
David Woodhouse6e03a202009-04-09 22:04:07 -0700459 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800460 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700461 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800462 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700463 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
David Woodhouse6e03a202009-04-09 22:04:07 -0700465 new_pages = kmalloc(new_array_size * sizeof(void *),
466 GFP_KERNEL);
467 if (!new_pages) {
468 fw_load_abort(fw_priv);
469 return -ENOMEM;
470 }
Ming Lei12446912012-08-04 12:01:20 +0800471 memcpy(new_pages, buf->pages,
472 buf->page_array_size * sizeof(void *));
473 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
474 (new_array_size - buf->page_array_size));
475 kfree(buf->pages);
476 buf->pages = new_pages;
477 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700479
Ming Lei12446912012-08-04 12:01:20 +0800480 while (buf->nr_pages < pages_needed) {
481 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700482 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
483
Ming Lei12446912012-08-04 12:01:20 +0800484 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700485 fw_load_abort(fw_priv);
486 return -ENOMEM;
487 }
Ming Lei12446912012-08-04 12:01:20 +0800488 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return 0;
491}
492
493/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800494 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700495 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700496 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700497 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800498 * @buffer: buffer being written
499 * @offset: buffer offset for write in total data store area
500 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800502 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 * the driver as a firmware image.
504 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700505static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
506 struct bin_attribute *bin_attr,
507 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200509 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700510 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800511 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 ssize_t retval;
513
514 if (!capable(CAP_SYS_RAWIO))
515 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700516
Laura Garciacad1e552006-05-23 23:22:38 +0200517 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800518 buf = fw_priv->buf;
519 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 retval = -ENODEV;
521 goto out;
522 }
Ming Lei65710cb2012-08-04 12:01:16 +0800523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 retval = fw_realloc_buffer(fw_priv, offset + count);
525 if (retval)
526 goto out;
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700529
530 while (count) {
531 void *page_data;
532 int page_nr = offset >> PAGE_SHIFT;
533 int page_ofs = offset & (PAGE_SIZE - 1);
534 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
535
Ming Lei12446912012-08-04 12:01:20 +0800536 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700537
538 memcpy(page_data + page_ofs, buffer, page_cnt);
539
Ming Lei12446912012-08-04 12:01:20 +0800540 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700541 buffer += page_cnt;
542 offset += page_cnt;
543 count -= page_cnt;
544 }
545
Ming Lei12446912012-08-04 12:01:20 +0800546 buf->size = max_t(size_t, offset, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547out:
Laura Garciacad1e552006-05-23 23:22:38 +0200548 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return retval;
550}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800551
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700552static struct bin_attribute firmware_attr_data = {
553 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 .size = 0,
555 .read = firmware_data_read,
556 .write = firmware_data_write,
557};
558
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700559static void firmware_class_timeout(u_long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 fw_load_abort(fw_priv);
564}
565
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700566static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200567fw_create_instance(struct firmware *firmware, const char *fw_name,
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700568 struct device *device, bool uevent, bool nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700570 struct firmware_priv *fw_priv;
571 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Ming Lei12446912012-08-04 12:01:20 +0800573 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700574 if (!fw_priv) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700575 dev_err(device, "%s: kmalloc failed\n", __func__);
Ming Lei12446912012-08-04 12:01:20 +0800576 fw_priv = ERR_PTR(-ENOMEM);
577 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700580 fw_priv->nowait = nowait;
Ming Lei1f2b7952012-08-04 12:01:21 +0800581 fw_priv->fw = firmware;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700582 setup_timer(&fw_priv->timeout,
583 firmware_class_timeout, (u_long) fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700585 f_dev = &fw_priv->dev;
586
587 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800588 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700589 f_dev->parent = device;
590 f_dev->class = &firmware_class;
Ming Lei12446912012-08-04 12:01:20 +0800591exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700592 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
Ming Lei1f2b7952012-08-04 12:01:21 +0800595/* one pages buffer is mapped/unmapped only once */
596static int fw_map_pages_buf(struct firmware_buf *buf)
597{
598 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
599 if (!buf->data)
600 return -ENOMEM;
601 return 0;
602}
603
604/* store the pages buffer info firmware from buf */
605static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
606{
607 fw->priv = buf;
608 fw->pages = buf->pages;
609 fw->size = buf->size;
610 fw->data = buf->data;
611
612 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
613 __func__, buf->fw_id, buf, buf->data,
614 (unsigned int)buf->size);
615}
616
Ming Leif531f05a2012-08-04 12:01:25 +0800617static void fw_name_devm_release(struct device *dev, void *res)
618{
619 struct fw_name_devm *fwn = res;
620
621 if (fwn->magic == (unsigned long)&fw_cache)
622 pr_debug("%s: fw_name-%s devm-%p released\n",
623 __func__, fwn->name, res);
624}
625
626static int fw_devm_match(struct device *dev, void *res,
627 void *match_data)
628{
629 struct fw_name_devm *fwn = res;
630
631 return (fwn->magic == (unsigned long)&fw_cache) &&
632 !strcmp(fwn->name, match_data);
633}
634
635static struct fw_name_devm *fw_find_devm_name(struct device *dev,
636 const char *name)
637{
638 struct fw_name_devm *fwn;
639
640 fwn = devres_find(dev, fw_name_devm_release,
641 fw_devm_match, (void *)name);
642 return fwn;
643}
644
645/* add firmware name into devres list */
646static int fw_add_devm_name(struct device *dev, const char *name)
647{
648 struct fw_name_devm *fwn;
649
650 fwn = fw_find_devm_name(dev, name);
651 if (fwn)
652 return 1;
653
654 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
655 strlen(name) + 1, GFP_KERNEL);
656 if (!fwn)
657 return -ENOMEM;
658
659 fwn->magic = (unsigned long)&fw_cache;
660 strcpy(fwn->name, name);
661 devres_add(dev, fwn);
662
663 return 0;
664}
665
Ming Lei1f2b7952012-08-04 12:01:21 +0800666static void _request_firmware_cleanup(const struct firmware **firmware_p)
667{
668 release_firmware(*firmware_p);
669 *firmware_p = NULL;
670}
671
Stephen Boyddddb5542012-03-28 23:30:43 +0200672static struct firmware_priv *
673_request_firmware_prepare(const struct firmware **firmware_p, const char *name,
674 struct device *device, bool uevent, bool nowait)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700675{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +0800677 struct firmware_priv *fw_priv = NULL;
678 struct firmware_buf *buf;
679 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 if (!firmware_p)
Stephen Boyddddb5542012-03-28 23:30:43 +0200682 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Jiri Slaby4aed0642005-09-13 01:25:01 -0700684 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700686 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
687 __func__);
Stephen Boyddddb5542012-03-28 23:30:43 +0200688 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800691 if (fw_get_builtin_firmware(firmware, name)) {
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100692 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
Stephen Boyddddb5542012-03-28 23:30:43 +0200693 return NULL;
David Woodhouse5658c762008-05-23 13:52:42 +0100694 }
695
Ming Lei1f2b7952012-08-04 12:01:21 +0800696 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
697 if (!ret)
698 fw_priv = fw_create_instance(firmware, name, device,
699 uevent, nowait);
700
701 if (IS_ERR(fw_priv) || ret < 0) {
702 kfree(firmware);
Stephen Boyddddb5542012-03-28 23:30:43 +0200703 *firmware_p = NULL;
Ming Lei1f2b7952012-08-04 12:01:21 +0800704 return ERR_PTR(-ENOMEM);
705 } else if (fw_priv) {
706 fw_priv->buf = buf;
707
708 /*
709 * bind with 'buf' now to avoid warning in failure path
710 * of requesting firmware.
711 */
712 firmware->priv = buf;
713 return fw_priv;
Stephen Boyddddb5542012-03-28 23:30:43 +0200714 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800715
716 /* share the cached buf, which is inprogessing or completed */
717 check_status:
718 mutex_lock(&fw_lock);
719 if (test_bit(FW_STATUS_ABORT, &buf->status)) {
720 fw_priv = ERR_PTR(-ENOENT);
721 _request_firmware_cleanup(firmware_p);
722 goto exit;
723 } else if (test_bit(FW_STATUS_DONE, &buf->status)) {
724 fw_priv = NULL;
725 fw_set_page_data(buf, firmware);
726 goto exit;
727 }
728 mutex_unlock(&fw_lock);
729 wait_for_completion(&buf->completion);
730 goto check_status;
731
732exit:
733 mutex_unlock(&fw_lock);
Stephen Boyddddb5542012-03-28 23:30:43 +0200734 return fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200735}
736
Stephen Boyddddb5542012-03-28 23:30:43 +0200737static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
738 long timeout)
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200739{
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200740 int retval = 0;
Stephen Boyddddb5542012-03-28 23:30:43 +0200741 struct device *f_dev = &fw_priv->dev;
Ming Lei12446912012-08-04 12:01:20 +0800742 struct firmware_buf *buf = fw_priv->buf;
Linus Torvaldscaca9512011-08-24 15:55:30 -0700743
Stephen Boyddddb5542012-03-28 23:30:43 +0200744 dev_set_uevent_suppress(f_dev, true);
David Woodhouse5658c762008-05-23 13:52:42 +0100745
Stephen Boyddddb5542012-03-28 23:30:43 +0200746 /* Need to pin this module until class device is destroyed */
747 __module_get(THIS_MODULE);
748
749 retval = device_add(f_dev);
750 if (retval) {
751 dev_err(f_dev, "%s: device_register failed\n", __func__);
752 goto err_put_dev;
753 }
754
755 retval = device_create_bin_file(f_dev, &firmware_attr_data);
756 if (retval) {
757 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
758 goto err_del_dev;
759 }
760
761 retval = device_create_file(f_dev, &dev_attr_loading);
762 if (retval) {
763 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
764 goto err_del_bin_attr;
765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Kay Sievers312c0042005-11-16 09:00:00 +0100767 if (uevent) {
Stephen Boyddddb5542012-03-28 23:30:43 +0200768 dev_set_uevent_suppress(f_dev, false);
Ming Lei12446912012-08-04 12:01:20 +0800769 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200770 if (timeout != MAX_SCHEDULE_TIMEOUT)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700771 mod_timer(&fw_priv->timeout,
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200772 round_jiffies_up(jiffies + timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700774 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
775 }
776
Ming Lei12446912012-08-04 12:01:20 +0800777 wait_for_completion(&buf->completion);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700778
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700779 del_timer_sync(&fw_priv->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Laura Garciacad1e552006-05-23 23:22:38 +0200781 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800782 if (!buf->size || test_bit(FW_STATUS_ABORT, &buf->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 retval = -ENOENT;
Ming Lei65710cb2012-08-04 12:01:16 +0800784
Ming Leif531f05a2012-08-04 12:01:25 +0800785 /*
786 * add firmware name into devres list so that we can auto cache
787 * and uncache firmware for device.
788 *
789 * f_dev->parent may has been deleted already, but the problem
790 * should be fixed in devres or driver core.
791 */
792 if (!retval && f_dev->parent)
793 fw_add_devm_name(f_dev->parent, buf->fw_id);
794
Ming Lei65710cb2012-08-04 12:01:16 +0800795 if (!retval)
Ming Lei1f2b7952012-08-04 12:01:21 +0800796 retval = fw_map_pages_buf(buf);
Ming Lei12446912012-08-04 12:01:20 +0800797
Ming Lei1f2b7952012-08-04 12:01:21 +0800798 /* pass the pages buffer to driver at the last minute */
799 fw_set_page_data(buf, fw_priv->fw);
800
Ming Lei12446912012-08-04 12:01:20 +0800801 fw_priv->buf = NULL;
Laura Garciacad1e552006-05-23 23:22:38 +0200802 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Stephen Boyddddb5542012-03-28 23:30:43 +0200804 device_remove_file(f_dev, &dev_attr_loading);
805err_del_bin_attr:
806 device_remove_bin_file(f_dev, &firmware_attr_data);
807err_del_dev:
808 device_del(f_dev);
809err_put_dev:
810 put_device(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return retval;
812}
813
814/**
Kay Sievers312c0042005-11-16 09:00:00 +0100815 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800816 * @firmware_p: pointer to firmware image
817 * @name: name of firmware file
818 * @device: device for which firmware is being loaded
819 *
820 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700821 * of @name for device @device.
822 *
823 * Should be called from user context where sleeping is allowed.
824 *
Kay Sievers312c0042005-11-16 09:00:00 +0100825 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700826 * should be distinctive enough not to be confused with any other
827 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +0800828 *
829 * Caller must hold the reference count of @device.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700830 **/
831int
832request_firmware(const struct firmware **firmware_p, const char *name,
833 struct device *device)
834{
Stephen Boyddddb5542012-03-28 23:30:43 +0200835 struct firmware_priv *fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200836 int ret;
837
Stephen Boyddddb5542012-03-28 23:30:43 +0200838 fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
839 false);
840 if (IS_ERR_OR_NULL(fw_priv))
841 return PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200842
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200843 ret = usermodehelper_read_trylock();
844 if (WARN_ON(ret)) {
845 dev_err(device, "firmware: %s will not be loaded\n", name);
846 } else {
Stephen Boyddddb5542012-03-28 23:30:43 +0200847 ret = _request_firmware_load(fw_priv, true,
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200848 firmware_loading_timeout());
849 usermodehelper_read_unlock();
850 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200851 if (ret)
852 _request_firmware_cleanup(firmware_p);
853
854 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700855}
856
857/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800859 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800861void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800864 if (!fw_is_builtin_firmware(fw))
865 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 kfree(fw);
867 }
868}
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870/* Async support */
871struct firmware_work {
872 struct work_struct work;
873 struct module *module;
874 const char *name;
875 struct device *device;
876 void *context;
877 void (*cont)(const struct firmware *fw, void *context);
Bob Liu072fc8f2011-01-26 18:33:32 +0800878 bool uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879};
880
Stephen Boyda36cf842012-03-28 23:31:00 +0200881static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Stephen Boyda36cf842012-03-28 23:31:00 +0200883 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 const struct firmware *fw;
Stephen Boyddddb5542012-03-28 23:30:43 +0200885 struct firmware_priv *fw_priv;
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200886 long timeout;
matthieu castet113fab12005-11-13 16:07:39 -0800887 int ret;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700888
Stephen Boyda36cf842012-03-28 23:31:00 +0200889 fw_work = container_of(work, struct firmware_work, work);
Stephen Boyddddb5542012-03-28 23:30:43 +0200890 fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
891 fw_work->uevent, true);
892 if (IS_ERR_OR_NULL(fw_priv)) {
893 ret = PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200894 goto out;
Stephen Boyddddb5542012-03-28 23:30:43 +0200895 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200896
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200897 timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
898 if (timeout) {
Stephen Boyddddb5542012-03-28 23:30:43 +0200899 ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200900 usermodehelper_read_unlock();
901 } else {
902 dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
903 fw_work->name);
904 ret = -EAGAIN;
905 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200906 if (ret)
907 _request_firmware_cleanup(&fw);
908
909 out:
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100910 fw_work->cont(fw, fw_work->context);
Ming Lei0cfc1e12012-08-04 12:01:23 +0800911 put_device(fw_work->device);
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 module_put(fw_work->module);
914 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
917/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000918 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800919 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +0100920 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800921 * is non-zero else the firmware copy must be done manually.
922 * @name: name of firmware file
923 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100924 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800925 * @context: will be passed over to @cont, and
926 * @fw may be %NULL if firmware request fails.
927 * @cont: function will be called asynchronously when the firmware
928 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 *
Ming Lei0cfc1e12012-08-04 12:01:23 +0800930 * Caller must hold the reference count of @device.
931 *
Ming Lei6f21a622012-08-04 12:01:24 +0800932 * Asynchronous variant of request_firmware() for user contexts:
933 * - sleep for as small periods as possible since it may
934 * increase kernel boot time of built-in device drivers
935 * requesting firmware in their ->probe() methods, if
936 * @gfp is GFP_KERNEL.
937 *
938 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 **/
940int
941request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +0800942 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100943 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 void (*cont)(const struct firmware *fw, void *context))
945{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700946 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700948 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (!fw_work)
950 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700951
952 fw_work->module = module;
953 fw_work->name = name;
954 fw_work->device = device;
955 fw_work->context = context;
956 fw_work->cont = cont;
957 fw_work->uevent = uevent;
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (!try_module_get(module)) {
960 kfree(fw_work);
961 return -EFAULT;
962 }
963
Ming Lei0cfc1e12012-08-04 12:01:23 +0800964 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +0200965 INIT_WORK(&fw_work->work, request_firmware_work_func);
966 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return 0;
968}
969
Ming Lei2887b392012-08-04 12:01:22 +0800970/**
971 * cache_firmware - cache one firmware image in kernel memory space
972 * @fw_name: the firmware image name
973 *
974 * Cache firmware in kernel memory so that drivers can use it when
975 * system isn't ready for them to request firmware image from userspace.
976 * Once it returns successfully, driver can use request_firmware or its
977 * nowait version to get the cached firmware without any interacting
978 * with userspace
979 *
980 * Return 0 if the firmware image has been cached successfully
981 * Return !0 otherwise
982 *
983 */
984int cache_firmware(const char *fw_name)
985{
986 int ret;
987 const struct firmware *fw;
988
989 pr_debug("%s: %s\n", __func__, fw_name);
990
991 ret = request_firmware(&fw, fw_name, NULL);
992 if (!ret)
993 kfree(fw);
994
995 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
996
997 return ret;
998}
999
1000/**
1001 * uncache_firmware - remove one cached firmware image
1002 * @fw_name: the firmware image name
1003 *
1004 * Uncache one firmware image which has been cached successfully
1005 * before.
1006 *
1007 * Return 0 if the firmware cache has been removed successfully
1008 * Return !0 otherwise
1009 *
1010 */
1011int uncache_firmware(const char *fw_name)
1012{
1013 struct firmware_buf *buf;
1014 struct firmware fw;
1015
1016 pr_debug("%s: %s\n", __func__, fw_name);
1017
1018 if (fw_get_builtin_firmware(&fw, fw_name))
1019 return 0;
1020
1021 buf = fw_lookup_buf(fw_name);
1022 if (buf) {
1023 fw_free_buf(buf);
1024 return 0;
1025 }
1026
1027 return -EINVAL;
1028}
1029
Ming Lei37276a52012-08-04 12:01:27 +08001030static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1031{
1032 struct fw_cache_entry *fce;
1033
1034 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1035 if (!fce)
1036 goto exit;
1037
1038 strcpy(fce->name, name);
1039exit:
1040 return fce;
1041}
1042
1043static void free_fw_cache_entry(struct fw_cache_entry *fce)
1044{
1045 kfree(fce);
1046}
1047
1048static void __async_dev_cache_fw_image(void *fw_entry,
1049 async_cookie_t cookie)
1050{
1051 struct fw_cache_entry *fce = fw_entry;
1052 struct firmware_cache *fwc = &fw_cache;
1053 int ret;
1054
1055 ret = cache_firmware(fce->name);
1056 if (ret)
1057 goto free;
1058
1059 spin_lock(&fwc->name_lock);
1060 list_add(&fce->list, &fwc->fw_names);
1061 spin_unlock(&fwc->name_lock);
1062 goto drop_ref;
1063
1064free:
1065 free_fw_cache_entry(fce);
1066drop_ref:
1067 spin_lock(&fwc->name_lock);
1068 fwc->cnt--;
1069 spin_unlock(&fwc->name_lock);
1070
1071 wake_up(&fwc->wait_queue);
1072}
1073
1074/* called with dev->devres_lock held */
1075static void dev_create_fw_entry(struct device *dev, void *res,
1076 void *data)
1077{
1078 struct fw_name_devm *fwn = res;
1079 const char *fw_name = fwn->name;
1080 struct list_head *head = data;
1081 struct fw_cache_entry *fce;
1082
1083 fce = alloc_fw_cache_entry(fw_name);
1084 if (fce)
1085 list_add(&fce->list, head);
1086}
1087
1088static int devm_name_match(struct device *dev, void *res,
1089 void *match_data)
1090{
1091 struct fw_name_devm *fwn = res;
1092 return (fwn->magic == (unsigned long)match_data);
1093}
1094
Ming Leiab6dd8e2012-08-17 22:07:00 +08001095static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001096{
1097 LIST_HEAD(todo);
1098 struct fw_cache_entry *fce;
1099 struct fw_cache_entry *fce_next;
1100 struct firmware_cache *fwc = &fw_cache;
1101
1102 devres_for_each_res(dev, fw_name_devm_release,
1103 devm_name_match, &fw_cache,
1104 dev_create_fw_entry, &todo);
1105
1106 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1107 list_del(&fce->list);
1108
1109 spin_lock(&fwc->name_lock);
1110 fwc->cnt++;
1111 spin_unlock(&fwc->name_lock);
1112
1113 async_schedule(__async_dev_cache_fw_image, (void *)fce);
1114 }
1115}
1116
1117static void __device_uncache_fw_images(void)
1118{
1119 struct firmware_cache *fwc = &fw_cache;
1120 struct fw_cache_entry *fce;
1121
1122 spin_lock(&fwc->name_lock);
1123 while (!list_empty(&fwc->fw_names)) {
1124 fce = list_entry(fwc->fw_names.next,
1125 struct fw_cache_entry, list);
1126 list_del(&fce->list);
1127 spin_unlock(&fwc->name_lock);
1128
1129 uncache_firmware(fce->name);
1130 free_fw_cache_entry(fce);
1131
1132 spin_lock(&fwc->name_lock);
1133 }
1134 spin_unlock(&fwc->name_lock);
1135}
1136
1137/**
1138 * device_cache_fw_images - cache devices' firmware
1139 *
1140 * If one device called request_firmware or its nowait version
1141 * successfully before, the firmware names are recored into the
1142 * device's devres link list, so device_cache_fw_images can call
1143 * cache_firmware() to cache these firmwares for the device,
1144 * then the device driver can load its firmwares easily at
1145 * time when system is not ready to complete loading firmware.
1146 */
1147static void device_cache_fw_images(void)
1148{
1149 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001150 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001151 DEFINE_WAIT(wait);
1152
1153 pr_debug("%s\n", __func__);
1154
Ming Leiffe53f62012-08-04 12:01:28 +08001155 /*
1156 * use small loading timeout for caching devices' firmware
1157 * because all these firmware images have been loaded
1158 * successfully at lease once, also system is ready for
1159 * completing firmware loading now. The maximum size of
1160 * firmware in current distributions is about 2M bytes,
1161 * so 10 secs should be enough.
1162 */
1163 old_timeout = loading_timeout;
1164 loading_timeout = 10;
1165
Ming Leiab6dd8e2012-08-17 22:07:00 +08001166 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Lei37276a52012-08-04 12:01:27 +08001167
1168 /* wait for completion of caching firmware for all devices */
1169 spin_lock(&fwc->name_lock);
1170 for (;;) {
1171 prepare_to_wait(&fwc->wait_queue, &wait,
1172 TASK_UNINTERRUPTIBLE);
1173 if (!fwc->cnt)
1174 break;
1175
1176 spin_unlock(&fwc->name_lock);
1177
1178 schedule();
1179
1180 spin_lock(&fwc->name_lock);
1181 }
1182 spin_unlock(&fwc->name_lock);
1183 finish_wait(&fwc->wait_queue, &wait);
Ming Leiffe53f62012-08-04 12:01:28 +08001184
1185 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001186}
1187
1188/**
1189 * device_uncache_fw_images - uncache devices' firmware
1190 *
1191 * uncache all firmwares which have been cached successfully
1192 * by device_uncache_fw_images earlier
1193 */
1194static void device_uncache_fw_images(void)
1195{
1196 pr_debug("%s\n", __func__);
1197 __device_uncache_fw_images();
1198}
1199
1200static void device_uncache_fw_images_work(struct work_struct *work)
1201{
1202 device_uncache_fw_images();
1203}
1204
1205/**
1206 * device_uncache_fw_images_delay - uncache devices firmwares
1207 * @delay: number of milliseconds to delay uncache device firmwares
1208 *
1209 * uncache all devices's firmwares which has been cached successfully
1210 * by device_cache_fw_images after @delay milliseconds.
1211 */
1212static void device_uncache_fw_images_delay(unsigned long delay)
1213{
1214 schedule_delayed_work(&fw_cache.work,
1215 msecs_to_jiffies(delay));
1216}
1217
Ming Lei07646d92012-08-04 12:01:29 +08001218#ifdef CONFIG_PM
1219static int fw_pm_notify(struct notifier_block *notify_block,
1220 unsigned long mode, void *unused)
1221{
1222 switch (mode) {
1223 case PM_HIBERNATION_PREPARE:
1224 case PM_SUSPEND_PREPARE:
1225 device_cache_fw_images();
1226 break;
1227
1228 case PM_POST_SUSPEND:
1229 case PM_POST_HIBERNATION:
1230 case PM_POST_RESTORE:
1231 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1232 break;
1233 }
1234
1235 return 0;
1236}
1237#else
1238static int fw_pm_notify(struct notifier_block *notify_block,
1239 unsigned long mode, void *unused)
Ming Leic08f6772012-08-17 22:06:58 +08001240{
1241 return 0;
1242}
Ming Lei07646d92012-08-04 12:01:29 +08001243#endif
1244
Ming Lei37276a52012-08-04 12:01:27 +08001245static void __init fw_cache_init(void)
1246{
1247 spin_lock_init(&fw_cache.lock);
1248 INIT_LIST_HEAD(&fw_cache.head);
1249
1250 spin_lock_init(&fw_cache.name_lock);
1251 INIT_LIST_HEAD(&fw_cache.fw_names);
1252 fw_cache.cnt = 0;
1253
1254 init_waitqueue_head(&fw_cache.wait_queue);
1255 INIT_DELAYED_WORK(&fw_cache.work,
1256 device_uncache_fw_images_work);
Ming Lei07646d92012-08-04 12:01:29 +08001257
1258 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1259 register_pm_notifier(&fw_cache.pm_notify);
Ming Lei37276a52012-08-04 12:01:27 +08001260}
1261
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001262static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
Ming Lei1f2b7952012-08-04 12:01:21 +08001264 fw_cache_init();
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001265 return class_register(&firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001267
1268static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Ming Lei07646d92012-08-04 12:01:29 +08001270 unregister_pm_notifier(&fw_cache.pm_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 class_unregister(&firmware_class);
1272}
1273
Shaohua Lia30a6a22006-09-27 01:50:52 -07001274fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275module_exit(firmware_class_exit);
1276
1277EXPORT_SYMBOL(release_firmware);
1278EXPORT_SYMBOL(request_firmware);
1279EXPORT_SYMBOL(request_firmware_nowait);
Ming Lei2887b392012-08-04 12:01:22 +08001280EXPORT_SYMBOL_GPL(cache_firmware);
1281EXPORT_SYMBOL_GPL(uncache_firmware);