blob: 95f6851b3010b3dd2a036b2a387e9e732e75f1c3 [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 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
Ming Leiac39b3e2012-08-20 19:04:16 +0800109 int state;
110
Ming Lei37276a52012-08-04 12:01:27 +0800111 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 Lei1f2b7952012-08-04 12:01:21 +0800116};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Ming Lei12446912012-08-04 12:01:20 +0800118struct firmware_buf {
Ming Lei1f2b7952012-08-04 12:01:21 +0800119 struct kref ref;
120 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 struct completion completion;
Ming Lei1f2b7952012-08-04 12:01:21 +0800122 struct firmware_cache *fwc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 unsigned long status;
Ming Lei65710cb2012-08-04 12:01:16 +0800124 void *data;
125 size_t size;
David Woodhouse6e03a202009-04-09 22:04:07 -0700126 struct page **pages;
127 int nr_pages;
128 int page_array_size;
Dmitry Torokhove1771232010-03-13 23:49:23 -0800129 char fw_id[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130};
131
Ming Lei37276a52012-08-04 12:01:27 +0800132struct fw_cache_entry {
133 struct list_head list;
134 char name[];
135};
136
Ming Lei12446912012-08-04 12:01:20 +0800137struct firmware_priv {
138 struct timer_list timeout;
139 bool nowait;
140 struct device dev;
141 struct firmware_buf *buf;
Ming Lei1f2b7952012-08-04 12:01:21 +0800142 struct firmware *fw;
Ming Lei12446912012-08-04 12:01:20 +0800143};
144
Ming Leif531f05a2012-08-04 12:01:25 +0800145struct fw_name_devm {
146 unsigned long magic;
147 char name[];
148};
149
Ming Lei1f2b7952012-08-04 12:01:21 +0800150#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
151
Ming Leiac39b3e2012-08-20 19:04:16 +0800152#define FW_LOADER_NO_CACHE 0
153#define FW_LOADER_START_CACHE 1
154
155static int fw_cache_piggyback_on_request(const char *name);
156
Ming Lei1f2b7952012-08-04 12:01:21 +0800157/* fw_lock could be moved to 'struct firmware_priv' but since it is just
158 * guarding for corner cases a global lock should be OK */
159static DEFINE_MUTEX(fw_lock);
160
161static struct firmware_cache fw_cache;
162
163static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
164 struct firmware_cache *fwc)
165{
166 struct firmware_buf *buf;
167
168 buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
169
170 if (!buf)
171 return buf;
172
173 kref_init(&buf->ref);
174 strcpy(buf->fw_id, fw_name);
175 buf->fwc = fwc;
176 init_completion(&buf->completion);
177
178 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
179
180 return buf;
181}
182
Ming Lei2887b392012-08-04 12:01:22 +0800183static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
184{
185 struct firmware_buf *tmp;
186 struct firmware_cache *fwc = &fw_cache;
187
188 list_for_each_entry(tmp, &fwc->head, list)
189 if (!strcmp(tmp->fw_id, fw_name))
190 return tmp;
191 return NULL;
192}
193
Ming Lei1f2b7952012-08-04 12:01:21 +0800194static int fw_lookup_and_allocate_buf(const char *fw_name,
195 struct firmware_cache *fwc,
196 struct firmware_buf **buf)
197{
198 struct firmware_buf *tmp;
199
200 spin_lock(&fwc->lock);
Ming Lei2887b392012-08-04 12:01:22 +0800201 tmp = __fw_lookup_buf(fw_name);
202 if (tmp) {
203 kref_get(&tmp->ref);
204 spin_unlock(&fwc->lock);
205 *buf = tmp;
206 return 1;
207 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800208 tmp = __allocate_fw_buf(fw_name, fwc);
209 if (tmp)
210 list_add(&tmp->list, &fwc->head);
211 spin_unlock(&fwc->lock);
212
213 *buf = tmp;
214
215 return tmp ? 0 : -ENOMEM;
216}
217
Ming Lei2887b392012-08-04 12:01:22 +0800218static struct firmware_buf *fw_lookup_buf(const char *fw_name)
219{
220 struct firmware_buf *tmp;
221 struct firmware_cache *fwc = &fw_cache;
222
223 spin_lock(&fwc->lock);
224 tmp = __fw_lookup_buf(fw_name);
225 spin_unlock(&fwc->lock);
226
227 return tmp;
228}
229
Ming Lei1f2b7952012-08-04 12:01:21 +0800230static void __fw_free_buf(struct kref *ref)
231{
232 struct firmware_buf *buf = to_fwbuf(ref);
233 struct firmware_cache *fwc = buf->fwc;
234 int i;
235
236 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
237 __func__, buf->fw_id, buf, buf->data,
238 (unsigned int)buf->size);
239
240 spin_lock(&fwc->lock);
241 list_del(&buf->list);
242 spin_unlock(&fwc->lock);
243
244 vunmap(buf->data);
245 for (i = 0; i < buf->nr_pages; i++)
246 __free_page(buf->pages[i]);
247 kfree(buf->pages);
248 kfree(buf);
249}
250
251static void fw_free_buf(struct firmware_buf *buf)
252{
253 kref_put(&buf->ref, __fw_free_buf);
254}
255
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700256static struct firmware_priv *to_firmware_priv(struct device *dev)
257{
258 return container_of(dev, struct firmware_priv, dev);
259}
260
261static void fw_load_abort(struct firmware_priv *fw_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Ming Lei12446912012-08-04 12:01:20 +0800263 struct firmware_buf *buf = fw_priv->buf;
264
265 set_bit(FW_STATUS_ABORT, &buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800266 complete_all(&buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700269static ssize_t firmware_timeout_show(struct class *class,
270 struct class_attribute *attr,
271 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 return sprintf(buf, "%d\n", loading_timeout);
274}
275
276/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800277 * firmware_timeout_store - set number of seconds to wait for firmware
278 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800279 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800280 * @buf: buffer to scan for timeout value
281 * @count: number of bytes in @buf
282 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800284 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 * firmware will be provided.
286 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800287 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700289static ssize_t firmware_timeout_store(struct class *class,
290 struct class_attribute *attr,
291 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700294 if (loading_timeout < 0)
295 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return count;
298}
299
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800300static struct class_attribute firmware_class_attrs[] = {
301 __ATTR(timeout, S_IWUSR | S_IRUGO,
302 firmware_timeout_show, firmware_timeout_store),
303 __ATTR_NULL
304};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800306static void fw_dev_release(struct device *dev)
307{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700308 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800309
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800310 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800311
312 module_put(THIS_MODULE);
313}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Kay Sievers7eff2e72007-08-14 15:15:12 +0200315static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700317 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Ming Lei12446912012-08-04 12:01:20 +0800319 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200321 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700322 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200323 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
324 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 return 0;
327}
328
Adrian Bunk1b81d662006-05-20 15:00:16 -0700329static struct class firmware_class = {
330 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800331 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700332 .dev_uevent = firmware_uevent,
333 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700334};
335
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700336static ssize_t firmware_loading_show(struct device *dev,
337 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700339 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800340 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return sprintf(buf, "%d\n", loading);
343}
344
Ming Lei65710cb2012-08-04 12:01:16 +0800345/* firmware holds the ownership of pages */
David Woodhousedd336c52010-05-02 11:21:21 +0300346static void firmware_free_data(const struct firmware *fw)
347{
Ming Lei1f2b7952012-08-04 12:01:21 +0800348 WARN_ON(!fw->priv);
349 fw_free_buf(fw->priv);
David Woodhousedd336c52010-05-02 11:21:21 +0300350}
351
David Woodhouse6e03a202009-04-09 22:04:07 -0700352/* Some architectures don't have PAGE_KERNEL_RO */
353#ifndef PAGE_KERNEL_RO
354#define PAGE_KERNEL_RO PAGE_KERNEL
355#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800357 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700358 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800359 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800360 * @buf: buffer to scan for loading control value
361 * @count: number of bytes in @buf
362 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 * The relevant values are:
364 *
365 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800366 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 * -1: Conclude the load with an error and discard any written data.
368 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700369static ssize_t firmware_loading_store(struct device *dev,
370 struct device_attribute *attr,
371 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700373 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800374 struct firmware_buf *fw_buf = fw_priv->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700376 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Neil Hormaneea915b2012-01-02 15:31:23 -0500378 mutex_lock(&fw_lock);
379
Ming Lei12446912012-08-04 12:01:20 +0800380 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500381 goto out;
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 switch (loading) {
384 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800385 /* discarding any previous partial load */
Ming Lei12446912012-08-04 12:01:20 +0800386 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
387 for (i = 0; i < fw_buf->nr_pages; i++)
388 __free_page(fw_buf->pages[i]);
389 kfree(fw_buf->pages);
390 fw_buf->pages = NULL;
391 fw_buf->page_array_size = 0;
392 fw_buf->nr_pages = 0;
393 set_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei28eefa72012-08-04 12:01:17 +0800394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 break;
396 case 0:
Ming Lei12446912012-08-04 12:01:20 +0800397 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
398 set_bit(FW_STATUS_DONE, &fw_buf->status);
399 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800400 complete_all(&fw_buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 break;
402 }
403 /* fallthrough */
404 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700405 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 /* fallthrough */
407 case -1:
408 fw_load_abort(fw_priv);
409 break;
410 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500411out:
412 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return count;
414}
415
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700416static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700418static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
419 struct bin_attribute *bin_attr,
420 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200422 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700423 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800424 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700425 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Laura Garciacad1e552006-05-23 23:22:38 +0200427 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800428 buf = fw_priv->buf;
429 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 ret_count = -ENODEV;
431 goto out;
432 }
Ming Lei12446912012-08-04 12:01:20 +0800433 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200434 ret_count = 0;
435 goto out;
436 }
Ming Lei12446912012-08-04 12:01:20 +0800437 if (count > buf->size - offset)
438 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700439
440 ret_count = count;
441
442 while (count) {
443 void *page_data;
444 int page_nr = offset >> PAGE_SHIFT;
445 int page_ofs = offset & (PAGE_SIZE-1);
446 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
447
Ming Lei12446912012-08-04 12:01:20 +0800448 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700449
450 memcpy(buffer, page_data + page_ofs, page_cnt);
451
Ming Lei12446912012-08-04 12:01:20 +0800452 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700453 buffer += page_cnt;
454 offset += page_cnt;
455 count -= page_cnt;
456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457out:
Laura Garciacad1e552006-05-23 23:22:38 +0200458 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return ret_count;
460}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800461
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700462static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Ming Lei12446912012-08-04 12:01:20 +0800464 struct firmware_buf *buf = fw_priv->buf;
David Woodhouse6e03a202009-04-09 22:04:07 -0700465 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
David Woodhouse6e03a202009-04-09 22:04:07 -0700467 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800468 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700469 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800470 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700471 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
David Woodhouse6e03a202009-04-09 22:04:07 -0700473 new_pages = kmalloc(new_array_size * sizeof(void *),
474 GFP_KERNEL);
475 if (!new_pages) {
476 fw_load_abort(fw_priv);
477 return -ENOMEM;
478 }
Ming Lei12446912012-08-04 12:01:20 +0800479 memcpy(new_pages, buf->pages,
480 buf->page_array_size * sizeof(void *));
481 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
482 (new_array_size - buf->page_array_size));
483 kfree(buf->pages);
484 buf->pages = new_pages;
485 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700487
Ming Lei12446912012-08-04 12:01:20 +0800488 while (buf->nr_pages < pages_needed) {
489 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700490 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
491
Ming Lei12446912012-08-04 12:01:20 +0800492 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700493 fw_load_abort(fw_priv);
494 return -ENOMEM;
495 }
Ming Lei12446912012-08-04 12:01:20 +0800496 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return 0;
499}
500
501/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800502 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700503 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700504 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700505 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800506 * @buffer: buffer being written
507 * @offset: buffer offset for write in total data store area
508 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800510 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 * the driver as a firmware image.
512 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700513static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
514 struct bin_attribute *bin_attr,
515 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200517 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700518 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800519 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 ssize_t retval;
521
522 if (!capable(CAP_SYS_RAWIO))
523 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700524
Laura Garciacad1e552006-05-23 23:22:38 +0200525 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800526 buf = fw_priv->buf;
527 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 retval = -ENODEV;
529 goto out;
530 }
Ming Lei65710cb2012-08-04 12:01:16 +0800531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 retval = fw_realloc_buffer(fw_priv, offset + count);
533 if (retval)
534 goto out;
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700537
538 while (count) {
539 void *page_data;
540 int page_nr = offset >> PAGE_SHIFT;
541 int page_ofs = offset & (PAGE_SIZE - 1);
542 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
543
Ming Lei12446912012-08-04 12:01:20 +0800544 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700545
546 memcpy(page_data + page_ofs, buffer, page_cnt);
547
Ming Lei12446912012-08-04 12:01:20 +0800548 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700549 buffer += page_cnt;
550 offset += page_cnt;
551 count -= page_cnt;
552 }
553
Ming Lei12446912012-08-04 12:01:20 +0800554 buf->size = max_t(size_t, offset, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555out:
Laura Garciacad1e552006-05-23 23:22:38 +0200556 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return retval;
558}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800559
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700560static struct bin_attribute firmware_attr_data = {
561 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 .size = 0,
563 .read = firmware_data_read,
564 .write = firmware_data_write,
565};
566
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700567static void firmware_class_timeout(u_long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 fw_load_abort(fw_priv);
572}
573
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700574static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200575fw_create_instance(struct firmware *firmware, const char *fw_name,
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700576 struct device *device, bool uevent, bool nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700578 struct firmware_priv *fw_priv;
579 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Ming Lei12446912012-08-04 12:01:20 +0800581 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700582 if (!fw_priv) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700583 dev_err(device, "%s: kmalloc failed\n", __func__);
Ming Lei12446912012-08-04 12:01:20 +0800584 fw_priv = ERR_PTR(-ENOMEM);
585 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700588 fw_priv->nowait = nowait;
Ming Lei1f2b7952012-08-04 12:01:21 +0800589 fw_priv->fw = firmware;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700590 setup_timer(&fw_priv->timeout,
591 firmware_class_timeout, (u_long) fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700593 f_dev = &fw_priv->dev;
594
595 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800596 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700597 f_dev->parent = device;
598 f_dev->class = &firmware_class;
Ming Lei12446912012-08-04 12:01:20 +0800599exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700600 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Ming Lei1f2b7952012-08-04 12:01:21 +0800603/* one pages buffer is mapped/unmapped only once */
604static int fw_map_pages_buf(struct firmware_buf *buf)
605{
606 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
607 if (!buf->data)
608 return -ENOMEM;
609 return 0;
610}
611
612/* store the pages buffer info firmware from buf */
613static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
614{
615 fw->priv = buf;
616 fw->pages = buf->pages;
617 fw->size = buf->size;
618 fw->data = buf->data;
619
620 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
621 __func__, buf->fw_id, buf, buf->data,
622 (unsigned int)buf->size);
623}
624
Ming Leif531f05a2012-08-04 12:01:25 +0800625static void fw_name_devm_release(struct device *dev, void *res)
626{
627 struct fw_name_devm *fwn = res;
628
629 if (fwn->magic == (unsigned long)&fw_cache)
630 pr_debug("%s: fw_name-%s devm-%p released\n",
631 __func__, fwn->name, res);
632}
633
634static int fw_devm_match(struct device *dev, void *res,
635 void *match_data)
636{
637 struct fw_name_devm *fwn = res;
638
639 return (fwn->magic == (unsigned long)&fw_cache) &&
640 !strcmp(fwn->name, match_data);
641}
642
643static struct fw_name_devm *fw_find_devm_name(struct device *dev,
644 const char *name)
645{
646 struct fw_name_devm *fwn;
647
648 fwn = devres_find(dev, fw_name_devm_release,
649 fw_devm_match, (void *)name);
650 return fwn;
651}
652
653/* add firmware name into devres list */
654static int fw_add_devm_name(struct device *dev, const char *name)
655{
656 struct fw_name_devm *fwn;
657
658 fwn = fw_find_devm_name(dev, name);
659 if (fwn)
660 return 1;
661
662 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
663 strlen(name) + 1, GFP_KERNEL);
664 if (!fwn)
665 return -ENOMEM;
666
667 fwn->magic = (unsigned long)&fw_cache;
668 strcpy(fwn->name, name);
669 devres_add(dev, fwn);
670
671 return 0;
672}
673
Ming Lei1f2b7952012-08-04 12:01:21 +0800674static void _request_firmware_cleanup(const struct firmware **firmware_p)
675{
676 release_firmware(*firmware_p);
677 *firmware_p = NULL;
678}
679
Stephen Boyddddb5542012-03-28 23:30:43 +0200680static struct firmware_priv *
681_request_firmware_prepare(const struct firmware **firmware_p, const char *name,
682 struct device *device, bool uevent, bool nowait)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700683{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +0800685 struct firmware_priv *fw_priv = NULL;
686 struct firmware_buf *buf;
687 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 if (!firmware_p)
Stephen Boyddddb5542012-03-28 23:30:43 +0200690 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Jiri Slaby4aed0642005-09-13 01:25:01 -0700692 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700694 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
695 __func__);
Stephen Boyddddb5542012-03-28 23:30:43 +0200696 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800699 if (fw_get_builtin_firmware(firmware, name)) {
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100700 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
Stephen Boyddddb5542012-03-28 23:30:43 +0200701 return NULL;
David Woodhouse5658c762008-05-23 13:52:42 +0100702 }
703
Ming Lei1f2b7952012-08-04 12:01:21 +0800704 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
705 if (!ret)
706 fw_priv = fw_create_instance(firmware, name, device,
707 uevent, nowait);
708
709 if (IS_ERR(fw_priv) || ret < 0) {
710 kfree(firmware);
Stephen Boyddddb5542012-03-28 23:30:43 +0200711 *firmware_p = NULL;
Ming Lei1f2b7952012-08-04 12:01:21 +0800712 return ERR_PTR(-ENOMEM);
713 } else if (fw_priv) {
714 fw_priv->buf = buf;
715
716 /*
717 * bind with 'buf' now to avoid warning in failure path
718 * of requesting firmware.
719 */
720 firmware->priv = buf;
721 return fw_priv;
Stephen Boyddddb5542012-03-28 23:30:43 +0200722 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800723
724 /* share the cached buf, which is inprogessing or completed */
725 check_status:
726 mutex_lock(&fw_lock);
727 if (test_bit(FW_STATUS_ABORT, &buf->status)) {
728 fw_priv = ERR_PTR(-ENOENT);
Ming Leief40bb12012-08-20 19:04:15 +0800729 firmware->priv = buf;
Ming Lei1f2b7952012-08-04 12:01:21 +0800730 _request_firmware_cleanup(firmware_p);
731 goto exit;
732 } else if (test_bit(FW_STATUS_DONE, &buf->status)) {
733 fw_priv = NULL;
734 fw_set_page_data(buf, firmware);
735 goto exit;
736 }
737 mutex_unlock(&fw_lock);
738 wait_for_completion(&buf->completion);
739 goto check_status;
740
741exit:
742 mutex_unlock(&fw_lock);
Stephen Boyddddb5542012-03-28 23:30:43 +0200743 return fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200744}
745
Stephen Boyddddb5542012-03-28 23:30:43 +0200746static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
747 long timeout)
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200748{
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200749 int retval = 0;
Stephen Boyddddb5542012-03-28 23:30:43 +0200750 struct device *f_dev = &fw_priv->dev;
Ming Lei12446912012-08-04 12:01:20 +0800751 struct firmware_buf *buf = fw_priv->buf;
Ming Leiac39b3e2012-08-20 19:04:16 +0800752 struct firmware_cache *fwc = &fw_cache;
Linus Torvaldscaca9512011-08-24 15:55:30 -0700753
Stephen Boyddddb5542012-03-28 23:30:43 +0200754 dev_set_uevent_suppress(f_dev, true);
David Woodhouse5658c762008-05-23 13:52:42 +0100755
Stephen Boyddddb5542012-03-28 23:30:43 +0200756 /* Need to pin this module until class device is destroyed */
757 __module_get(THIS_MODULE);
758
759 retval = device_add(f_dev);
760 if (retval) {
761 dev_err(f_dev, "%s: device_register failed\n", __func__);
762 goto err_put_dev;
763 }
764
765 retval = device_create_bin_file(f_dev, &firmware_attr_data);
766 if (retval) {
767 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
768 goto err_del_dev;
769 }
770
771 retval = device_create_file(f_dev, &dev_attr_loading);
772 if (retval) {
773 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
774 goto err_del_bin_attr;
775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Kay Sievers312c0042005-11-16 09:00:00 +0100777 if (uevent) {
Stephen Boyddddb5542012-03-28 23:30:43 +0200778 dev_set_uevent_suppress(f_dev, false);
Ming Lei12446912012-08-04 12:01:20 +0800779 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200780 if (timeout != MAX_SCHEDULE_TIMEOUT)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700781 mod_timer(&fw_priv->timeout,
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200782 round_jiffies_up(jiffies + timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700784 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
785 }
786
Ming Lei12446912012-08-04 12:01:20 +0800787 wait_for_completion(&buf->completion);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700788
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700789 del_timer_sync(&fw_priv->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Laura Garciacad1e552006-05-23 23:22:38 +0200791 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800792 if (!buf->size || test_bit(FW_STATUS_ABORT, &buf->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 retval = -ENOENT;
Ming Lei65710cb2012-08-04 12:01:16 +0800794
Ming Leif531f05a2012-08-04 12:01:25 +0800795 /*
796 * add firmware name into devres list so that we can auto cache
797 * and uncache firmware for device.
798 *
799 * f_dev->parent may has been deleted already, but the problem
800 * should be fixed in devres or driver core.
801 */
802 if (!retval && f_dev->parent)
803 fw_add_devm_name(f_dev->parent, buf->fw_id);
804
Ming Lei65710cb2012-08-04 12:01:16 +0800805 if (!retval)
Ming Lei1f2b7952012-08-04 12:01:21 +0800806 retval = fw_map_pages_buf(buf);
Ming Lei12446912012-08-04 12:01:20 +0800807
Ming Leiac39b3e2012-08-20 19:04:16 +0800808 /*
809 * After caching firmware image is started, let it piggyback
810 * on request firmware.
811 */
812 if (!retval && fwc->state == FW_LOADER_START_CACHE) {
813 if (fw_cache_piggyback_on_request(buf->fw_id))
814 kref_get(&buf->ref);
815 }
816
Ming Lei1f2b7952012-08-04 12:01:21 +0800817 /* pass the pages buffer to driver at the last minute */
818 fw_set_page_data(buf, fw_priv->fw);
819
Ming Lei12446912012-08-04 12:01:20 +0800820 fw_priv->buf = NULL;
Laura Garciacad1e552006-05-23 23:22:38 +0200821 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Stephen Boyddddb5542012-03-28 23:30:43 +0200823 device_remove_file(f_dev, &dev_attr_loading);
824err_del_bin_attr:
825 device_remove_bin_file(f_dev, &firmware_attr_data);
826err_del_dev:
827 device_del(f_dev);
828err_put_dev:
829 put_device(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return retval;
831}
832
833/**
Kay Sievers312c0042005-11-16 09:00:00 +0100834 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800835 * @firmware_p: pointer to firmware image
836 * @name: name of firmware file
837 * @device: device for which firmware is being loaded
838 *
839 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700840 * of @name for device @device.
841 *
842 * Should be called from user context where sleeping is allowed.
843 *
Kay Sievers312c0042005-11-16 09:00:00 +0100844 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700845 * should be distinctive enough not to be confused with any other
846 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +0800847 *
848 * Caller must hold the reference count of @device.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700849 **/
850int
851request_firmware(const struct firmware **firmware_p, const char *name,
852 struct device *device)
853{
Stephen Boyddddb5542012-03-28 23:30:43 +0200854 struct firmware_priv *fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200855 int ret;
856
Stephen Boyddddb5542012-03-28 23:30:43 +0200857 fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
858 false);
859 if (IS_ERR_OR_NULL(fw_priv))
860 return PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200861
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200862 ret = usermodehelper_read_trylock();
863 if (WARN_ON(ret)) {
864 dev_err(device, "firmware: %s will not be loaded\n", name);
865 } else {
Stephen Boyddddb5542012-03-28 23:30:43 +0200866 ret = _request_firmware_load(fw_priv, true,
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200867 firmware_loading_timeout());
868 usermodehelper_read_unlock();
869 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200870 if (ret)
871 _request_firmware_cleanup(firmware_p);
872
873 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700874}
875
876/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800878 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800880void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800883 if (!fw_is_builtin_firmware(fw))
884 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 kfree(fw);
886 }
887}
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889/* Async support */
890struct firmware_work {
891 struct work_struct work;
892 struct module *module;
893 const char *name;
894 struct device *device;
895 void *context;
896 void (*cont)(const struct firmware *fw, void *context);
Bob Liu072fc8f2011-01-26 18:33:32 +0800897 bool uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898};
899
Stephen Boyda36cf842012-03-28 23:31:00 +0200900static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Stephen Boyda36cf842012-03-28 23:31:00 +0200902 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 const struct firmware *fw;
Stephen Boyddddb5542012-03-28 23:30:43 +0200904 struct firmware_priv *fw_priv;
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200905 long timeout;
matthieu castet113fab12005-11-13 16:07:39 -0800906 int ret;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700907
Stephen Boyda36cf842012-03-28 23:31:00 +0200908 fw_work = container_of(work, struct firmware_work, work);
Stephen Boyddddb5542012-03-28 23:30:43 +0200909 fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
910 fw_work->uevent, true);
911 if (IS_ERR_OR_NULL(fw_priv)) {
912 ret = PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200913 goto out;
Stephen Boyddddb5542012-03-28 23:30:43 +0200914 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200915
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200916 timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
917 if (timeout) {
Stephen Boyddddb5542012-03-28 23:30:43 +0200918 ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200919 usermodehelper_read_unlock();
920 } else {
921 dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
922 fw_work->name);
923 ret = -EAGAIN;
924 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200925 if (ret)
926 _request_firmware_cleanup(&fw);
927
928 out:
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100929 fw_work->cont(fw, fw_work->context);
Ming Lei0cfc1e12012-08-04 12:01:23 +0800930 put_device(fw_work->device);
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 module_put(fw_work->module);
933 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
936/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000937 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800938 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +0100939 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800940 * is non-zero else the firmware copy must be done manually.
941 * @name: name of firmware file
942 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100943 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800944 * @context: will be passed over to @cont, and
945 * @fw may be %NULL if firmware request fails.
946 * @cont: function will be called asynchronously when the firmware
947 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 *
Ming Lei0cfc1e12012-08-04 12:01:23 +0800949 * Caller must hold the reference count of @device.
950 *
Ming Lei6f21a622012-08-04 12:01:24 +0800951 * Asynchronous variant of request_firmware() for user contexts:
952 * - sleep for as small periods as possible since it may
953 * increase kernel boot time of built-in device drivers
954 * requesting firmware in their ->probe() methods, if
955 * @gfp is GFP_KERNEL.
956 *
957 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 **/
959int
960request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +0800961 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100962 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 void (*cont)(const struct firmware *fw, void *context))
964{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700965 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700967 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (!fw_work)
969 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700970
971 fw_work->module = module;
972 fw_work->name = name;
973 fw_work->device = device;
974 fw_work->context = context;
975 fw_work->cont = cont;
976 fw_work->uevent = uevent;
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (!try_module_get(module)) {
979 kfree(fw_work);
980 return -EFAULT;
981 }
982
Ming Lei0cfc1e12012-08-04 12:01:23 +0800983 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +0200984 INIT_WORK(&fw_work->work, request_firmware_work_func);
985 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return 0;
987}
988
Ming Lei2887b392012-08-04 12:01:22 +0800989/**
990 * cache_firmware - cache one firmware image in kernel memory space
991 * @fw_name: the firmware image name
992 *
993 * Cache firmware in kernel memory so that drivers can use it when
994 * system isn't ready for them to request firmware image from userspace.
995 * Once it returns successfully, driver can use request_firmware or its
996 * nowait version to get the cached firmware without any interacting
997 * with userspace
998 *
999 * Return 0 if the firmware image has been cached successfully
1000 * Return !0 otherwise
1001 *
1002 */
1003int cache_firmware(const char *fw_name)
1004{
1005 int ret;
1006 const struct firmware *fw;
1007
1008 pr_debug("%s: %s\n", __func__, fw_name);
1009
1010 ret = request_firmware(&fw, fw_name, NULL);
1011 if (!ret)
1012 kfree(fw);
1013
1014 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1015
1016 return ret;
1017}
1018
1019/**
1020 * uncache_firmware - remove one cached firmware image
1021 * @fw_name: the firmware image name
1022 *
1023 * Uncache one firmware image which has been cached successfully
1024 * before.
1025 *
1026 * Return 0 if the firmware cache has been removed successfully
1027 * Return !0 otherwise
1028 *
1029 */
1030int uncache_firmware(const char *fw_name)
1031{
1032 struct firmware_buf *buf;
1033 struct firmware fw;
1034
1035 pr_debug("%s: %s\n", __func__, fw_name);
1036
1037 if (fw_get_builtin_firmware(&fw, fw_name))
1038 return 0;
1039
1040 buf = fw_lookup_buf(fw_name);
1041 if (buf) {
1042 fw_free_buf(buf);
1043 return 0;
1044 }
1045
1046 return -EINVAL;
1047}
1048
Ming Lei37276a52012-08-04 12:01:27 +08001049static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1050{
1051 struct fw_cache_entry *fce;
1052
1053 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1054 if (!fce)
1055 goto exit;
1056
1057 strcpy(fce->name, name);
1058exit:
1059 return fce;
1060}
1061
Ming Leiac39b3e2012-08-20 19:04:16 +08001062static int fw_cache_piggyback_on_request(const char *name)
1063{
1064 struct firmware_cache *fwc = &fw_cache;
1065 struct fw_cache_entry *fce;
1066 int ret = 0;
1067
1068 spin_lock(&fwc->name_lock);
1069 list_for_each_entry(fce, &fwc->fw_names, list) {
1070 if (!strcmp(fce->name, name))
1071 goto found;
1072 }
1073
1074 fce = alloc_fw_cache_entry(name);
1075 if (fce) {
1076 ret = 1;
1077 list_add(&fce->list, &fwc->fw_names);
1078 pr_debug("%s: fw: %s\n", __func__, name);
1079 }
1080found:
1081 spin_unlock(&fwc->name_lock);
1082 return ret;
1083}
1084
Ming Lei37276a52012-08-04 12:01:27 +08001085static void free_fw_cache_entry(struct fw_cache_entry *fce)
1086{
1087 kfree(fce);
1088}
1089
1090static void __async_dev_cache_fw_image(void *fw_entry,
1091 async_cookie_t cookie)
1092{
1093 struct fw_cache_entry *fce = fw_entry;
1094 struct firmware_cache *fwc = &fw_cache;
1095 int ret;
1096
1097 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001098 if (ret) {
1099 spin_lock(&fwc->name_lock);
1100 list_del(&fce->list);
1101 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001102
Ming Leiac39b3e2012-08-20 19:04:16 +08001103 free_fw_cache_entry(fce);
1104 }
Ming Lei37276a52012-08-04 12:01:27 +08001105
Ming Lei37276a52012-08-04 12:01:27 +08001106 spin_lock(&fwc->name_lock);
1107 fwc->cnt--;
1108 spin_unlock(&fwc->name_lock);
1109
1110 wake_up(&fwc->wait_queue);
1111}
1112
1113/* called with dev->devres_lock held */
1114static void dev_create_fw_entry(struct device *dev, void *res,
1115 void *data)
1116{
1117 struct fw_name_devm *fwn = res;
1118 const char *fw_name = fwn->name;
1119 struct list_head *head = data;
1120 struct fw_cache_entry *fce;
1121
1122 fce = alloc_fw_cache_entry(fw_name);
1123 if (fce)
1124 list_add(&fce->list, head);
1125}
1126
1127static int devm_name_match(struct device *dev, void *res,
1128 void *match_data)
1129{
1130 struct fw_name_devm *fwn = res;
1131 return (fwn->magic == (unsigned long)match_data);
1132}
1133
Ming Leiab6dd8e2012-08-17 22:07:00 +08001134static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001135{
1136 LIST_HEAD(todo);
1137 struct fw_cache_entry *fce;
1138 struct fw_cache_entry *fce_next;
1139 struct firmware_cache *fwc = &fw_cache;
1140
1141 devres_for_each_res(dev, fw_name_devm_release,
1142 devm_name_match, &fw_cache,
1143 dev_create_fw_entry, &todo);
1144
1145 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1146 list_del(&fce->list);
1147
1148 spin_lock(&fwc->name_lock);
1149 fwc->cnt++;
Ming Leiac39b3e2012-08-20 19:04:16 +08001150 list_add(&fce->list, &fwc->fw_names);
Ming Lei37276a52012-08-04 12:01:27 +08001151 spin_unlock(&fwc->name_lock);
1152
1153 async_schedule(__async_dev_cache_fw_image, (void *)fce);
1154 }
1155}
1156
1157static void __device_uncache_fw_images(void)
1158{
1159 struct firmware_cache *fwc = &fw_cache;
1160 struct fw_cache_entry *fce;
1161
1162 spin_lock(&fwc->name_lock);
1163 while (!list_empty(&fwc->fw_names)) {
1164 fce = list_entry(fwc->fw_names.next,
1165 struct fw_cache_entry, list);
1166 list_del(&fce->list);
1167 spin_unlock(&fwc->name_lock);
1168
1169 uncache_firmware(fce->name);
1170 free_fw_cache_entry(fce);
1171
1172 spin_lock(&fwc->name_lock);
1173 }
1174 spin_unlock(&fwc->name_lock);
1175}
1176
1177/**
1178 * device_cache_fw_images - cache devices' firmware
1179 *
1180 * If one device called request_firmware or its nowait version
1181 * successfully before, the firmware names are recored into the
1182 * device's devres link list, so device_cache_fw_images can call
1183 * cache_firmware() to cache these firmwares for the device,
1184 * then the device driver can load its firmwares easily at
1185 * time when system is not ready to complete loading firmware.
1186 */
1187static void device_cache_fw_images(void)
1188{
1189 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001190 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001191 DEFINE_WAIT(wait);
1192
1193 pr_debug("%s\n", __func__);
1194
Ming Leiffe53f62012-08-04 12:01:28 +08001195 /*
1196 * use small loading timeout for caching devices' firmware
1197 * because all these firmware images have been loaded
1198 * successfully at lease once, also system is ready for
1199 * completing firmware loading now. The maximum size of
1200 * firmware in current distributions is about 2M bytes,
1201 * so 10 secs should be enough.
1202 */
1203 old_timeout = loading_timeout;
1204 loading_timeout = 10;
1205
Ming Leiac39b3e2012-08-20 19:04:16 +08001206 mutex_lock(&fw_lock);
1207 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001208 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001209 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001210
1211 /* wait for completion of caching firmware for all devices */
1212 spin_lock(&fwc->name_lock);
1213 for (;;) {
1214 prepare_to_wait(&fwc->wait_queue, &wait,
1215 TASK_UNINTERRUPTIBLE);
1216 if (!fwc->cnt)
1217 break;
1218
1219 spin_unlock(&fwc->name_lock);
1220
1221 schedule();
1222
1223 spin_lock(&fwc->name_lock);
1224 }
1225 spin_unlock(&fwc->name_lock);
1226 finish_wait(&fwc->wait_queue, &wait);
Ming Leiffe53f62012-08-04 12:01:28 +08001227
1228 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001229}
1230
1231/**
1232 * device_uncache_fw_images - uncache devices' firmware
1233 *
1234 * uncache all firmwares which have been cached successfully
1235 * by device_uncache_fw_images earlier
1236 */
1237static void device_uncache_fw_images(void)
1238{
1239 pr_debug("%s\n", __func__);
1240 __device_uncache_fw_images();
1241}
1242
1243static void device_uncache_fw_images_work(struct work_struct *work)
1244{
1245 device_uncache_fw_images();
1246}
1247
1248/**
1249 * device_uncache_fw_images_delay - uncache devices firmwares
1250 * @delay: number of milliseconds to delay uncache device firmwares
1251 *
1252 * uncache all devices's firmwares which has been cached successfully
1253 * by device_cache_fw_images after @delay milliseconds.
1254 */
1255static void device_uncache_fw_images_delay(unsigned long delay)
1256{
1257 schedule_delayed_work(&fw_cache.work,
1258 msecs_to_jiffies(delay));
1259}
1260
Ming Lei07646d92012-08-04 12:01:29 +08001261#ifdef CONFIG_PM
1262static int fw_pm_notify(struct notifier_block *notify_block,
1263 unsigned long mode, void *unused)
1264{
1265 switch (mode) {
1266 case PM_HIBERNATION_PREPARE:
1267 case PM_SUSPEND_PREPARE:
1268 device_cache_fw_images();
1269 break;
1270
1271 case PM_POST_SUSPEND:
1272 case PM_POST_HIBERNATION:
1273 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001274 /*
1275 * In case that system sleep failed and syscore_suspend is
1276 * not called.
1277 */
1278 mutex_lock(&fw_lock);
1279 fw_cache.state = FW_LOADER_NO_CACHE;
1280 mutex_unlock(&fw_lock);
1281
Ming Lei07646d92012-08-04 12:01:29 +08001282 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1283 break;
1284 }
1285
1286 return 0;
1287}
1288#else
1289static int fw_pm_notify(struct notifier_block *notify_block,
1290 unsigned long mode, void *unused)
Ming Leic08f6772012-08-17 22:06:58 +08001291{
1292 return 0;
1293}
Ming Lei07646d92012-08-04 12:01:29 +08001294#endif
1295
Ming Leiac39b3e2012-08-20 19:04:16 +08001296/* stop caching firmware once syscore_suspend is reached */
1297static int fw_suspend(void)
1298{
1299 fw_cache.state = FW_LOADER_NO_CACHE;
1300 return 0;
1301}
1302
1303static struct syscore_ops fw_syscore_ops = {
1304 .suspend = fw_suspend,
1305};
1306
Ming Lei37276a52012-08-04 12:01:27 +08001307static void __init fw_cache_init(void)
1308{
1309 spin_lock_init(&fw_cache.lock);
1310 INIT_LIST_HEAD(&fw_cache.head);
1311
1312 spin_lock_init(&fw_cache.name_lock);
1313 INIT_LIST_HEAD(&fw_cache.fw_names);
1314 fw_cache.cnt = 0;
Ming Leiac39b3e2012-08-20 19:04:16 +08001315 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001316
1317 init_waitqueue_head(&fw_cache.wait_queue);
1318 INIT_DELAYED_WORK(&fw_cache.work,
1319 device_uncache_fw_images_work);
Ming Lei07646d92012-08-04 12:01:29 +08001320
1321 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1322 register_pm_notifier(&fw_cache.pm_notify);
Ming Leiac39b3e2012-08-20 19:04:16 +08001323
1324 register_syscore_ops(&fw_syscore_ops);
Ming Lei37276a52012-08-04 12:01:27 +08001325}
1326
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001327static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
Ming Lei1f2b7952012-08-04 12:01:21 +08001329 fw_cache_init();
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001330 return class_register(&firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001332
1333static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
Ming Leiac39b3e2012-08-20 19:04:16 +08001335 unregister_syscore_ops(&fw_syscore_ops);
Ming Lei07646d92012-08-04 12:01:29 +08001336 unregister_pm_notifier(&fw_cache.pm_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 class_unregister(&firmware_class);
1338}
1339
Shaohua Lia30a6a22006-09-27 01:50:52 -07001340fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341module_exit(firmware_class_exit);
1342
1343EXPORT_SYMBOL(release_firmware);
1344EXPORT_SYMBOL(request_firmware);
1345EXPORT_SYMBOL(request_firmware_nowait);
Ming Lei2887b392012-08-04 12:01:22 +08001346EXPORT_SYMBOL_GPL(cache_firmware);
1347EXPORT_SYMBOL_GPL(uncache_firmware);