blob: 68fd4c698c7745b76d1b42b21a2dc258df7e454a [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>
27
28#include "base.h"
29#include "power/power.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 Lei1f2b7952012-08-04 12:01:21 +0800111};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Ming Lei12446912012-08-04 12:01:20 +0800113struct firmware_buf {
Ming Lei1f2b7952012-08-04 12:01:21 +0800114 struct kref ref;
115 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 struct completion completion;
Ming Lei1f2b7952012-08-04 12:01:21 +0800117 struct firmware_cache *fwc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 unsigned long status;
Ming Lei65710cb2012-08-04 12:01:16 +0800119 void *data;
120 size_t size;
David Woodhouse6e03a202009-04-09 22:04:07 -0700121 struct page **pages;
122 int nr_pages;
123 int page_array_size;
Dmitry Torokhove1771232010-03-13 23:49:23 -0800124 char fw_id[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
Ming Lei37276a52012-08-04 12:01:27 +0800127struct fw_cache_entry {
128 struct list_head list;
129 char name[];
130};
131
Ming Lei12446912012-08-04 12:01:20 +0800132struct firmware_priv {
133 struct timer_list timeout;
134 bool nowait;
135 struct device dev;
136 struct firmware_buf *buf;
Ming Lei1f2b7952012-08-04 12:01:21 +0800137 struct firmware *fw;
Ming Lei12446912012-08-04 12:01:20 +0800138};
139
Ming Leif531f05a2012-08-04 12:01:25 +0800140struct fw_name_devm {
141 unsigned long magic;
142 char name[];
143};
144
Ming Lei1f2b7952012-08-04 12:01:21 +0800145#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
146
147/* fw_lock could be moved to 'struct firmware_priv' but since it is just
148 * guarding for corner cases a global lock should be OK */
149static DEFINE_MUTEX(fw_lock);
150
151static struct firmware_cache fw_cache;
152
153static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
154 struct firmware_cache *fwc)
155{
156 struct firmware_buf *buf;
157
158 buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
159
160 if (!buf)
161 return buf;
162
163 kref_init(&buf->ref);
164 strcpy(buf->fw_id, fw_name);
165 buf->fwc = fwc;
166 init_completion(&buf->completion);
167
168 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
169
170 return buf;
171}
172
Ming Lei2887b392012-08-04 12:01:22 +0800173static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
174{
175 struct firmware_buf *tmp;
176 struct firmware_cache *fwc = &fw_cache;
177
178 list_for_each_entry(tmp, &fwc->head, list)
179 if (!strcmp(tmp->fw_id, fw_name))
180 return tmp;
181 return NULL;
182}
183
Ming Lei1f2b7952012-08-04 12:01:21 +0800184static int fw_lookup_and_allocate_buf(const char *fw_name,
185 struct firmware_cache *fwc,
186 struct firmware_buf **buf)
187{
188 struct firmware_buf *tmp;
189
190 spin_lock(&fwc->lock);
Ming Lei2887b392012-08-04 12:01:22 +0800191 tmp = __fw_lookup_buf(fw_name);
192 if (tmp) {
193 kref_get(&tmp->ref);
194 spin_unlock(&fwc->lock);
195 *buf = tmp;
196 return 1;
197 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800198 tmp = __allocate_fw_buf(fw_name, fwc);
199 if (tmp)
200 list_add(&tmp->list, &fwc->head);
201 spin_unlock(&fwc->lock);
202
203 *buf = tmp;
204
205 return tmp ? 0 : -ENOMEM;
206}
207
Ming Lei2887b392012-08-04 12:01:22 +0800208static struct firmware_buf *fw_lookup_buf(const char *fw_name)
209{
210 struct firmware_buf *tmp;
211 struct firmware_cache *fwc = &fw_cache;
212
213 spin_lock(&fwc->lock);
214 tmp = __fw_lookup_buf(fw_name);
215 spin_unlock(&fwc->lock);
216
217 return tmp;
218}
219
Ming Lei1f2b7952012-08-04 12:01:21 +0800220static void __fw_free_buf(struct kref *ref)
221{
222 struct firmware_buf *buf = to_fwbuf(ref);
223 struct firmware_cache *fwc = buf->fwc;
224 int i;
225
226 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
227 __func__, buf->fw_id, buf, buf->data,
228 (unsigned int)buf->size);
229
230 spin_lock(&fwc->lock);
231 list_del(&buf->list);
232 spin_unlock(&fwc->lock);
233
234 vunmap(buf->data);
235 for (i = 0; i < buf->nr_pages; i++)
236 __free_page(buf->pages[i]);
237 kfree(buf->pages);
238 kfree(buf);
239}
240
241static void fw_free_buf(struct firmware_buf *buf)
242{
243 kref_put(&buf->ref, __fw_free_buf);
244}
245
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700246static struct firmware_priv *to_firmware_priv(struct device *dev)
247{
248 return container_of(dev, struct firmware_priv, dev);
249}
250
251static void fw_load_abort(struct firmware_priv *fw_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Ming Lei12446912012-08-04 12:01:20 +0800253 struct firmware_buf *buf = fw_priv->buf;
254
255 set_bit(FW_STATUS_ABORT, &buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800256 complete_all(&buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700259static ssize_t firmware_timeout_show(struct class *class,
260 struct class_attribute *attr,
261 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 return sprintf(buf, "%d\n", loading_timeout);
264}
265
266/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800267 * firmware_timeout_store - set number of seconds to wait for firmware
268 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800269 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800270 * @buf: buffer to scan for timeout value
271 * @count: number of bytes in @buf
272 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800274 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 * firmware will be provided.
276 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800277 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700279static ssize_t firmware_timeout_store(struct class *class,
280 struct class_attribute *attr,
281 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700284 if (loading_timeout < 0)
285 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return count;
288}
289
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800290static struct class_attribute firmware_class_attrs[] = {
291 __ATTR(timeout, S_IWUSR | S_IRUGO,
292 firmware_timeout_show, firmware_timeout_store),
293 __ATTR_NULL
294};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800296static void fw_dev_release(struct device *dev)
297{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700298 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800299
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800300 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800301
302 module_put(THIS_MODULE);
303}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Kay Sievers7eff2e72007-08-14 15:15:12 +0200305static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700307 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Ming Lei12446912012-08-04 12:01:20 +0800309 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200311 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700312 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200313 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
314 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 return 0;
317}
318
Adrian Bunk1b81d662006-05-20 15:00:16 -0700319static struct class firmware_class = {
320 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800321 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700322 .dev_uevent = firmware_uevent,
323 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700324};
325
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700326static ssize_t firmware_loading_show(struct device *dev,
327 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700329 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800330 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return sprintf(buf, "%d\n", loading);
333}
334
Ming Lei65710cb2012-08-04 12:01:16 +0800335/* firmware holds the ownership of pages */
David Woodhousedd336c52010-05-02 11:21:21 +0300336static void firmware_free_data(const struct firmware *fw)
337{
Ming Lei1f2b7952012-08-04 12:01:21 +0800338 WARN_ON(!fw->priv);
339 fw_free_buf(fw->priv);
David Woodhousedd336c52010-05-02 11:21:21 +0300340}
341
David Woodhouse6e03a202009-04-09 22:04:07 -0700342/* Some architectures don't have PAGE_KERNEL_RO */
343#ifndef PAGE_KERNEL_RO
344#define PAGE_KERNEL_RO PAGE_KERNEL
345#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800347 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700348 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800349 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800350 * @buf: buffer to scan for loading control value
351 * @count: number of bytes in @buf
352 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 * The relevant values are:
354 *
355 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800356 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 * -1: Conclude the load with an error and discard any written data.
358 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700359static ssize_t firmware_loading_store(struct device *dev,
360 struct device_attribute *attr,
361 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700363 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800364 struct firmware_buf *fw_buf = fw_priv->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700366 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Neil Hormaneea915b2012-01-02 15:31:23 -0500368 mutex_lock(&fw_lock);
369
Ming Lei12446912012-08-04 12:01:20 +0800370 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500371 goto out;
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 switch (loading) {
374 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800375 /* discarding any previous partial load */
Ming Lei12446912012-08-04 12:01:20 +0800376 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
377 for (i = 0; i < fw_buf->nr_pages; i++)
378 __free_page(fw_buf->pages[i]);
379 kfree(fw_buf->pages);
380 fw_buf->pages = NULL;
381 fw_buf->page_array_size = 0;
382 fw_buf->nr_pages = 0;
383 set_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei28eefa72012-08-04 12:01:17 +0800384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 break;
386 case 0:
Ming Lei12446912012-08-04 12:01:20 +0800387 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
388 set_bit(FW_STATUS_DONE, &fw_buf->status);
389 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800390 complete_all(&fw_buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 break;
392 }
393 /* fallthrough */
394 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700395 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 /* fallthrough */
397 case -1:
398 fw_load_abort(fw_priv);
399 break;
400 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500401out:
402 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return count;
404}
405
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700406static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700408static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
409 struct bin_attribute *bin_attr,
410 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200412 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700413 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800414 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700415 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Laura Garciacad1e552006-05-23 23:22:38 +0200417 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800418 buf = fw_priv->buf;
419 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 ret_count = -ENODEV;
421 goto out;
422 }
Ming Lei12446912012-08-04 12:01:20 +0800423 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200424 ret_count = 0;
425 goto out;
426 }
Ming Lei12446912012-08-04 12:01:20 +0800427 if (count > buf->size - offset)
428 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700429
430 ret_count = count;
431
432 while (count) {
433 void *page_data;
434 int page_nr = offset >> PAGE_SHIFT;
435 int page_ofs = offset & (PAGE_SIZE-1);
436 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
437
Ming Lei12446912012-08-04 12:01:20 +0800438 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700439
440 memcpy(buffer, page_data + page_ofs, page_cnt);
441
Ming Lei12446912012-08-04 12:01:20 +0800442 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700443 buffer += page_cnt;
444 offset += page_cnt;
445 count -= page_cnt;
446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447out:
Laura Garciacad1e552006-05-23 23:22:38 +0200448 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return ret_count;
450}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800451
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700452static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Ming Lei12446912012-08-04 12:01:20 +0800454 struct firmware_buf *buf = fw_priv->buf;
David Woodhouse6e03a202009-04-09 22:04:07 -0700455 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
David Woodhouse6e03a202009-04-09 22:04:07 -0700457 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800458 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700459 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800460 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700461 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
David Woodhouse6e03a202009-04-09 22:04:07 -0700463 new_pages = kmalloc(new_array_size * sizeof(void *),
464 GFP_KERNEL);
465 if (!new_pages) {
466 fw_load_abort(fw_priv);
467 return -ENOMEM;
468 }
Ming Lei12446912012-08-04 12:01:20 +0800469 memcpy(new_pages, buf->pages,
470 buf->page_array_size * sizeof(void *));
471 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
472 (new_array_size - buf->page_array_size));
473 kfree(buf->pages);
474 buf->pages = new_pages;
475 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700477
Ming Lei12446912012-08-04 12:01:20 +0800478 while (buf->nr_pages < pages_needed) {
479 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700480 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
481
Ming Lei12446912012-08-04 12:01:20 +0800482 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700483 fw_load_abort(fw_priv);
484 return -ENOMEM;
485 }
Ming Lei12446912012-08-04 12:01:20 +0800486 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return 0;
489}
490
491/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800492 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700493 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700494 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700495 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800496 * @buffer: buffer being written
497 * @offset: buffer offset for write in total data store area
498 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800500 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 * the driver as a firmware image.
502 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700503static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
504 struct bin_attribute *bin_attr,
505 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200507 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700508 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800509 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 ssize_t retval;
511
512 if (!capable(CAP_SYS_RAWIO))
513 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700514
Laura Garciacad1e552006-05-23 23:22:38 +0200515 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800516 buf = fw_priv->buf;
517 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 retval = -ENODEV;
519 goto out;
520 }
Ming Lei65710cb2012-08-04 12:01:16 +0800521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 retval = fw_realloc_buffer(fw_priv, offset + count);
523 if (retval)
524 goto out;
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700527
528 while (count) {
529 void *page_data;
530 int page_nr = offset >> PAGE_SHIFT;
531 int page_ofs = offset & (PAGE_SIZE - 1);
532 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
533
Ming Lei12446912012-08-04 12:01:20 +0800534 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700535
536 memcpy(page_data + page_ofs, buffer, page_cnt);
537
Ming Lei12446912012-08-04 12:01:20 +0800538 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700539 buffer += page_cnt;
540 offset += page_cnt;
541 count -= page_cnt;
542 }
543
Ming Lei12446912012-08-04 12:01:20 +0800544 buf->size = max_t(size_t, offset, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545out:
Laura Garciacad1e552006-05-23 23:22:38 +0200546 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return retval;
548}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800549
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700550static struct bin_attribute firmware_attr_data = {
551 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 .size = 0,
553 .read = firmware_data_read,
554 .write = firmware_data_write,
555};
556
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700557static void firmware_class_timeout(u_long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 fw_load_abort(fw_priv);
562}
563
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700564static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200565fw_create_instance(struct firmware *firmware, const char *fw_name,
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700566 struct device *device, bool uevent, bool nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700568 struct firmware_priv *fw_priv;
569 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Ming Lei12446912012-08-04 12:01:20 +0800571 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700572 if (!fw_priv) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700573 dev_err(device, "%s: kmalloc failed\n", __func__);
Ming Lei12446912012-08-04 12:01:20 +0800574 fw_priv = ERR_PTR(-ENOMEM);
575 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700578 fw_priv->nowait = nowait;
Ming Lei1f2b7952012-08-04 12:01:21 +0800579 fw_priv->fw = firmware;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700580 setup_timer(&fw_priv->timeout,
581 firmware_class_timeout, (u_long) fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700583 f_dev = &fw_priv->dev;
584
585 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800586 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700587 f_dev->parent = device;
588 f_dev->class = &firmware_class;
Ming Lei12446912012-08-04 12:01:20 +0800589exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700590 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
Ming Lei1f2b7952012-08-04 12:01:21 +0800593/* one pages buffer is mapped/unmapped only once */
594static int fw_map_pages_buf(struct firmware_buf *buf)
595{
596 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
597 if (!buf->data)
598 return -ENOMEM;
599 return 0;
600}
601
602/* store the pages buffer info firmware from buf */
603static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
604{
605 fw->priv = buf;
606 fw->pages = buf->pages;
607 fw->size = buf->size;
608 fw->data = buf->data;
609
610 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
611 __func__, buf->fw_id, buf, buf->data,
612 (unsigned int)buf->size);
613}
614
Ming Leif531f05a2012-08-04 12:01:25 +0800615static void fw_name_devm_release(struct device *dev, void *res)
616{
617 struct fw_name_devm *fwn = res;
618
619 if (fwn->magic == (unsigned long)&fw_cache)
620 pr_debug("%s: fw_name-%s devm-%p released\n",
621 __func__, fwn->name, res);
622}
623
624static int fw_devm_match(struct device *dev, void *res,
625 void *match_data)
626{
627 struct fw_name_devm *fwn = res;
628
629 return (fwn->magic == (unsigned long)&fw_cache) &&
630 !strcmp(fwn->name, match_data);
631}
632
633static struct fw_name_devm *fw_find_devm_name(struct device *dev,
634 const char *name)
635{
636 struct fw_name_devm *fwn;
637
638 fwn = devres_find(dev, fw_name_devm_release,
639 fw_devm_match, (void *)name);
640 return fwn;
641}
642
643/* add firmware name into devres list */
644static int fw_add_devm_name(struct device *dev, const char *name)
645{
646 struct fw_name_devm *fwn;
647
648 fwn = fw_find_devm_name(dev, name);
649 if (fwn)
650 return 1;
651
652 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
653 strlen(name) + 1, GFP_KERNEL);
654 if (!fwn)
655 return -ENOMEM;
656
657 fwn->magic = (unsigned long)&fw_cache;
658 strcpy(fwn->name, name);
659 devres_add(dev, fwn);
660
661 return 0;
662}
663
Ming Lei1f2b7952012-08-04 12:01:21 +0800664static void _request_firmware_cleanup(const struct firmware **firmware_p)
665{
666 release_firmware(*firmware_p);
667 *firmware_p = NULL;
668}
669
Stephen Boyddddb5542012-03-28 23:30:43 +0200670static struct firmware_priv *
671_request_firmware_prepare(const struct firmware **firmware_p, const char *name,
672 struct device *device, bool uevent, bool nowait)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700673{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +0800675 struct firmware_priv *fw_priv = NULL;
676 struct firmware_buf *buf;
677 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 if (!firmware_p)
Stephen Boyddddb5542012-03-28 23:30:43 +0200680 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Jiri Slaby4aed0642005-09-13 01:25:01 -0700682 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700684 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
685 __func__);
Stephen Boyddddb5542012-03-28 23:30:43 +0200686 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800689 if (fw_get_builtin_firmware(firmware, name)) {
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100690 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
Stephen Boyddddb5542012-03-28 23:30:43 +0200691 return NULL;
David Woodhouse5658c762008-05-23 13:52:42 +0100692 }
693
Ming Lei1f2b7952012-08-04 12:01:21 +0800694 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
695 if (!ret)
696 fw_priv = fw_create_instance(firmware, name, device,
697 uevent, nowait);
698
699 if (IS_ERR(fw_priv) || ret < 0) {
700 kfree(firmware);
Stephen Boyddddb5542012-03-28 23:30:43 +0200701 *firmware_p = NULL;
Ming Lei1f2b7952012-08-04 12:01:21 +0800702 return ERR_PTR(-ENOMEM);
703 } else if (fw_priv) {
704 fw_priv->buf = buf;
705
706 /*
707 * bind with 'buf' now to avoid warning in failure path
708 * of requesting firmware.
709 */
710 firmware->priv = buf;
711 return fw_priv;
Stephen Boyddddb5542012-03-28 23:30:43 +0200712 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800713
714 /* share the cached buf, which is inprogessing or completed */
715 check_status:
716 mutex_lock(&fw_lock);
717 if (test_bit(FW_STATUS_ABORT, &buf->status)) {
718 fw_priv = ERR_PTR(-ENOENT);
719 _request_firmware_cleanup(firmware_p);
720 goto exit;
721 } else if (test_bit(FW_STATUS_DONE, &buf->status)) {
722 fw_priv = NULL;
723 fw_set_page_data(buf, firmware);
724 goto exit;
725 }
726 mutex_unlock(&fw_lock);
727 wait_for_completion(&buf->completion);
728 goto check_status;
729
730exit:
731 mutex_unlock(&fw_lock);
Stephen Boyddddb5542012-03-28 23:30:43 +0200732 return fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200733}
734
Stephen Boyddddb5542012-03-28 23:30:43 +0200735static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
736 long timeout)
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200737{
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200738 int retval = 0;
Stephen Boyddddb5542012-03-28 23:30:43 +0200739 struct device *f_dev = &fw_priv->dev;
Ming Lei12446912012-08-04 12:01:20 +0800740 struct firmware_buf *buf = fw_priv->buf;
Linus Torvaldscaca9512011-08-24 15:55:30 -0700741
Stephen Boyddddb5542012-03-28 23:30:43 +0200742 dev_set_uevent_suppress(f_dev, true);
David Woodhouse5658c762008-05-23 13:52:42 +0100743
Stephen Boyddddb5542012-03-28 23:30:43 +0200744 /* Need to pin this module until class device is destroyed */
745 __module_get(THIS_MODULE);
746
747 retval = device_add(f_dev);
748 if (retval) {
749 dev_err(f_dev, "%s: device_register failed\n", __func__);
750 goto err_put_dev;
751 }
752
753 retval = device_create_bin_file(f_dev, &firmware_attr_data);
754 if (retval) {
755 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
756 goto err_del_dev;
757 }
758
759 retval = device_create_file(f_dev, &dev_attr_loading);
760 if (retval) {
761 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
762 goto err_del_bin_attr;
763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Kay Sievers312c0042005-11-16 09:00:00 +0100765 if (uevent) {
Stephen Boyddddb5542012-03-28 23:30:43 +0200766 dev_set_uevent_suppress(f_dev, false);
Ming Lei12446912012-08-04 12:01:20 +0800767 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200768 if (timeout != MAX_SCHEDULE_TIMEOUT)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700769 mod_timer(&fw_priv->timeout,
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200770 round_jiffies_up(jiffies + timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700772 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
773 }
774
Ming Lei12446912012-08-04 12:01:20 +0800775 wait_for_completion(&buf->completion);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700776
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700777 del_timer_sync(&fw_priv->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Laura Garciacad1e552006-05-23 23:22:38 +0200779 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800780 if (!buf->size || test_bit(FW_STATUS_ABORT, &buf->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 retval = -ENOENT;
Ming Lei65710cb2012-08-04 12:01:16 +0800782
Ming Leif531f05a2012-08-04 12:01:25 +0800783 /*
784 * add firmware name into devres list so that we can auto cache
785 * and uncache firmware for device.
786 *
787 * f_dev->parent may has been deleted already, but the problem
788 * should be fixed in devres or driver core.
789 */
790 if (!retval && f_dev->parent)
791 fw_add_devm_name(f_dev->parent, buf->fw_id);
792
Ming Lei65710cb2012-08-04 12:01:16 +0800793 if (!retval)
Ming Lei1f2b7952012-08-04 12:01:21 +0800794 retval = fw_map_pages_buf(buf);
Ming Lei12446912012-08-04 12:01:20 +0800795
Ming Lei1f2b7952012-08-04 12:01:21 +0800796 /* pass the pages buffer to driver at the last minute */
797 fw_set_page_data(buf, fw_priv->fw);
798
Ming Lei12446912012-08-04 12:01:20 +0800799 fw_priv->buf = NULL;
Laura Garciacad1e552006-05-23 23:22:38 +0200800 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Stephen Boyddddb5542012-03-28 23:30:43 +0200802 device_remove_file(f_dev, &dev_attr_loading);
803err_del_bin_attr:
804 device_remove_bin_file(f_dev, &firmware_attr_data);
805err_del_dev:
806 device_del(f_dev);
807err_put_dev:
808 put_device(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return retval;
810}
811
812/**
Kay Sievers312c0042005-11-16 09:00:00 +0100813 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800814 * @firmware_p: pointer to firmware image
815 * @name: name of firmware file
816 * @device: device for which firmware is being loaded
817 *
818 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700819 * of @name for device @device.
820 *
821 * Should be called from user context where sleeping is allowed.
822 *
Kay Sievers312c0042005-11-16 09:00:00 +0100823 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700824 * should be distinctive enough not to be confused with any other
825 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +0800826 *
827 * Caller must hold the reference count of @device.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700828 **/
829int
830request_firmware(const struct firmware **firmware_p, const char *name,
831 struct device *device)
832{
Stephen Boyddddb5542012-03-28 23:30:43 +0200833 struct firmware_priv *fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200834 int ret;
835
Stephen Boyddddb5542012-03-28 23:30:43 +0200836 fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
837 false);
838 if (IS_ERR_OR_NULL(fw_priv))
839 return PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200840
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200841 ret = usermodehelper_read_trylock();
842 if (WARN_ON(ret)) {
843 dev_err(device, "firmware: %s will not be loaded\n", name);
844 } else {
Stephen Boyddddb5542012-03-28 23:30:43 +0200845 ret = _request_firmware_load(fw_priv, true,
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200846 firmware_loading_timeout());
847 usermodehelper_read_unlock();
848 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200849 if (ret)
850 _request_firmware_cleanup(firmware_p);
851
852 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700853}
854
855/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800857 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800859void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
861 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800862 if (!fw_is_builtin_firmware(fw))
863 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 kfree(fw);
865 }
866}
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868/* Async support */
869struct firmware_work {
870 struct work_struct work;
871 struct module *module;
872 const char *name;
873 struct device *device;
874 void *context;
875 void (*cont)(const struct firmware *fw, void *context);
Bob Liu072fc8f2011-01-26 18:33:32 +0800876 bool uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877};
878
Stephen Boyda36cf842012-03-28 23:31:00 +0200879static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Stephen Boyda36cf842012-03-28 23:31:00 +0200881 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 const struct firmware *fw;
Stephen Boyddddb5542012-03-28 23:30:43 +0200883 struct firmware_priv *fw_priv;
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200884 long timeout;
matthieu castet113fab12005-11-13 16:07:39 -0800885 int ret;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700886
Stephen Boyda36cf842012-03-28 23:31:00 +0200887 fw_work = container_of(work, struct firmware_work, work);
Stephen Boyddddb5542012-03-28 23:30:43 +0200888 fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
889 fw_work->uevent, true);
890 if (IS_ERR_OR_NULL(fw_priv)) {
891 ret = PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200892 goto out;
Stephen Boyddddb5542012-03-28 23:30:43 +0200893 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200894
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200895 timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
896 if (timeout) {
Stephen Boyddddb5542012-03-28 23:30:43 +0200897 ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200898 usermodehelper_read_unlock();
899 } else {
900 dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
901 fw_work->name);
902 ret = -EAGAIN;
903 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200904 if (ret)
905 _request_firmware_cleanup(&fw);
906
907 out:
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100908 fw_work->cont(fw, fw_work->context);
Ming Lei0cfc1e12012-08-04 12:01:23 +0800909 put_device(fw_work->device);
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 module_put(fw_work->module);
912 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
915/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000916 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800917 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +0100918 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800919 * is non-zero else the firmware copy must be done manually.
920 * @name: name of firmware file
921 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100922 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800923 * @context: will be passed over to @cont, and
924 * @fw may be %NULL if firmware request fails.
925 * @cont: function will be called asynchronously when the firmware
926 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 *
Ming Lei0cfc1e12012-08-04 12:01:23 +0800928 * Caller must hold the reference count of @device.
929 *
Ming Lei6f21a622012-08-04 12:01:24 +0800930 * Asynchronous variant of request_firmware() for user contexts:
931 * - sleep for as small periods as possible since it may
932 * increase kernel boot time of built-in device drivers
933 * requesting firmware in their ->probe() methods, if
934 * @gfp is GFP_KERNEL.
935 *
936 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 **/
938int
939request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +0800940 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100941 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 void (*cont)(const struct firmware *fw, void *context))
943{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700944 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700946 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (!fw_work)
948 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700949
950 fw_work->module = module;
951 fw_work->name = name;
952 fw_work->device = device;
953 fw_work->context = context;
954 fw_work->cont = cont;
955 fw_work->uevent = uevent;
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (!try_module_get(module)) {
958 kfree(fw_work);
959 return -EFAULT;
960 }
961
Ming Lei0cfc1e12012-08-04 12:01:23 +0800962 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +0200963 INIT_WORK(&fw_work->work, request_firmware_work_func);
964 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return 0;
966}
967
Ming Lei2887b392012-08-04 12:01:22 +0800968/**
969 * cache_firmware - cache one firmware image in kernel memory space
970 * @fw_name: the firmware image name
971 *
972 * Cache firmware in kernel memory so that drivers can use it when
973 * system isn't ready for them to request firmware image from userspace.
974 * Once it returns successfully, driver can use request_firmware or its
975 * nowait version to get the cached firmware without any interacting
976 * with userspace
977 *
978 * Return 0 if the firmware image has been cached successfully
979 * Return !0 otherwise
980 *
981 */
982int cache_firmware(const char *fw_name)
983{
984 int ret;
985 const struct firmware *fw;
986
987 pr_debug("%s: %s\n", __func__, fw_name);
988
989 ret = request_firmware(&fw, fw_name, NULL);
990 if (!ret)
991 kfree(fw);
992
993 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
994
995 return ret;
996}
997
998/**
999 * uncache_firmware - remove one cached firmware image
1000 * @fw_name: the firmware image name
1001 *
1002 * Uncache one firmware image which has been cached successfully
1003 * before.
1004 *
1005 * Return 0 if the firmware cache has been removed successfully
1006 * Return !0 otherwise
1007 *
1008 */
1009int uncache_firmware(const char *fw_name)
1010{
1011 struct firmware_buf *buf;
1012 struct firmware fw;
1013
1014 pr_debug("%s: %s\n", __func__, fw_name);
1015
1016 if (fw_get_builtin_firmware(&fw, fw_name))
1017 return 0;
1018
1019 buf = fw_lookup_buf(fw_name);
1020 if (buf) {
1021 fw_free_buf(buf);
1022 return 0;
1023 }
1024
1025 return -EINVAL;
1026}
1027
Ming Lei37276a52012-08-04 12:01:27 +08001028static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1029{
1030 struct fw_cache_entry *fce;
1031
1032 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1033 if (!fce)
1034 goto exit;
1035
1036 strcpy(fce->name, name);
1037exit:
1038 return fce;
1039}
1040
1041static void free_fw_cache_entry(struct fw_cache_entry *fce)
1042{
1043 kfree(fce);
1044}
1045
1046static void __async_dev_cache_fw_image(void *fw_entry,
1047 async_cookie_t cookie)
1048{
1049 struct fw_cache_entry *fce = fw_entry;
1050 struct firmware_cache *fwc = &fw_cache;
1051 int ret;
1052
1053 ret = cache_firmware(fce->name);
1054 if (ret)
1055 goto free;
1056
1057 spin_lock(&fwc->name_lock);
1058 list_add(&fce->list, &fwc->fw_names);
1059 spin_unlock(&fwc->name_lock);
1060 goto drop_ref;
1061
1062free:
1063 free_fw_cache_entry(fce);
1064drop_ref:
1065 spin_lock(&fwc->name_lock);
1066 fwc->cnt--;
1067 spin_unlock(&fwc->name_lock);
1068
1069 wake_up(&fwc->wait_queue);
1070}
1071
1072/* called with dev->devres_lock held */
1073static void dev_create_fw_entry(struct device *dev, void *res,
1074 void *data)
1075{
1076 struct fw_name_devm *fwn = res;
1077 const char *fw_name = fwn->name;
1078 struct list_head *head = data;
1079 struct fw_cache_entry *fce;
1080
1081 fce = alloc_fw_cache_entry(fw_name);
1082 if (fce)
1083 list_add(&fce->list, head);
1084}
1085
1086static int devm_name_match(struct device *dev, void *res,
1087 void *match_data)
1088{
1089 struct fw_name_devm *fwn = res;
1090 return (fwn->magic == (unsigned long)match_data);
1091}
1092
1093static void dev_cache_fw_image(struct device *dev)
1094{
1095 LIST_HEAD(todo);
1096 struct fw_cache_entry *fce;
1097 struct fw_cache_entry *fce_next;
1098 struct firmware_cache *fwc = &fw_cache;
1099
1100 devres_for_each_res(dev, fw_name_devm_release,
1101 devm_name_match, &fw_cache,
1102 dev_create_fw_entry, &todo);
1103
1104 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1105 list_del(&fce->list);
1106
1107 spin_lock(&fwc->name_lock);
1108 fwc->cnt++;
1109 spin_unlock(&fwc->name_lock);
1110
1111 async_schedule(__async_dev_cache_fw_image, (void *)fce);
1112 }
1113}
1114
1115static void __device_uncache_fw_images(void)
1116{
1117 struct firmware_cache *fwc = &fw_cache;
1118 struct fw_cache_entry *fce;
1119
1120 spin_lock(&fwc->name_lock);
1121 while (!list_empty(&fwc->fw_names)) {
1122 fce = list_entry(fwc->fw_names.next,
1123 struct fw_cache_entry, list);
1124 list_del(&fce->list);
1125 spin_unlock(&fwc->name_lock);
1126
1127 uncache_firmware(fce->name);
1128 free_fw_cache_entry(fce);
1129
1130 spin_lock(&fwc->name_lock);
1131 }
1132 spin_unlock(&fwc->name_lock);
1133}
1134
1135/**
1136 * device_cache_fw_images - cache devices' firmware
1137 *
1138 * If one device called request_firmware or its nowait version
1139 * successfully before, the firmware names are recored into the
1140 * device's devres link list, so device_cache_fw_images can call
1141 * cache_firmware() to cache these firmwares for the device,
1142 * then the device driver can load its firmwares easily at
1143 * time when system is not ready to complete loading firmware.
1144 */
1145static void device_cache_fw_images(void)
1146{
1147 struct firmware_cache *fwc = &fw_cache;
1148 struct device *dev;
1149 DEFINE_WAIT(wait);
1150
1151 pr_debug("%s\n", __func__);
1152
1153 device_pm_lock();
1154 list_for_each_entry(dev, &dpm_list, power.entry)
1155 dev_cache_fw_image(dev);
1156 device_pm_unlock();
1157
1158 /* wait for completion of caching firmware for all devices */
1159 spin_lock(&fwc->name_lock);
1160 for (;;) {
1161 prepare_to_wait(&fwc->wait_queue, &wait,
1162 TASK_UNINTERRUPTIBLE);
1163 if (!fwc->cnt)
1164 break;
1165
1166 spin_unlock(&fwc->name_lock);
1167
1168 schedule();
1169
1170 spin_lock(&fwc->name_lock);
1171 }
1172 spin_unlock(&fwc->name_lock);
1173 finish_wait(&fwc->wait_queue, &wait);
1174}
1175
1176/**
1177 * device_uncache_fw_images - uncache devices' firmware
1178 *
1179 * uncache all firmwares which have been cached successfully
1180 * by device_uncache_fw_images earlier
1181 */
1182static void device_uncache_fw_images(void)
1183{
1184 pr_debug("%s\n", __func__);
1185 __device_uncache_fw_images();
1186}
1187
1188static void device_uncache_fw_images_work(struct work_struct *work)
1189{
1190 device_uncache_fw_images();
1191}
1192
1193/**
1194 * device_uncache_fw_images_delay - uncache devices firmwares
1195 * @delay: number of milliseconds to delay uncache device firmwares
1196 *
1197 * uncache all devices's firmwares which has been cached successfully
1198 * by device_cache_fw_images after @delay milliseconds.
1199 */
1200static void device_uncache_fw_images_delay(unsigned long delay)
1201{
1202 schedule_delayed_work(&fw_cache.work,
1203 msecs_to_jiffies(delay));
1204}
1205
1206static void __init fw_cache_init(void)
1207{
1208 spin_lock_init(&fw_cache.lock);
1209 INIT_LIST_HEAD(&fw_cache.head);
1210
1211 spin_lock_init(&fw_cache.name_lock);
1212 INIT_LIST_HEAD(&fw_cache.fw_names);
1213 fw_cache.cnt = 0;
1214
1215 init_waitqueue_head(&fw_cache.wait_queue);
1216 INIT_DELAYED_WORK(&fw_cache.work,
1217 device_uncache_fw_images_work);
1218}
1219
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001220static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Ming Lei1f2b7952012-08-04 12:01:21 +08001222 fw_cache_init();
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001223 return class_register(&firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001225
1226static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
1228 class_unregister(&firmware_class);
1229}
1230
Shaohua Lia30a6a22006-09-27 01:50:52 -07001231fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232module_exit(firmware_class_exit);
1233
1234EXPORT_SYMBOL(release_firmware);
1235EXPORT_SYMBOL(request_firmware);
1236EXPORT_SYMBOL(request_firmware_nowait);
Ming Lei2887b392012-08-04 12:01:22 +08001237EXPORT_SYMBOL_GPL(cache_firmware);
1238EXPORT_SYMBOL_GPL(uncache_firmware);