blob: 6e210802c37b5b4abbeb25657f6850a5faf7e1a4 [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 Leiac39b3e2012-08-20 19:04:16 +080028#include <linux/syscore_ops.h>
Ming Lei37276a52012-08-04 12:01:27 +080029
30#include "base.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 Leicfe016b2012-09-08 17:32:30 +080099 int state;
Ming Lei37276a52012-08-04 12:01:27 +0800100
Ming Leicfe016b2012-09-08 17:32:30 +0800101#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +0800102 /*
103 * Names of firmware images which have been cached successfully
104 * will be added into the below list so that device uncache
105 * helper can trace which firmware images have been cached
106 * before.
107 */
108 spinlock_t name_lock;
109 struct list_head fw_names;
110
111 wait_queue_head_t wait_queue;
112 int cnt;
113 struct delayed_work work;
Ming Lei07646d92012-08-04 12:01:29 +0800114
115 struct notifier_block pm_notify;
Ming Leicfe016b2012-09-08 17:32:30 +0800116#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800117};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Ming Lei12446912012-08-04 12:01:20 +0800119struct firmware_buf {
Ming Lei1f2b7952012-08-04 12:01:21 +0800120 struct kref ref;
121 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 struct completion completion;
Ming Lei1f2b7952012-08-04 12:01:21 +0800123 struct firmware_cache *fwc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 unsigned long status;
Ming Lei65710cb2012-08-04 12:01:16 +0800125 void *data;
126 size_t size;
David Woodhouse6e03a202009-04-09 22:04:07 -0700127 struct page **pages;
128 int nr_pages;
129 int page_array_size;
Dmitry Torokhove1771232010-03-13 23:49:23 -0800130 char fw_id[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
Ming Lei37276a52012-08-04 12:01:27 +0800133struct fw_cache_entry {
134 struct list_head list;
135 char name[];
136};
137
Ming Lei12446912012-08-04 12:01:20 +0800138struct firmware_priv {
139 struct timer_list timeout;
140 bool nowait;
141 struct device dev;
142 struct firmware_buf *buf;
Ming Lei1f2b7952012-08-04 12:01:21 +0800143 struct firmware *fw;
Ming Lei12446912012-08-04 12:01:20 +0800144};
145
Ming Leif531f052012-08-04 12:01:25 +0800146struct fw_name_devm {
147 unsigned long magic;
148 char name[];
149};
150
Ming Lei1f2b7952012-08-04 12:01:21 +0800151#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
152
Ming Leiac39b3e2012-08-20 19:04:16 +0800153#define FW_LOADER_NO_CACHE 0
154#define FW_LOADER_START_CACHE 1
155
156static int fw_cache_piggyback_on_request(const char *name);
157
Ming Lei1f2b7952012-08-04 12:01:21 +0800158/* fw_lock could be moved to 'struct firmware_priv' but since it is just
159 * guarding for corner cases a global lock should be OK */
160static DEFINE_MUTEX(fw_lock);
161
162static struct firmware_cache fw_cache;
163
164static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
165 struct firmware_cache *fwc)
166{
167 struct firmware_buf *buf;
168
169 buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
170
171 if (!buf)
172 return buf;
173
174 kref_init(&buf->ref);
175 strcpy(buf->fw_id, fw_name);
176 buf->fwc = fwc;
177 init_completion(&buf->completion);
178
179 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
180
181 return buf;
182}
183
Ming Lei2887b392012-08-04 12:01:22 +0800184static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
185{
186 struct firmware_buf *tmp;
187 struct firmware_cache *fwc = &fw_cache;
188
189 list_for_each_entry(tmp, &fwc->head, list)
190 if (!strcmp(tmp->fw_id, fw_name))
191 return tmp;
192 return NULL;
193}
194
Ming Lei1f2b7952012-08-04 12:01:21 +0800195static int fw_lookup_and_allocate_buf(const char *fw_name,
196 struct firmware_cache *fwc,
197 struct firmware_buf **buf)
198{
199 struct firmware_buf *tmp;
200
201 spin_lock(&fwc->lock);
Ming Lei2887b392012-08-04 12:01:22 +0800202 tmp = __fw_lookup_buf(fw_name);
203 if (tmp) {
204 kref_get(&tmp->ref);
205 spin_unlock(&fwc->lock);
206 *buf = tmp;
207 return 1;
208 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800209 tmp = __allocate_fw_buf(fw_name, fwc);
210 if (tmp)
211 list_add(&tmp->list, &fwc->head);
212 spin_unlock(&fwc->lock);
213
214 *buf = tmp;
215
216 return tmp ? 0 : -ENOMEM;
217}
218
Ming Lei2887b392012-08-04 12:01:22 +0800219static struct firmware_buf *fw_lookup_buf(const char *fw_name)
220{
221 struct firmware_buf *tmp;
222 struct firmware_cache *fwc = &fw_cache;
223
224 spin_lock(&fwc->lock);
225 tmp = __fw_lookup_buf(fw_name);
226 spin_unlock(&fwc->lock);
227
228 return tmp;
229}
230
Ming Lei1f2b7952012-08-04 12:01:21 +0800231static void __fw_free_buf(struct kref *ref)
232{
233 struct firmware_buf *buf = to_fwbuf(ref);
234 struct firmware_cache *fwc = buf->fwc;
235 int i;
236
237 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
238 __func__, buf->fw_id, buf, buf->data,
239 (unsigned int)buf->size);
240
241 spin_lock(&fwc->lock);
242 list_del(&buf->list);
243 spin_unlock(&fwc->lock);
244
245 vunmap(buf->data);
246 for (i = 0; i < buf->nr_pages; i++)
247 __free_page(buf->pages[i]);
248 kfree(buf->pages);
249 kfree(buf);
250}
251
252static void fw_free_buf(struct firmware_buf *buf)
253{
254 kref_put(&buf->ref, __fw_free_buf);
255}
256
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700257static struct firmware_priv *to_firmware_priv(struct device *dev)
258{
259 return container_of(dev, struct firmware_priv, dev);
260}
261
262static void fw_load_abort(struct firmware_priv *fw_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Ming Lei12446912012-08-04 12:01:20 +0800264 struct firmware_buf *buf = fw_priv->buf;
265
266 set_bit(FW_STATUS_ABORT, &buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800267 complete_all(&buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700270static ssize_t firmware_timeout_show(struct class *class,
271 struct class_attribute *attr,
272 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 return sprintf(buf, "%d\n", loading_timeout);
275}
276
277/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800278 * firmware_timeout_store - set number of seconds to wait for firmware
279 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800280 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800281 * @buf: buffer to scan for timeout value
282 * @count: number of bytes in @buf
283 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800285 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 * firmware will be provided.
287 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800288 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700290static ssize_t firmware_timeout_store(struct class *class,
291 struct class_attribute *attr,
292 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700295 if (loading_timeout < 0)
296 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return count;
299}
300
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800301static struct class_attribute firmware_class_attrs[] = {
302 __ATTR(timeout, S_IWUSR | S_IRUGO,
303 firmware_timeout_show, firmware_timeout_store),
304 __ATTR_NULL
305};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800307static void fw_dev_release(struct device *dev)
308{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700309 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800310
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800311 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800312
313 module_put(THIS_MODULE);
314}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Kay Sievers7eff2e72007-08-14 15:15:12 +0200316static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700318 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Ming Lei12446912012-08-04 12:01:20 +0800320 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200322 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700323 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200324 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
325 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 return 0;
328}
329
Adrian Bunk1b81d662006-05-20 15:00:16 -0700330static struct class firmware_class = {
331 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800332 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700333 .dev_uevent = firmware_uevent,
334 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700335};
336
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700337static ssize_t firmware_loading_show(struct device *dev,
338 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700340 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800341 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return sprintf(buf, "%d\n", loading);
344}
345
Ming Lei65710cb2012-08-04 12:01:16 +0800346/* firmware holds the ownership of pages */
David Woodhousedd336c52010-05-02 11:21:21 +0300347static void firmware_free_data(const struct firmware *fw)
348{
Ming Lei1f2b7952012-08-04 12:01:21 +0800349 WARN_ON(!fw->priv);
350 fw_free_buf(fw->priv);
David Woodhousedd336c52010-05-02 11:21:21 +0300351}
352
David Woodhouse6e03a202009-04-09 22:04:07 -0700353/* Some architectures don't have PAGE_KERNEL_RO */
354#ifndef PAGE_KERNEL_RO
355#define PAGE_KERNEL_RO PAGE_KERNEL
356#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800358 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700359 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800360 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800361 * @buf: buffer to scan for loading control value
362 * @count: number of bytes in @buf
363 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 * The relevant values are:
365 *
366 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800367 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 * -1: Conclude the load with an error and discard any written data.
369 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700370static ssize_t firmware_loading_store(struct device *dev,
371 struct device_attribute *attr,
372 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700374 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800375 struct firmware_buf *fw_buf = fw_priv->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700377 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Neil Hormaneea915b2012-01-02 15:31:23 -0500379 mutex_lock(&fw_lock);
380
Ming Lei12446912012-08-04 12:01:20 +0800381 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500382 goto out;
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 switch (loading) {
385 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800386 /* discarding any previous partial load */
Ming Lei12446912012-08-04 12:01:20 +0800387 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
388 for (i = 0; i < fw_buf->nr_pages; i++)
389 __free_page(fw_buf->pages[i]);
390 kfree(fw_buf->pages);
391 fw_buf->pages = NULL;
392 fw_buf->page_array_size = 0;
393 fw_buf->nr_pages = 0;
394 set_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei28eefa72012-08-04 12:01:17 +0800395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 break;
397 case 0:
Ming Lei12446912012-08-04 12:01:20 +0800398 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
399 set_bit(FW_STATUS_DONE, &fw_buf->status);
400 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800401 complete_all(&fw_buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 break;
403 }
404 /* fallthrough */
405 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700406 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 /* fallthrough */
408 case -1:
409 fw_load_abort(fw_priv);
410 break;
411 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500412out:
413 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return count;
415}
416
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700417static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700419static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
420 struct bin_attribute *bin_attr,
421 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200423 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700424 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800425 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700426 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Laura Garciacad1e552006-05-23 23:22:38 +0200428 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800429 buf = fw_priv->buf;
430 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 ret_count = -ENODEV;
432 goto out;
433 }
Ming Lei12446912012-08-04 12:01:20 +0800434 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200435 ret_count = 0;
436 goto out;
437 }
Ming Lei12446912012-08-04 12:01:20 +0800438 if (count > buf->size - offset)
439 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700440
441 ret_count = count;
442
443 while (count) {
444 void *page_data;
445 int page_nr = offset >> PAGE_SHIFT;
446 int page_ofs = offset & (PAGE_SIZE-1);
447 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
448
Ming Lei12446912012-08-04 12:01:20 +0800449 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700450
451 memcpy(buffer, page_data + page_ofs, page_cnt);
452
Ming Lei12446912012-08-04 12:01:20 +0800453 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700454 buffer += page_cnt;
455 offset += page_cnt;
456 count -= page_cnt;
457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458out:
Laura Garciacad1e552006-05-23 23:22:38 +0200459 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return ret_count;
461}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800462
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700463static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Ming Lei12446912012-08-04 12:01:20 +0800465 struct firmware_buf *buf = fw_priv->buf;
David Woodhouse6e03a202009-04-09 22:04:07 -0700466 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
David Woodhouse6e03a202009-04-09 22:04:07 -0700468 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800469 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700470 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800471 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700472 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
David Woodhouse6e03a202009-04-09 22:04:07 -0700474 new_pages = kmalloc(new_array_size * sizeof(void *),
475 GFP_KERNEL);
476 if (!new_pages) {
477 fw_load_abort(fw_priv);
478 return -ENOMEM;
479 }
Ming Lei12446912012-08-04 12:01:20 +0800480 memcpy(new_pages, buf->pages,
481 buf->page_array_size * sizeof(void *));
482 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
483 (new_array_size - buf->page_array_size));
484 kfree(buf->pages);
485 buf->pages = new_pages;
486 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700488
Ming Lei12446912012-08-04 12:01:20 +0800489 while (buf->nr_pages < pages_needed) {
490 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700491 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
492
Ming Lei12446912012-08-04 12:01:20 +0800493 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700494 fw_load_abort(fw_priv);
495 return -ENOMEM;
496 }
Ming Lei12446912012-08-04 12:01:20 +0800497 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return 0;
500}
501
502/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800503 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700504 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700505 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700506 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800507 * @buffer: buffer being written
508 * @offset: buffer offset for write in total data store area
509 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800511 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 * the driver as a firmware image.
513 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700514static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
515 struct bin_attribute *bin_attr,
516 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200518 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700519 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800520 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 ssize_t retval;
522
523 if (!capable(CAP_SYS_RAWIO))
524 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700525
Laura Garciacad1e552006-05-23 23:22:38 +0200526 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800527 buf = fw_priv->buf;
528 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 retval = -ENODEV;
530 goto out;
531 }
Ming Lei65710cb2012-08-04 12:01:16 +0800532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 retval = fw_realloc_buffer(fw_priv, offset + count);
534 if (retval)
535 goto out;
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700538
539 while (count) {
540 void *page_data;
541 int page_nr = offset >> PAGE_SHIFT;
542 int page_ofs = offset & (PAGE_SIZE - 1);
543 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
544
Ming Lei12446912012-08-04 12:01:20 +0800545 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700546
547 memcpy(page_data + page_ofs, buffer, page_cnt);
548
Ming Lei12446912012-08-04 12:01:20 +0800549 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700550 buffer += page_cnt;
551 offset += page_cnt;
552 count -= page_cnt;
553 }
554
Ming Lei12446912012-08-04 12:01:20 +0800555 buf->size = max_t(size_t, offset, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556out:
Laura Garciacad1e552006-05-23 23:22:38 +0200557 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return retval;
559}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800560
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700561static struct bin_attribute firmware_attr_data = {
562 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 .size = 0,
564 .read = firmware_data_read,
565 .write = firmware_data_write,
566};
567
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700568static void firmware_class_timeout(u_long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 fw_load_abort(fw_priv);
573}
574
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700575static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200576fw_create_instance(struct firmware *firmware, const char *fw_name,
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700577 struct device *device, bool uevent, bool nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700579 struct firmware_priv *fw_priv;
580 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Ming Lei12446912012-08-04 12:01:20 +0800582 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700583 if (!fw_priv) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700584 dev_err(device, "%s: kmalloc failed\n", __func__);
Ming Lei12446912012-08-04 12:01:20 +0800585 fw_priv = ERR_PTR(-ENOMEM);
586 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700589 fw_priv->nowait = nowait;
Ming Lei1f2b7952012-08-04 12:01:21 +0800590 fw_priv->fw = firmware;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700591 setup_timer(&fw_priv->timeout,
592 firmware_class_timeout, (u_long) fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700594 f_dev = &fw_priv->dev;
595
596 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800597 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700598 f_dev->parent = device;
599 f_dev->class = &firmware_class;
Ming Lei12446912012-08-04 12:01:20 +0800600exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700601 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
Ming Lei1f2b7952012-08-04 12:01:21 +0800604/* one pages buffer is mapped/unmapped only once */
605static int fw_map_pages_buf(struct firmware_buf *buf)
606{
607 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
608 if (!buf->data)
609 return -ENOMEM;
610 return 0;
611}
612
613/* store the pages buffer info firmware from buf */
614static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
615{
616 fw->priv = buf;
617 fw->pages = buf->pages;
618 fw->size = buf->size;
619 fw->data = buf->data;
620
621 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
622 __func__, buf->fw_id, buf, buf->data,
623 (unsigned int)buf->size);
624}
625
Ming Leicfe016b2012-09-08 17:32:30 +0800626#ifdef CONFIG_PM_SLEEP
Ming Leif531f052012-08-04 12:01:25 +0800627static void fw_name_devm_release(struct device *dev, void *res)
628{
629 struct fw_name_devm *fwn = res;
630
631 if (fwn->magic == (unsigned long)&fw_cache)
632 pr_debug("%s: fw_name-%s devm-%p released\n",
633 __func__, fwn->name, res);
634}
635
636static int fw_devm_match(struct device *dev, void *res,
637 void *match_data)
638{
639 struct fw_name_devm *fwn = res;
640
641 return (fwn->magic == (unsigned long)&fw_cache) &&
642 !strcmp(fwn->name, match_data);
643}
644
645static struct fw_name_devm *fw_find_devm_name(struct device *dev,
646 const char *name)
647{
648 struct fw_name_devm *fwn;
649
650 fwn = devres_find(dev, fw_name_devm_release,
651 fw_devm_match, (void *)name);
652 return fwn;
653}
654
655/* add firmware name into devres list */
656static int fw_add_devm_name(struct device *dev, const char *name)
657{
658 struct fw_name_devm *fwn;
659
660 fwn = fw_find_devm_name(dev, name);
661 if (fwn)
662 return 1;
663
664 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
665 strlen(name) + 1, GFP_KERNEL);
666 if (!fwn)
667 return -ENOMEM;
668
669 fwn->magic = (unsigned long)&fw_cache;
670 strcpy(fwn->name, name);
671 devres_add(dev, fwn);
672
673 return 0;
674}
Ming Leicfe016b2012-09-08 17:32:30 +0800675#else
676static int fw_add_devm_name(struct device *dev, const char *name)
677{
678 return 0;
679}
680#endif
Ming Leif531f052012-08-04 12:01:25 +0800681
Ming Lei1f2b7952012-08-04 12:01:21 +0800682static void _request_firmware_cleanup(const struct firmware **firmware_p)
683{
684 release_firmware(*firmware_p);
685 *firmware_p = NULL;
686}
687
Stephen Boyddddb5542012-03-28 23:30:43 +0200688static struct firmware_priv *
689_request_firmware_prepare(const struct firmware **firmware_p, const char *name,
690 struct device *device, bool uevent, bool nowait)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700691{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +0800693 struct firmware_priv *fw_priv = NULL;
694 struct firmware_buf *buf;
695 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 if (!firmware_p)
Stephen Boyddddb5542012-03-28 23:30:43 +0200698 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Jiri Slaby4aed0642005-09-13 01:25:01 -0700700 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700702 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
703 __func__);
Stephen Boyddddb5542012-03-28 23:30:43 +0200704 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800707 if (fw_get_builtin_firmware(firmware, name)) {
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100708 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
Stephen Boyddddb5542012-03-28 23:30:43 +0200709 return NULL;
David Woodhouse5658c762008-05-23 13:52:42 +0100710 }
711
Ming Lei1f2b7952012-08-04 12:01:21 +0800712 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
713 if (!ret)
714 fw_priv = fw_create_instance(firmware, name, device,
715 uevent, nowait);
716
717 if (IS_ERR(fw_priv) || ret < 0) {
718 kfree(firmware);
Stephen Boyddddb5542012-03-28 23:30:43 +0200719 *firmware_p = NULL;
Ming Lei1f2b7952012-08-04 12:01:21 +0800720 return ERR_PTR(-ENOMEM);
721 } else if (fw_priv) {
722 fw_priv->buf = buf;
723
724 /*
725 * bind with 'buf' now to avoid warning in failure path
726 * of requesting firmware.
727 */
728 firmware->priv = buf;
729 return fw_priv;
Stephen Boyddddb5542012-03-28 23:30:43 +0200730 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800731
732 /* share the cached buf, which is inprogessing or completed */
733 check_status:
734 mutex_lock(&fw_lock);
735 if (test_bit(FW_STATUS_ABORT, &buf->status)) {
736 fw_priv = ERR_PTR(-ENOENT);
Ming Leief40bb12012-08-20 19:04:15 +0800737 firmware->priv = buf;
Ming Lei1f2b7952012-08-04 12:01:21 +0800738 _request_firmware_cleanup(firmware_p);
739 goto exit;
740 } else if (test_bit(FW_STATUS_DONE, &buf->status)) {
741 fw_priv = NULL;
742 fw_set_page_data(buf, firmware);
743 goto exit;
744 }
745 mutex_unlock(&fw_lock);
746 wait_for_completion(&buf->completion);
747 goto check_status;
748
749exit:
750 mutex_unlock(&fw_lock);
Stephen Boyddddb5542012-03-28 23:30:43 +0200751 return fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200752}
753
Stephen Boyddddb5542012-03-28 23:30:43 +0200754static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
755 long timeout)
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200756{
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200757 int retval = 0;
Stephen Boyddddb5542012-03-28 23:30:43 +0200758 struct device *f_dev = &fw_priv->dev;
Ming Lei12446912012-08-04 12:01:20 +0800759 struct firmware_buf *buf = fw_priv->buf;
Ming Leiac39b3e2012-08-20 19:04:16 +0800760 struct firmware_cache *fwc = &fw_cache;
Linus Torvaldscaca9512011-08-24 15:55:30 -0700761
Stephen Boyddddb5542012-03-28 23:30:43 +0200762 dev_set_uevent_suppress(f_dev, true);
David Woodhouse5658c762008-05-23 13:52:42 +0100763
Stephen Boyddddb5542012-03-28 23:30:43 +0200764 /* Need to pin this module until class device is destroyed */
765 __module_get(THIS_MODULE);
766
767 retval = device_add(f_dev);
768 if (retval) {
769 dev_err(f_dev, "%s: device_register failed\n", __func__);
770 goto err_put_dev;
771 }
772
773 retval = device_create_bin_file(f_dev, &firmware_attr_data);
774 if (retval) {
775 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
776 goto err_del_dev;
777 }
778
779 retval = device_create_file(f_dev, &dev_attr_loading);
780 if (retval) {
781 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
782 goto err_del_bin_attr;
783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Kay Sievers312c0042005-11-16 09:00:00 +0100785 if (uevent) {
Stephen Boyddddb5542012-03-28 23:30:43 +0200786 dev_set_uevent_suppress(f_dev, false);
Ming Lei12446912012-08-04 12:01:20 +0800787 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200788 if (timeout != MAX_SCHEDULE_TIMEOUT)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700789 mod_timer(&fw_priv->timeout,
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200790 round_jiffies_up(jiffies + timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700792 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
793 }
794
Ming Lei12446912012-08-04 12:01:20 +0800795 wait_for_completion(&buf->completion);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700796
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700797 del_timer_sync(&fw_priv->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Laura Garciacad1e552006-05-23 23:22:38 +0200799 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800800 if (!buf->size || test_bit(FW_STATUS_ABORT, &buf->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 retval = -ENOENT;
Ming Lei65710cb2012-08-04 12:01:16 +0800802
Ming Leif531f052012-08-04 12:01:25 +0800803 /*
804 * add firmware name into devres list so that we can auto cache
805 * and uncache firmware for device.
806 *
807 * f_dev->parent may has been deleted already, but the problem
808 * should be fixed in devres or driver core.
809 */
810 if (!retval && f_dev->parent)
811 fw_add_devm_name(f_dev->parent, buf->fw_id);
812
Ming Lei65710cb2012-08-04 12:01:16 +0800813 if (!retval)
Ming Lei1f2b7952012-08-04 12:01:21 +0800814 retval = fw_map_pages_buf(buf);
Ming Lei12446912012-08-04 12:01:20 +0800815
Ming Leiac39b3e2012-08-20 19:04:16 +0800816 /*
817 * After caching firmware image is started, let it piggyback
818 * on request firmware.
819 */
820 if (!retval && fwc->state == FW_LOADER_START_CACHE) {
821 if (fw_cache_piggyback_on_request(buf->fw_id))
822 kref_get(&buf->ref);
823 }
824
Ming Lei1f2b7952012-08-04 12:01:21 +0800825 /* pass the pages buffer to driver at the last minute */
826 fw_set_page_data(buf, fw_priv->fw);
827
Ming Lei12446912012-08-04 12:01:20 +0800828 fw_priv->buf = NULL;
Laura Garciacad1e552006-05-23 23:22:38 +0200829 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Stephen Boyddddb5542012-03-28 23:30:43 +0200831 device_remove_file(f_dev, &dev_attr_loading);
832err_del_bin_attr:
833 device_remove_bin_file(f_dev, &firmware_attr_data);
834err_del_dev:
835 device_del(f_dev);
836err_put_dev:
837 put_device(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return retval;
839}
840
841/**
Kay Sievers312c0042005-11-16 09:00:00 +0100842 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800843 * @firmware_p: pointer to firmware image
844 * @name: name of firmware file
845 * @device: device for which firmware is being loaded
846 *
847 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700848 * of @name for device @device.
849 *
850 * Should be called from user context where sleeping is allowed.
851 *
Kay Sievers312c0042005-11-16 09:00:00 +0100852 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700853 * should be distinctive enough not to be confused with any other
854 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +0800855 *
856 * Caller must hold the reference count of @device.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700857 **/
858int
859request_firmware(const struct firmware **firmware_p, const char *name,
860 struct device *device)
861{
Stephen Boyddddb5542012-03-28 23:30:43 +0200862 struct firmware_priv *fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200863 int ret;
864
Stephen Boyddddb5542012-03-28 23:30:43 +0200865 fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
866 false);
867 if (IS_ERR_OR_NULL(fw_priv))
868 return PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200869
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200870 ret = usermodehelper_read_trylock();
871 if (WARN_ON(ret)) {
872 dev_err(device, "firmware: %s will not be loaded\n", name);
873 } else {
Stephen Boyddddb5542012-03-28 23:30:43 +0200874 ret = _request_firmware_load(fw_priv, true,
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200875 firmware_loading_timeout());
876 usermodehelper_read_unlock();
877 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200878 if (ret)
879 _request_firmware_cleanup(firmware_p);
880
881 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700882}
883
884/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800886 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800888void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
890 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800891 if (!fw_is_builtin_firmware(fw))
892 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 kfree(fw);
894 }
895}
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897/* Async support */
898struct firmware_work {
899 struct work_struct work;
900 struct module *module;
901 const char *name;
902 struct device *device;
903 void *context;
904 void (*cont)(const struct firmware *fw, void *context);
Bob Liu072fc8f2011-01-26 18:33:32 +0800905 bool uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906};
907
Stephen Boyda36cf842012-03-28 23:31:00 +0200908static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Stephen Boyda36cf842012-03-28 23:31:00 +0200910 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 const struct firmware *fw;
Stephen Boyddddb5542012-03-28 23:30:43 +0200912 struct firmware_priv *fw_priv;
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200913 long timeout;
matthieu castet113fab12005-11-13 16:07:39 -0800914 int ret;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700915
Stephen Boyda36cf842012-03-28 23:31:00 +0200916 fw_work = container_of(work, struct firmware_work, work);
Stephen Boyddddb5542012-03-28 23:30:43 +0200917 fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
918 fw_work->uevent, true);
919 if (IS_ERR_OR_NULL(fw_priv)) {
920 ret = PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200921 goto out;
Stephen Boyddddb5542012-03-28 23:30:43 +0200922 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200923
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200924 timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
925 if (timeout) {
Stephen Boyddddb5542012-03-28 23:30:43 +0200926 ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200927 usermodehelper_read_unlock();
928 } else {
929 dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
930 fw_work->name);
931 ret = -EAGAIN;
932 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200933 if (ret)
934 _request_firmware_cleanup(&fw);
935
936 out:
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100937 fw_work->cont(fw, fw_work->context);
Ming Lei0cfc1e12012-08-04 12:01:23 +0800938 put_device(fw_work->device);
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 module_put(fw_work->module);
941 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
944/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000945 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800946 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +0100947 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800948 * is non-zero else the firmware copy must be done manually.
949 * @name: name of firmware file
950 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100951 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800952 * @context: will be passed over to @cont, and
953 * @fw may be %NULL if firmware request fails.
954 * @cont: function will be called asynchronously when the firmware
955 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 *
Ming Lei0cfc1e12012-08-04 12:01:23 +0800957 * Caller must hold the reference count of @device.
958 *
Ming Lei6f21a622012-08-04 12:01:24 +0800959 * Asynchronous variant of request_firmware() for user contexts:
960 * - sleep for as small periods as possible since it may
961 * increase kernel boot time of built-in device drivers
962 * requesting firmware in their ->probe() methods, if
963 * @gfp is GFP_KERNEL.
964 *
965 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 **/
967int
968request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +0800969 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100970 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 void (*cont)(const struct firmware *fw, void *context))
972{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700973 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700975 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (!fw_work)
977 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700978
979 fw_work->module = module;
980 fw_work->name = name;
981 fw_work->device = device;
982 fw_work->context = context;
983 fw_work->cont = cont;
984 fw_work->uevent = uevent;
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (!try_module_get(module)) {
987 kfree(fw_work);
988 return -EFAULT;
989 }
990
Ming Lei0cfc1e12012-08-04 12:01:23 +0800991 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +0200992 INIT_WORK(&fw_work->work, request_firmware_work_func);
993 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return 0;
995}
996
Ming Lei2887b392012-08-04 12:01:22 +0800997/**
998 * cache_firmware - cache one firmware image in kernel memory space
999 * @fw_name: the firmware image name
1000 *
1001 * Cache firmware in kernel memory so that drivers can use it when
1002 * system isn't ready for them to request firmware image from userspace.
1003 * Once it returns successfully, driver can use request_firmware or its
1004 * nowait version to get the cached firmware without any interacting
1005 * with userspace
1006 *
1007 * Return 0 if the firmware image has been cached successfully
1008 * Return !0 otherwise
1009 *
1010 */
1011int cache_firmware(const char *fw_name)
1012{
1013 int ret;
1014 const struct firmware *fw;
1015
1016 pr_debug("%s: %s\n", __func__, fw_name);
1017
1018 ret = request_firmware(&fw, fw_name, NULL);
1019 if (!ret)
1020 kfree(fw);
1021
1022 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1023
1024 return ret;
1025}
1026
1027/**
1028 * uncache_firmware - remove one cached firmware image
1029 * @fw_name: the firmware image name
1030 *
1031 * Uncache one firmware image which has been cached successfully
1032 * before.
1033 *
1034 * Return 0 if the firmware cache has been removed successfully
1035 * Return !0 otherwise
1036 *
1037 */
1038int uncache_firmware(const char *fw_name)
1039{
1040 struct firmware_buf *buf;
1041 struct firmware fw;
1042
1043 pr_debug("%s: %s\n", __func__, fw_name);
1044
1045 if (fw_get_builtin_firmware(&fw, fw_name))
1046 return 0;
1047
1048 buf = fw_lookup_buf(fw_name);
1049 if (buf) {
1050 fw_free_buf(buf);
1051 return 0;
1052 }
1053
1054 return -EINVAL;
1055}
1056
Ming Leicfe016b2012-09-08 17:32:30 +08001057#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +08001058static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1059{
1060 struct fw_cache_entry *fce;
1061
1062 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1063 if (!fce)
1064 goto exit;
1065
1066 strcpy(fce->name, name);
1067exit:
1068 return fce;
1069}
1070
Ming Leiac39b3e2012-08-20 19:04:16 +08001071static int fw_cache_piggyback_on_request(const char *name)
1072{
1073 struct firmware_cache *fwc = &fw_cache;
1074 struct fw_cache_entry *fce;
1075 int ret = 0;
1076
1077 spin_lock(&fwc->name_lock);
1078 list_for_each_entry(fce, &fwc->fw_names, list) {
1079 if (!strcmp(fce->name, name))
1080 goto found;
1081 }
1082
1083 fce = alloc_fw_cache_entry(name);
1084 if (fce) {
1085 ret = 1;
1086 list_add(&fce->list, &fwc->fw_names);
1087 pr_debug("%s: fw: %s\n", __func__, name);
1088 }
1089found:
1090 spin_unlock(&fwc->name_lock);
1091 return ret;
1092}
1093
Ming Lei37276a52012-08-04 12:01:27 +08001094static void free_fw_cache_entry(struct fw_cache_entry *fce)
1095{
1096 kfree(fce);
1097}
1098
1099static void __async_dev_cache_fw_image(void *fw_entry,
1100 async_cookie_t cookie)
1101{
1102 struct fw_cache_entry *fce = fw_entry;
1103 struct firmware_cache *fwc = &fw_cache;
1104 int ret;
1105
1106 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001107 if (ret) {
1108 spin_lock(&fwc->name_lock);
1109 list_del(&fce->list);
1110 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001111
Ming Leiac39b3e2012-08-20 19:04:16 +08001112 free_fw_cache_entry(fce);
1113 }
Ming Lei37276a52012-08-04 12:01:27 +08001114
Ming Lei37276a52012-08-04 12:01:27 +08001115 spin_lock(&fwc->name_lock);
1116 fwc->cnt--;
1117 spin_unlock(&fwc->name_lock);
1118
1119 wake_up(&fwc->wait_queue);
1120}
1121
1122/* called with dev->devres_lock held */
1123static void dev_create_fw_entry(struct device *dev, void *res,
1124 void *data)
1125{
1126 struct fw_name_devm *fwn = res;
1127 const char *fw_name = fwn->name;
1128 struct list_head *head = data;
1129 struct fw_cache_entry *fce;
1130
1131 fce = alloc_fw_cache_entry(fw_name);
1132 if (fce)
1133 list_add(&fce->list, head);
1134}
1135
1136static int devm_name_match(struct device *dev, void *res,
1137 void *match_data)
1138{
1139 struct fw_name_devm *fwn = res;
1140 return (fwn->magic == (unsigned long)match_data);
1141}
1142
Ming Leiab6dd8e2012-08-17 22:07:00 +08001143static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001144{
1145 LIST_HEAD(todo);
1146 struct fw_cache_entry *fce;
1147 struct fw_cache_entry *fce_next;
1148 struct firmware_cache *fwc = &fw_cache;
1149
1150 devres_for_each_res(dev, fw_name_devm_release,
1151 devm_name_match, &fw_cache,
1152 dev_create_fw_entry, &todo);
1153
1154 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1155 list_del(&fce->list);
1156
1157 spin_lock(&fwc->name_lock);
1158 fwc->cnt++;
Ming Leiac39b3e2012-08-20 19:04:16 +08001159 list_add(&fce->list, &fwc->fw_names);
Ming Lei37276a52012-08-04 12:01:27 +08001160 spin_unlock(&fwc->name_lock);
1161
1162 async_schedule(__async_dev_cache_fw_image, (void *)fce);
1163 }
1164}
1165
1166static void __device_uncache_fw_images(void)
1167{
1168 struct firmware_cache *fwc = &fw_cache;
1169 struct fw_cache_entry *fce;
1170
1171 spin_lock(&fwc->name_lock);
1172 while (!list_empty(&fwc->fw_names)) {
1173 fce = list_entry(fwc->fw_names.next,
1174 struct fw_cache_entry, list);
1175 list_del(&fce->list);
1176 spin_unlock(&fwc->name_lock);
1177
1178 uncache_firmware(fce->name);
1179 free_fw_cache_entry(fce);
1180
1181 spin_lock(&fwc->name_lock);
1182 }
1183 spin_unlock(&fwc->name_lock);
1184}
1185
1186/**
1187 * device_cache_fw_images - cache devices' firmware
1188 *
1189 * If one device called request_firmware or its nowait version
1190 * successfully before, the firmware names are recored into the
1191 * device's devres link list, so device_cache_fw_images can call
1192 * cache_firmware() to cache these firmwares for the device,
1193 * then the device driver can load its firmwares easily at
1194 * time when system is not ready to complete loading firmware.
1195 */
1196static void device_cache_fw_images(void)
1197{
1198 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001199 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001200 DEFINE_WAIT(wait);
1201
1202 pr_debug("%s\n", __func__);
1203
Ming Leiffe53f62012-08-04 12:01:28 +08001204 /*
1205 * use small loading timeout for caching devices' firmware
1206 * because all these firmware images have been loaded
1207 * successfully at lease once, also system is ready for
1208 * completing firmware loading now. The maximum size of
1209 * firmware in current distributions is about 2M bytes,
1210 * so 10 secs should be enough.
1211 */
1212 old_timeout = loading_timeout;
1213 loading_timeout = 10;
1214
Ming Leiac39b3e2012-08-20 19:04:16 +08001215 mutex_lock(&fw_lock);
1216 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001217 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001218 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001219
1220 /* wait for completion of caching firmware for all devices */
1221 spin_lock(&fwc->name_lock);
1222 for (;;) {
1223 prepare_to_wait(&fwc->wait_queue, &wait,
1224 TASK_UNINTERRUPTIBLE);
1225 if (!fwc->cnt)
1226 break;
1227
1228 spin_unlock(&fwc->name_lock);
1229
1230 schedule();
1231
1232 spin_lock(&fwc->name_lock);
1233 }
1234 spin_unlock(&fwc->name_lock);
1235 finish_wait(&fwc->wait_queue, &wait);
Ming Leiffe53f62012-08-04 12:01:28 +08001236
1237 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001238}
1239
1240/**
1241 * device_uncache_fw_images - uncache devices' firmware
1242 *
1243 * uncache all firmwares which have been cached successfully
1244 * by device_uncache_fw_images earlier
1245 */
1246static void device_uncache_fw_images(void)
1247{
1248 pr_debug("%s\n", __func__);
1249 __device_uncache_fw_images();
1250}
1251
1252static void device_uncache_fw_images_work(struct work_struct *work)
1253{
1254 device_uncache_fw_images();
1255}
1256
1257/**
1258 * device_uncache_fw_images_delay - uncache devices firmwares
1259 * @delay: number of milliseconds to delay uncache device firmwares
1260 *
1261 * uncache all devices's firmwares which has been cached successfully
1262 * by device_cache_fw_images after @delay milliseconds.
1263 */
1264static void device_uncache_fw_images_delay(unsigned long delay)
1265{
1266 schedule_delayed_work(&fw_cache.work,
1267 msecs_to_jiffies(delay));
1268}
1269
Ming Lei07646d92012-08-04 12:01:29 +08001270static int fw_pm_notify(struct notifier_block *notify_block,
1271 unsigned long mode, void *unused)
1272{
1273 switch (mode) {
1274 case PM_HIBERNATION_PREPARE:
1275 case PM_SUSPEND_PREPARE:
1276 device_cache_fw_images();
1277 break;
1278
1279 case PM_POST_SUSPEND:
1280 case PM_POST_HIBERNATION:
1281 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001282 /*
1283 * In case that system sleep failed and syscore_suspend is
1284 * not called.
1285 */
1286 mutex_lock(&fw_lock);
1287 fw_cache.state = FW_LOADER_NO_CACHE;
1288 mutex_unlock(&fw_lock);
1289
Ming Lei07646d92012-08-04 12:01:29 +08001290 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1291 break;
1292 }
1293
1294 return 0;
1295}
Ming Lei07646d92012-08-04 12:01:29 +08001296
Ming Leiac39b3e2012-08-20 19:04:16 +08001297/* stop caching firmware once syscore_suspend is reached */
1298static int fw_suspend(void)
1299{
1300 fw_cache.state = FW_LOADER_NO_CACHE;
1301 return 0;
1302}
1303
1304static struct syscore_ops fw_syscore_ops = {
1305 .suspend = fw_suspend,
1306};
Ming Leicfe016b2012-09-08 17:32:30 +08001307#else
1308static int fw_cache_piggyback_on_request(const char *name)
1309{
1310 return 0;
1311}
1312#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001313
Ming Lei37276a52012-08-04 12:01:27 +08001314static void __init fw_cache_init(void)
1315{
1316 spin_lock_init(&fw_cache.lock);
1317 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001318 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001319
Ming Leicfe016b2012-09-08 17:32:30 +08001320#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +08001321 spin_lock_init(&fw_cache.name_lock);
1322 INIT_LIST_HEAD(&fw_cache.fw_names);
1323 fw_cache.cnt = 0;
1324
1325 init_waitqueue_head(&fw_cache.wait_queue);
1326 INIT_DELAYED_WORK(&fw_cache.work,
1327 device_uncache_fw_images_work);
Ming Lei07646d92012-08-04 12:01:29 +08001328
1329 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1330 register_pm_notifier(&fw_cache.pm_notify);
Ming Leiac39b3e2012-08-20 19:04:16 +08001331
1332 register_syscore_ops(&fw_syscore_ops);
Ming Leicfe016b2012-09-08 17:32:30 +08001333#endif
Ming Lei37276a52012-08-04 12:01:27 +08001334}
1335
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001336static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Ming Lei1f2b7952012-08-04 12:01:21 +08001338 fw_cache_init();
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001339 return class_register(&firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001341
1342static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
Ming Leicfe016b2012-09-08 17:32:30 +08001344#ifdef CONFIG_PM_SLEEP
Ming Leiac39b3e2012-08-20 19:04:16 +08001345 unregister_syscore_ops(&fw_syscore_ops);
Ming Lei07646d92012-08-04 12:01:29 +08001346 unregister_pm_notifier(&fw_cache.pm_notify);
Ming Leicfe016b2012-09-08 17:32:30 +08001347#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 class_unregister(&firmware_class);
1349}
1350
Shaohua Lia30a6a22006-09-27 01:50:52 -07001351fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352module_exit(firmware_class_exit);
1353
1354EXPORT_SYMBOL(release_firmware);
1355EXPORT_SYMBOL(request_firmware);
1356EXPORT_SYMBOL(request_firmware_nowait);
Ming Lei2887b392012-08-04 12:01:22 +08001357EXPORT_SYMBOL_GPL(cache_firmware);
1358EXPORT_SYMBOL_GPL(uncache_firmware);