blob: 4c8d8efecdf41709d85bfc6f6e56fc0db9c8538d [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"
30#include "power/power.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Markus Rechberger87d37a42007-06-04 18:45:44 +020032MODULE_AUTHOR("Manuel Estrada Sainz");
Linus Torvalds1da177e2005-04-16 15:20:36 -070033MODULE_DESCRIPTION("Multi purpose firmware loading support");
34MODULE_LICENSE("GPL");
35
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080036/* Builtin firmware support */
37
38#ifdef CONFIG_FW_LOADER
39
40extern struct builtin_fw __start_builtin_fw[];
41extern struct builtin_fw __end_builtin_fw[];
42
43static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
44{
45 struct builtin_fw *b_fw;
46
47 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
48 if (strcmp(name, b_fw->name) == 0) {
49 fw->size = b_fw->size;
50 fw->data = b_fw->data;
51 return true;
52 }
53 }
54
55 return false;
56}
57
58static bool fw_is_builtin_firmware(const struct firmware *fw)
59{
60 struct builtin_fw *b_fw;
61
62 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
63 if (fw->data == b_fw->data)
64 return true;
65
66 return false;
67}
68
69#else /* Module case - no builtin firmware support */
70
71static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
72{
73 return false;
74}
75
76static inline bool fw_is_builtin_firmware(const struct firmware *fw)
77{
78 return false;
79}
80#endif
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082enum {
83 FW_STATUS_LOADING,
84 FW_STATUS_DONE,
85 FW_STATUS_ABORT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070086};
87
Dave Jones2f651682007-01-25 15:56:15 -050088static int loading_timeout = 60; /* In seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +020090static inline long firmware_loading_timeout(void)
91{
92 return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
93}
94
Ming Lei1f2b7952012-08-04 12:01:21 +080095struct firmware_cache {
96 /* firmware_buf instance will be added into the below list */
97 spinlock_t lock;
98 struct list_head head;
Ming Lei37276a52012-08-04 12:01:27 +080099
100 /*
101 * Names of firmware images which have been cached successfully
102 * will be added into the below list so that device uncache
103 * helper can trace which firmware images have been cached
104 * before.
105 */
106 spinlock_t name_lock;
107 struct list_head fw_names;
108
109 wait_queue_head_t wait_queue;
110 int cnt;
111 struct delayed_work work;
Ming Lei07646d92012-08-04 12:01:29 +0800112
113 struct notifier_block pm_notify;
Ming Lei1f2b7952012-08-04 12:01:21 +0800114};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Ming Lei12446912012-08-04 12:01:20 +0800116struct firmware_buf {
Ming Lei1f2b7952012-08-04 12:01:21 +0800117 struct kref ref;
118 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 struct completion completion;
Ming Lei1f2b7952012-08-04 12:01:21 +0800120 struct firmware_cache *fwc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 unsigned long status;
Ming Lei65710cb2012-08-04 12:01:16 +0800122 void *data;
123 size_t size;
David Woodhouse6e03a202009-04-09 22:04:07 -0700124 struct page **pages;
125 int nr_pages;
126 int page_array_size;
Dmitry Torokhove1771232010-03-13 23:49:23 -0800127 char fw_id[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
Ming Lei37276a52012-08-04 12:01:27 +0800130struct fw_cache_entry {
131 struct list_head list;
132 char name[];
133};
134
Ming Lei12446912012-08-04 12:01:20 +0800135struct firmware_priv {
136 struct timer_list timeout;
137 bool nowait;
138 struct device dev;
139 struct firmware_buf *buf;
Ming Lei1f2b7952012-08-04 12:01:21 +0800140 struct firmware *fw;
Ming Lei12446912012-08-04 12:01:20 +0800141};
142
Ming Leif531f05a2012-08-04 12:01:25 +0800143struct fw_name_devm {
144 unsigned long magic;
145 char name[];
146};
147
Ming Lei1f2b7952012-08-04 12:01:21 +0800148#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
149
150/* fw_lock could be moved to 'struct firmware_priv' but since it is just
151 * guarding for corner cases a global lock should be OK */
152static DEFINE_MUTEX(fw_lock);
153
154static struct firmware_cache fw_cache;
155
156static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
157 struct firmware_cache *fwc)
158{
159 struct firmware_buf *buf;
160
161 buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
162
163 if (!buf)
164 return buf;
165
166 kref_init(&buf->ref);
167 strcpy(buf->fw_id, fw_name);
168 buf->fwc = fwc;
169 init_completion(&buf->completion);
170
171 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
172
173 return buf;
174}
175
Ming Lei2887b392012-08-04 12:01:22 +0800176static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
177{
178 struct firmware_buf *tmp;
179 struct firmware_cache *fwc = &fw_cache;
180
181 list_for_each_entry(tmp, &fwc->head, list)
182 if (!strcmp(tmp->fw_id, fw_name))
183 return tmp;
184 return NULL;
185}
186
Ming Lei1f2b7952012-08-04 12:01:21 +0800187static int fw_lookup_and_allocate_buf(const char *fw_name,
188 struct firmware_cache *fwc,
189 struct firmware_buf **buf)
190{
191 struct firmware_buf *tmp;
192
193 spin_lock(&fwc->lock);
Ming Lei2887b392012-08-04 12:01:22 +0800194 tmp = __fw_lookup_buf(fw_name);
195 if (tmp) {
196 kref_get(&tmp->ref);
197 spin_unlock(&fwc->lock);
198 *buf = tmp;
199 return 1;
200 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800201 tmp = __allocate_fw_buf(fw_name, fwc);
202 if (tmp)
203 list_add(&tmp->list, &fwc->head);
204 spin_unlock(&fwc->lock);
205
206 *buf = tmp;
207
208 return tmp ? 0 : -ENOMEM;
209}
210
Ming Lei2887b392012-08-04 12:01:22 +0800211static struct firmware_buf *fw_lookup_buf(const char *fw_name)
212{
213 struct firmware_buf *tmp;
214 struct firmware_cache *fwc = &fw_cache;
215
216 spin_lock(&fwc->lock);
217 tmp = __fw_lookup_buf(fw_name);
218 spin_unlock(&fwc->lock);
219
220 return tmp;
221}
222
Ming Lei1f2b7952012-08-04 12:01:21 +0800223static void __fw_free_buf(struct kref *ref)
224{
225 struct firmware_buf *buf = to_fwbuf(ref);
226 struct firmware_cache *fwc = buf->fwc;
227 int i;
228
229 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
230 __func__, buf->fw_id, buf, buf->data,
231 (unsigned int)buf->size);
232
233 spin_lock(&fwc->lock);
234 list_del(&buf->list);
235 spin_unlock(&fwc->lock);
236
237 vunmap(buf->data);
238 for (i = 0; i < buf->nr_pages; i++)
239 __free_page(buf->pages[i]);
240 kfree(buf->pages);
241 kfree(buf);
242}
243
244static void fw_free_buf(struct firmware_buf *buf)
245{
246 kref_put(&buf->ref, __fw_free_buf);
247}
248
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700249static struct firmware_priv *to_firmware_priv(struct device *dev)
250{
251 return container_of(dev, struct firmware_priv, dev);
252}
253
254static void fw_load_abort(struct firmware_priv *fw_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Ming Lei12446912012-08-04 12:01:20 +0800256 struct firmware_buf *buf = fw_priv->buf;
257
258 set_bit(FW_STATUS_ABORT, &buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800259 complete_all(&buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700262static ssize_t firmware_timeout_show(struct class *class,
263 struct class_attribute *attr,
264 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 return sprintf(buf, "%d\n", loading_timeout);
267}
268
269/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800270 * firmware_timeout_store - set number of seconds to wait for firmware
271 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800272 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800273 * @buf: buffer to scan for timeout value
274 * @count: number of bytes in @buf
275 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800277 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 * firmware will be provided.
279 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800280 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700282static ssize_t firmware_timeout_store(struct class *class,
283 struct class_attribute *attr,
284 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700287 if (loading_timeout < 0)
288 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return count;
291}
292
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800293static struct class_attribute firmware_class_attrs[] = {
294 __ATTR(timeout, S_IWUSR | S_IRUGO,
295 firmware_timeout_show, firmware_timeout_store),
296 __ATTR_NULL
297};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800299static void fw_dev_release(struct device *dev)
300{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700301 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800302
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800303 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800304
305 module_put(THIS_MODULE);
306}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Kay Sievers7eff2e72007-08-14 15:15:12 +0200308static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700310 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Ming Lei12446912012-08-04 12:01:20 +0800312 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200314 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700315 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200316 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
317 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 return 0;
320}
321
Adrian Bunk1b81d662006-05-20 15:00:16 -0700322static struct class firmware_class = {
323 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800324 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700325 .dev_uevent = firmware_uevent,
326 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700327};
328
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700329static ssize_t firmware_loading_show(struct device *dev,
330 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700332 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800333 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return sprintf(buf, "%d\n", loading);
336}
337
Ming Lei65710cb2012-08-04 12:01:16 +0800338/* firmware holds the ownership of pages */
David Woodhousedd336c52010-05-02 11:21:21 +0300339static void firmware_free_data(const struct firmware *fw)
340{
Ming Lei1f2b7952012-08-04 12:01:21 +0800341 WARN_ON(!fw->priv);
342 fw_free_buf(fw->priv);
David Woodhousedd336c52010-05-02 11:21:21 +0300343}
344
David Woodhouse6e03a202009-04-09 22:04:07 -0700345/* Some architectures don't have PAGE_KERNEL_RO */
346#ifndef PAGE_KERNEL_RO
347#define PAGE_KERNEL_RO PAGE_KERNEL
348#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800350 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700351 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800352 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800353 * @buf: buffer to scan for loading control value
354 * @count: number of bytes in @buf
355 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 * The relevant values are:
357 *
358 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800359 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 * -1: Conclude the load with an error and discard any written data.
361 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700362static ssize_t firmware_loading_store(struct device *dev,
363 struct device_attribute *attr,
364 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700366 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800367 struct firmware_buf *fw_buf = fw_priv->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700369 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Neil Hormaneea915b2012-01-02 15:31:23 -0500371 mutex_lock(&fw_lock);
372
Ming Lei12446912012-08-04 12:01:20 +0800373 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500374 goto out;
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 switch (loading) {
377 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800378 /* discarding any previous partial load */
Ming Lei12446912012-08-04 12:01:20 +0800379 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
380 for (i = 0; i < fw_buf->nr_pages; i++)
381 __free_page(fw_buf->pages[i]);
382 kfree(fw_buf->pages);
383 fw_buf->pages = NULL;
384 fw_buf->page_array_size = 0;
385 fw_buf->nr_pages = 0;
386 set_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei28eefa72012-08-04 12:01:17 +0800387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 break;
389 case 0:
Ming Lei12446912012-08-04 12:01:20 +0800390 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
391 set_bit(FW_STATUS_DONE, &fw_buf->status);
392 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800393 complete_all(&fw_buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 break;
395 }
396 /* fallthrough */
397 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700398 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 /* fallthrough */
400 case -1:
401 fw_load_abort(fw_priv);
402 break;
403 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500404out:
405 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return count;
407}
408
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700409static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700411static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
412 struct bin_attribute *bin_attr,
413 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200415 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700416 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800417 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700418 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Laura Garciacad1e552006-05-23 23:22:38 +0200420 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800421 buf = fw_priv->buf;
422 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 ret_count = -ENODEV;
424 goto out;
425 }
Ming Lei12446912012-08-04 12:01:20 +0800426 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200427 ret_count = 0;
428 goto out;
429 }
Ming Lei12446912012-08-04 12:01:20 +0800430 if (count > buf->size - offset)
431 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700432
433 ret_count = count;
434
435 while (count) {
436 void *page_data;
437 int page_nr = offset >> PAGE_SHIFT;
438 int page_ofs = offset & (PAGE_SIZE-1);
439 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
440
Ming Lei12446912012-08-04 12:01:20 +0800441 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700442
443 memcpy(buffer, page_data + page_ofs, page_cnt);
444
Ming Lei12446912012-08-04 12:01:20 +0800445 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700446 buffer += page_cnt;
447 offset += page_cnt;
448 count -= page_cnt;
449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450out:
Laura Garciacad1e552006-05-23 23:22:38 +0200451 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return ret_count;
453}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800454
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700455static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Ming Lei12446912012-08-04 12:01:20 +0800457 struct firmware_buf *buf = fw_priv->buf;
David Woodhouse6e03a202009-04-09 22:04:07 -0700458 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
David Woodhouse6e03a202009-04-09 22:04:07 -0700460 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800461 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700462 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800463 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700464 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
David Woodhouse6e03a202009-04-09 22:04:07 -0700466 new_pages = kmalloc(new_array_size * sizeof(void *),
467 GFP_KERNEL);
468 if (!new_pages) {
469 fw_load_abort(fw_priv);
470 return -ENOMEM;
471 }
Ming Lei12446912012-08-04 12:01:20 +0800472 memcpy(new_pages, buf->pages,
473 buf->page_array_size * sizeof(void *));
474 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
475 (new_array_size - buf->page_array_size));
476 kfree(buf->pages);
477 buf->pages = new_pages;
478 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700480
Ming Lei12446912012-08-04 12:01:20 +0800481 while (buf->nr_pages < pages_needed) {
482 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700483 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
484
Ming Lei12446912012-08-04 12:01:20 +0800485 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700486 fw_load_abort(fw_priv);
487 return -ENOMEM;
488 }
Ming Lei12446912012-08-04 12:01:20 +0800489 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return 0;
492}
493
494/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800495 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700496 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700497 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700498 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800499 * @buffer: buffer being written
500 * @offset: buffer offset for write in total data store area
501 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800503 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 * the driver as a firmware image.
505 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700506static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
507 struct bin_attribute *bin_attr,
508 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200510 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700511 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800512 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 ssize_t retval;
514
515 if (!capable(CAP_SYS_RAWIO))
516 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700517
Laura Garciacad1e552006-05-23 23:22:38 +0200518 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800519 buf = fw_priv->buf;
520 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 retval = -ENODEV;
522 goto out;
523 }
Ming Lei65710cb2012-08-04 12:01:16 +0800524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 retval = fw_realloc_buffer(fw_priv, offset + count);
526 if (retval)
527 goto out;
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700530
531 while (count) {
532 void *page_data;
533 int page_nr = offset >> PAGE_SHIFT;
534 int page_ofs = offset & (PAGE_SIZE - 1);
535 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
536
Ming Lei12446912012-08-04 12:01:20 +0800537 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700538
539 memcpy(page_data + page_ofs, buffer, page_cnt);
540
Ming Lei12446912012-08-04 12:01:20 +0800541 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700542 buffer += page_cnt;
543 offset += page_cnt;
544 count -= page_cnt;
545 }
546
Ming Lei12446912012-08-04 12:01:20 +0800547 buf->size = max_t(size_t, offset, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548out:
Laura Garciacad1e552006-05-23 23:22:38 +0200549 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return retval;
551}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800552
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700553static struct bin_attribute firmware_attr_data = {
554 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 .size = 0,
556 .read = firmware_data_read,
557 .write = firmware_data_write,
558};
559
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700560static void firmware_class_timeout(u_long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 fw_load_abort(fw_priv);
565}
566
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700567static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200568fw_create_instance(struct firmware *firmware, const char *fw_name,
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700569 struct device *device, bool uevent, bool nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700571 struct firmware_priv *fw_priv;
572 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Ming Lei12446912012-08-04 12:01:20 +0800574 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700575 if (!fw_priv) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700576 dev_err(device, "%s: kmalloc failed\n", __func__);
Ming Lei12446912012-08-04 12:01:20 +0800577 fw_priv = ERR_PTR(-ENOMEM);
578 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700581 fw_priv->nowait = nowait;
Ming Lei1f2b7952012-08-04 12:01:21 +0800582 fw_priv->fw = firmware;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700583 setup_timer(&fw_priv->timeout,
584 firmware_class_timeout, (u_long) fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700586 f_dev = &fw_priv->dev;
587
588 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800589 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700590 f_dev->parent = device;
591 f_dev->class = &firmware_class;
Ming Lei12446912012-08-04 12:01:20 +0800592exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700593 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
Ming Lei1f2b7952012-08-04 12:01:21 +0800596/* one pages buffer is mapped/unmapped only once */
597static int fw_map_pages_buf(struct firmware_buf *buf)
598{
599 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
600 if (!buf->data)
601 return -ENOMEM;
602 return 0;
603}
604
605/* store the pages buffer info firmware from buf */
606static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
607{
608 fw->priv = buf;
609 fw->pages = buf->pages;
610 fw->size = buf->size;
611 fw->data = buf->data;
612
613 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
614 __func__, buf->fw_id, buf, buf->data,
615 (unsigned int)buf->size);
616}
617
Ming Leif531f05a2012-08-04 12:01:25 +0800618static void fw_name_devm_release(struct device *dev, void *res)
619{
620 struct fw_name_devm *fwn = res;
621
622 if (fwn->magic == (unsigned long)&fw_cache)
623 pr_debug("%s: fw_name-%s devm-%p released\n",
624 __func__, fwn->name, res);
625}
626
627static int fw_devm_match(struct device *dev, void *res,
628 void *match_data)
629{
630 struct fw_name_devm *fwn = res;
631
632 return (fwn->magic == (unsigned long)&fw_cache) &&
633 !strcmp(fwn->name, match_data);
634}
635
636static struct fw_name_devm *fw_find_devm_name(struct device *dev,
637 const char *name)
638{
639 struct fw_name_devm *fwn;
640
641 fwn = devres_find(dev, fw_name_devm_release,
642 fw_devm_match, (void *)name);
643 return fwn;
644}
645
646/* add firmware name into devres list */
647static int fw_add_devm_name(struct device *dev, const char *name)
648{
649 struct fw_name_devm *fwn;
650
651 fwn = fw_find_devm_name(dev, name);
652 if (fwn)
653 return 1;
654
655 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
656 strlen(name) + 1, GFP_KERNEL);
657 if (!fwn)
658 return -ENOMEM;
659
660 fwn->magic = (unsigned long)&fw_cache;
661 strcpy(fwn->name, name);
662 devres_add(dev, fwn);
663
664 return 0;
665}
666
Ming Lei1f2b7952012-08-04 12:01:21 +0800667static void _request_firmware_cleanup(const struct firmware **firmware_p)
668{
669 release_firmware(*firmware_p);
670 *firmware_p = NULL;
671}
672
Stephen Boyddddb5542012-03-28 23:30:43 +0200673static struct firmware_priv *
674_request_firmware_prepare(const struct firmware **firmware_p, const char *name,
675 struct device *device, bool uevent, bool nowait)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700676{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +0800678 struct firmware_priv *fw_priv = NULL;
679 struct firmware_buf *buf;
680 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 if (!firmware_p)
Stephen Boyddddb5542012-03-28 23:30:43 +0200683 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Jiri Slaby4aed0642005-09-13 01:25:01 -0700685 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700687 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
688 __func__);
Stephen Boyddddb5542012-03-28 23:30:43 +0200689 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800692 if (fw_get_builtin_firmware(firmware, name)) {
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100693 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
Stephen Boyddddb5542012-03-28 23:30:43 +0200694 return NULL;
David Woodhouse5658c762008-05-23 13:52:42 +0100695 }
696
Ming Lei1f2b7952012-08-04 12:01:21 +0800697 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
698 if (!ret)
699 fw_priv = fw_create_instance(firmware, name, device,
700 uevent, nowait);
701
702 if (IS_ERR(fw_priv) || ret < 0) {
703 kfree(firmware);
Stephen Boyddddb5542012-03-28 23:30:43 +0200704 *firmware_p = NULL;
Ming Lei1f2b7952012-08-04 12:01:21 +0800705 return ERR_PTR(-ENOMEM);
706 } else if (fw_priv) {
707 fw_priv->buf = buf;
708
709 /*
710 * bind with 'buf' now to avoid warning in failure path
711 * of requesting firmware.
712 */
713 firmware->priv = buf;
714 return fw_priv;
Stephen Boyddddb5542012-03-28 23:30:43 +0200715 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800716
717 /* share the cached buf, which is inprogessing or completed */
718 check_status:
719 mutex_lock(&fw_lock);
720 if (test_bit(FW_STATUS_ABORT, &buf->status)) {
721 fw_priv = ERR_PTR(-ENOENT);
722 _request_firmware_cleanup(firmware_p);
723 goto exit;
724 } else if (test_bit(FW_STATUS_DONE, &buf->status)) {
725 fw_priv = NULL;
726 fw_set_page_data(buf, firmware);
727 goto exit;
728 }
729 mutex_unlock(&fw_lock);
730 wait_for_completion(&buf->completion);
731 goto check_status;
732
733exit:
734 mutex_unlock(&fw_lock);
Stephen Boyddddb5542012-03-28 23:30:43 +0200735 return fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200736}
737
Stephen Boyddddb5542012-03-28 23:30:43 +0200738static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
739 long timeout)
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200740{
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200741 int retval = 0;
Stephen Boyddddb5542012-03-28 23:30:43 +0200742 struct device *f_dev = &fw_priv->dev;
Ming Lei12446912012-08-04 12:01:20 +0800743 struct firmware_buf *buf = fw_priv->buf;
Linus Torvaldscaca9512011-08-24 15:55:30 -0700744
Stephen Boyddddb5542012-03-28 23:30:43 +0200745 dev_set_uevent_suppress(f_dev, true);
David Woodhouse5658c762008-05-23 13:52:42 +0100746
Stephen Boyddddb5542012-03-28 23:30:43 +0200747 /* Need to pin this module until class device is destroyed */
748 __module_get(THIS_MODULE);
749
750 retval = device_add(f_dev);
751 if (retval) {
752 dev_err(f_dev, "%s: device_register failed\n", __func__);
753 goto err_put_dev;
754 }
755
756 retval = device_create_bin_file(f_dev, &firmware_attr_data);
757 if (retval) {
758 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
759 goto err_del_dev;
760 }
761
762 retval = device_create_file(f_dev, &dev_attr_loading);
763 if (retval) {
764 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
765 goto err_del_bin_attr;
766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Kay Sievers312c0042005-11-16 09:00:00 +0100768 if (uevent) {
Stephen Boyddddb5542012-03-28 23:30:43 +0200769 dev_set_uevent_suppress(f_dev, false);
Ming Lei12446912012-08-04 12:01:20 +0800770 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200771 if (timeout != MAX_SCHEDULE_TIMEOUT)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700772 mod_timer(&fw_priv->timeout,
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200773 round_jiffies_up(jiffies + timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700775 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
776 }
777
Ming Lei12446912012-08-04 12:01:20 +0800778 wait_for_completion(&buf->completion);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700779
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700780 del_timer_sync(&fw_priv->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Laura Garciacad1e552006-05-23 23:22:38 +0200782 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800783 if (!buf->size || test_bit(FW_STATUS_ABORT, &buf->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 retval = -ENOENT;
Ming Lei65710cb2012-08-04 12:01:16 +0800785
Ming Leif531f05a2012-08-04 12:01:25 +0800786 /*
787 * add firmware name into devres list so that we can auto cache
788 * and uncache firmware for device.
789 *
790 * f_dev->parent may has been deleted already, but the problem
791 * should be fixed in devres or driver core.
792 */
793 if (!retval && f_dev->parent)
794 fw_add_devm_name(f_dev->parent, buf->fw_id);
795
Ming Lei65710cb2012-08-04 12:01:16 +0800796 if (!retval)
Ming Lei1f2b7952012-08-04 12:01:21 +0800797 retval = fw_map_pages_buf(buf);
Ming Lei12446912012-08-04 12:01:20 +0800798
Ming Lei1f2b7952012-08-04 12:01:21 +0800799 /* pass the pages buffer to driver at the last minute */
800 fw_set_page_data(buf, fw_priv->fw);
801
Ming Lei12446912012-08-04 12:01:20 +0800802 fw_priv->buf = NULL;
Laura Garciacad1e552006-05-23 23:22:38 +0200803 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Stephen Boyddddb5542012-03-28 23:30:43 +0200805 device_remove_file(f_dev, &dev_attr_loading);
806err_del_bin_attr:
807 device_remove_bin_file(f_dev, &firmware_attr_data);
808err_del_dev:
809 device_del(f_dev);
810err_put_dev:
811 put_device(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return retval;
813}
814
815/**
Kay Sievers312c0042005-11-16 09:00:00 +0100816 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800817 * @firmware_p: pointer to firmware image
818 * @name: name of firmware file
819 * @device: device for which firmware is being loaded
820 *
821 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700822 * of @name for device @device.
823 *
824 * Should be called from user context where sleeping is allowed.
825 *
Kay Sievers312c0042005-11-16 09:00:00 +0100826 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700827 * should be distinctive enough not to be confused with any other
828 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +0800829 *
830 * Caller must hold the reference count of @device.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700831 **/
832int
833request_firmware(const struct firmware **firmware_p, const char *name,
834 struct device *device)
835{
Stephen Boyddddb5542012-03-28 23:30:43 +0200836 struct firmware_priv *fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200837 int ret;
838
Stephen Boyddddb5542012-03-28 23:30:43 +0200839 fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
840 false);
841 if (IS_ERR_OR_NULL(fw_priv))
842 return PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200843
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200844 ret = usermodehelper_read_trylock();
845 if (WARN_ON(ret)) {
846 dev_err(device, "firmware: %s will not be loaded\n", name);
847 } else {
Stephen Boyddddb5542012-03-28 23:30:43 +0200848 ret = _request_firmware_load(fw_priv, true,
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200849 firmware_loading_timeout());
850 usermodehelper_read_unlock();
851 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200852 if (ret)
853 _request_firmware_cleanup(firmware_p);
854
855 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700856}
857
858/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800860 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800862void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
864 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800865 if (!fw_is_builtin_firmware(fw))
866 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 kfree(fw);
868 }
869}
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871/* Async support */
872struct firmware_work {
873 struct work_struct work;
874 struct module *module;
875 const char *name;
876 struct device *device;
877 void *context;
878 void (*cont)(const struct firmware *fw, void *context);
Bob Liu072fc8f2011-01-26 18:33:32 +0800879 bool uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880};
881
Stephen Boyda36cf842012-03-28 23:31:00 +0200882static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Stephen Boyda36cf842012-03-28 23:31:00 +0200884 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 const struct firmware *fw;
Stephen Boyddddb5542012-03-28 23:30:43 +0200886 struct firmware_priv *fw_priv;
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200887 long timeout;
matthieu castet113fab12005-11-13 16:07:39 -0800888 int ret;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700889
Stephen Boyda36cf842012-03-28 23:31:00 +0200890 fw_work = container_of(work, struct firmware_work, work);
Stephen Boyddddb5542012-03-28 23:30:43 +0200891 fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
892 fw_work->uevent, true);
893 if (IS_ERR_OR_NULL(fw_priv)) {
894 ret = PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200895 goto out;
Stephen Boyddddb5542012-03-28 23:30:43 +0200896 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200897
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200898 timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
899 if (timeout) {
Stephen Boyddddb5542012-03-28 23:30:43 +0200900 ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200901 usermodehelper_read_unlock();
902 } else {
903 dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
904 fw_work->name);
905 ret = -EAGAIN;
906 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200907 if (ret)
908 _request_firmware_cleanup(&fw);
909
910 out:
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100911 fw_work->cont(fw, fw_work->context);
Ming Lei0cfc1e12012-08-04 12:01:23 +0800912 put_device(fw_work->device);
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 module_put(fw_work->module);
915 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
918/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000919 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800920 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +0100921 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800922 * is non-zero else the firmware copy must be done manually.
923 * @name: name of firmware file
924 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100925 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800926 * @context: will be passed over to @cont, and
927 * @fw may be %NULL if firmware request fails.
928 * @cont: function will be called asynchronously when the firmware
929 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 *
Ming Lei0cfc1e12012-08-04 12:01:23 +0800931 * Caller must hold the reference count of @device.
932 *
Ming Lei6f21a622012-08-04 12:01:24 +0800933 * Asynchronous variant of request_firmware() for user contexts:
934 * - sleep for as small periods as possible since it may
935 * increase kernel boot time of built-in device drivers
936 * requesting firmware in their ->probe() methods, if
937 * @gfp is GFP_KERNEL.
938 *
939 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 **/
941int
942request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +0800943 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100944 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 void (*cont)(const struct firmware *fw, void *context))
946{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700947 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700949 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (!fw_work)
951 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700952
953 fw_work->module = module;
954 fw_work->name = name;
955 fw_work->device = device;
956 fw_work->context = context;
957 fw_work->cont = cont;
958 fw_work->uevent = uevent;
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (!try_module_get(module)) {
961 kfree(fw_work);
962 return -EFAULT;
963 }
964
Ming Lei0cfc1e12012-08-04 12:01:23 +0800965 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +0200966 INIT_WORK(&fw_work->work, request_firmware_work_func);
967 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return 0;
969}
970
Ming Lei2887b392012-08-04 12:01:22 +0800971/**
972 * cache_firmware - cache one firmware image in kernel memory space
973 * @fw_name: the firmware image name
974 *
975 * Cache firmware in kernel memory so that drivers can use it when
976 * system isn't ready for them to request firmware image from userspace.
977 * Once it returns successfully, driver can use request_firmware or its
978 * nowait version to get the cached firmware without any interacting
979 * with userspace
980 *
981 * Return 0 if the firmware image has been cached successfully
982 * Return !0 otherwise
983 *
984 */
985int cache_firmware(const char *fw_name)
986{
987 int ret;
988 const struct firmware *fw;
989
990 pr_debug("%s: %s\n", __func__, fw_name);
991
992 ret = request_firmware(&fw, fw_name, NULL);
993 if (!ret)
994 kfree(fw);
995
996 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
997
998 return ret;
999}
1000
1001/**
1002 * uncache_firmware - remove one cached firmware image
1003 * @fw_name: the firmware image name
1004 *
1005 * Uncache one firmware image which has been cached successfully
1006 * before.
1007 *
1008 * Return 0 if the firmware cache has been removed successfully
1009 * Return !0 otherwise
1010 *
1011 */
1012int uncache_firmware(const char *fw_name)
1013{
1014 struct firmware_buf *buf;
1015 struct firmware fw;
1016
1017 pr_debug("%s: %s\n", __func__, fw_name);
1018
1019 if (fw_get_builtin_firmware(&fw, fw_name))
1020 return 0;
1021
1022 buf = fw_lookup_buf(fw_name);
1023 if (buf) {
1024 fw_free_buf(buf);
1025 return 0;
1026 }
1027
1028 return -EINVAL;
1029}
1030
Ming Lei37276a52012-08-04 12:01:27 +08001031static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1032{
1033 struct fw_cache_entry *fce;
1034
1035 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1036 if (!fce)
1037 goto exit;
1038
1039 strcpy(fce->name, name);
1040exit:
1041 return fce;
1042}
1043
1044static void free_fw_cache_entry(struct fw_cache_entry *fce)
1045{
1046 kfree(fce);
1047}
1048
1049static void __async_dev_cache_fw_image(void *fw_entry,
1050 async_cookie_t cookie)
1051{
1052 struct fw_cache_entry *fce = fw_entry;
1053 struct firmware_cache *fwc = &fw_cache;
1054 int ret;
1055
1056 ret = cache_firmware(fce->name);
1057 if (ret)
1058 goto free;
1059
1060 spin_lock(&fwc->name_lock);
1061 list_add(&fce->list, &fwc->fw_names);
1062 spin_unlock(&fwc->name_lock);
1063 goto drop_ref;
1064
1065free:
1066 free_fw_cache_entry(fce);
1067drop_ref:
1068 spin_lock(&fwc->name_lock);
1069 fwc->cnt--;
1070 spin_unlock(&fwc->name_lock);
1071
1072 wake_up(&fwc->wait_queue);
1073}
1074
1075/* called with dev->devres_lock held */
1076static void dev_create_fw_entry(struct device *dev, void *res,
1077 void *data)
1078{
1079 struct fw_name_devm *fwn = res;
1080 const char *fw_name = fwn->name;
1081 struct list_head *head = data;
1082 struct fw_cache_entry *fce;
1083
1084 fce = alloc_fw_cache_entry(fw_name);
1085 if (fce)
1086 list_add(&fce->list, head);
1087}
1088
1089static int devm_name_match(struct device *dev, void *res,
1090 void *match_data)
1091{
1092 struct fw_name_devm *fwn = res;
1093 return (fwn->magic == (unsigned long)match_data);
1094}
1095
1096static void dev_cache_fw_image(struct device *dev)
1097{
1098 LIST_HEAD(todo);
1099 struct fw_cache_entry *fce;
1100 struct fw_cache_entry *fce_next;
1101 struct firmware_cache *fwc = &fw_cache;
1102
1103 devres_for_each_res(dev, fw_name_devm_release,
1104 devm_name_match, &fw_cache,
1105 dev_create_fw_entry, &todo);
1106
1107 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1108 list_del(&fce->list);
1109
1110 spin_lock(&fwc->name_lock);
1111 fwc->cnt++;
1112 spin_unlock(&fwc->name_lock);
1113
1114 async_schedule(__async_dev_cache_fw_image, (void *)fce);
1115 }
1116}
1117
1118static void __device_uncache_fw_images(void)
1119{
1120 struct firmware_cache *fwc = &fw_cache;
1121 struct fw_cache_entry *fce;
1122
1123 spin_lock(&fwc->name_lock);
1124 while (!list_empty(&fwc->fw_names)) {
1125 fce = list_entry(fwc->fw_names.next,
1126 struct fw_cache_entry, list);
1127 list_del(&fce->list);
1128 spin_unlock(&fwc->name_lock);
1129
1130 uncache_firmware(fce->name);
1131 free_fw_cache_entry(fce);
1132
1133 spin_lock(&fwc->name_lock);
1134 }
1135 spin_unlock(&fwc->name_lock);
1136}
1137
1138/**
1139 * device_cache_fw_images - cache devices' firmware
1140 *
1141 * If one device called request_firmware or its nowait version
1142 * successfully before, the firmware names are recored into the
1143 * device's devres link list, so device_cache_fw_images can call
1144 * cache_firmware() to cache these firmwares for the device,
1145 * then the device driver can load its firmwares easily at
1146 * time when system is not ready to complete loading firmware.
1147 */
1148static void device_cache_fw_images(void)
1149{
1150 struct firmware_cache *fwc = &fw_cache;
1151 struct device *dev;
Ming Leiffe53f62012-08-04 12:01:28 +08001152 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001153 DEFINE_WAIT(wait);
1154
1155 pr_debug("%s\n", __func__);
1156
Ming Leiffe53f62012-08-04 12:01:28 +08001157 /*
1158 * use small loading timeout for caching devices' firmware
1159 * because all these firmware images have been loaded
1160 * successfully at lease once, also system is ready for
1161 * completing firmware loading now. The maximum size of
1162 * firmware in current distributions is about 2M bytes,
1163 * so 10 secs should be enough.
1164 */
1165 old_timeout = loading_timeout;
1166 loading_timeout = 10;
1167
Ming Lei37276a52012-08-04 12:01:27 +08001168 device_pm_lock();
1169 list_for_each_entry(dev, &dpm_list, power.entry)
1170 dev_cache_fw_image(dev);
1171 device_pm_unlock();
1172
1173 /* wait for completion of caching firmware for all devices */
1174 spin_lock(&fwc->name_lock);
1175 for (;;) {
1176 prepare_to_wait(&fwc->wait_queue, &wait,
1177 TASK_UNINTERRUPTIBLE);
1178 if (!fwc->cnt)
1179 break;
1180
1181 spin_unlock(&fwc->name_lock);
1182
1183 schedule();
1184
1185 spin_lock(&fwc->name_lock);
1186 }
1187 spin_unlock(&fwc->name_lock);
1188 finish_wait(&fwc->wait_queue, &wait);
Ming Leiffe53f62012-08-04 12:01:28 +08001189
1190 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001191}
1192
1193/**
1194 * device_uncache_fw_images - uncache devices' firmware
1195 *
1196 * uncache all firmwares which have been cached successfully
1197 * by device_uncache_fw_images earlier
1198 */
1199static void device_uncache_fw_images(void)
1200{
1201 pr_debug("%s\n", __func__);
1202 __device_uncache_fw_images();
1203}
1204
1205static void device_uncache_fw_images_work(struct work_struct *work)
1206{
1207 device_uncache_fw_images();
1208}
1209
1210/**
1211 * device_uncache_fw_images_delay - uncache devices firmwares
1212 * @delay: number of milliseconds to delay uncache device firmwares
1213 *
1214 * uncache all devices's firmwares which has been cached successfully
1215 * by device_cache_fw_images after @delay milliseconds.
1216 */
1217static void device_uncache_fw_images_delay(unsigned long delay)
1218{
1219 schedule_delayed_work(&fw_cache.work,
1220 msecs_to_jiffies(delay));
1221}
1222
Ming Lei07646d92012-08-04 12:01:29 +08001223#ifdef CONFIG_PM
1224static int fw_pm_notify(struct notifier_block *notify_block,
1225 unsigned long mode, void *unused)
1226{
1227 switch (mode) {
1228 case PM_HIBERNATION_PREPARE:
1229 case PM_SUSPEND_PREPARE:
1230 device_cache_fw_images();
1231 break;
1232
1233 case PM_POST_SUSPEND:
1234 case PM_POST_HIBERNATION:
1235 case PM_POST_RESTORE:
1236 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1237 break;
1238 }
1239
1240 return 0;
1241}
1242#else
1243static int fw_pm_notify(struct notifier_block *notify_block,
1244 unsigned long mode, void *unused)
Ming Leic08f6772012-08-17 22:06:58 +08001245{
1246 return 0;
1247}
Ming Lei07646d92012-08-04 12:01:29 +08001248#endif
1249
Ming Lei37276a52012-08-04 12:01:27 +08001250static void __init fw_cache_init(void)
1251{
1252 spin_lock_init(&fw_cache.lock);
1253 INIT_LIST_HEAD(&fw_cache.head);
1254
1255 spin_lock_init(&fw_cache.name_lock);
1256 INIT_LIST_HEAD(&fw_cache.fw_names);
1257 fw_cache.cnt = 0;
1258
1259 init_waitqueue_head(&fw_cache.wait_queue);
1260 INIT_DELAYED_WORK(&fw_cache.work,
1261 device_uncache_fw_images_work);
Ming Lei07646d92012-08-04 12:01:29 +08001262
1263 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1264 register_pm_notifier(&fw_cache.pm_notify);
Ming Lei37276a52012-08-04 12:01:27 +08001265}
1266
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001267static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Ming Lei1f2b7952012-08-04 12:01:21 +08001269 fw_cache_init();
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001270 return class_register(&firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001272
1273static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Ming Lei07646d92012-08-04 12:01:29 +08001275 unregister_pm_notifier(&fw_cache.pm_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 class_unregister(&firmware_class);
1277}
1278
Shaohua Lia30a6a22006-09-27 01:50:52 -07001279fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280module_exit(firmware_class_exit);
1281
1282EXPORT_SYMBOL(release_firmware);
1283EXPORT_SYMBOL(request_firmware);
1284EXPORT_SYMBOL(request_firmware_nowait);
Ming Lei2887b392012-08-04 12:01:22 +08001285EXPORT_SYMBOL_GPL(cache_firmware);
1286EXPORT_SYMBOL_GPL(uncache_firmware);