blob: 65c60666685b0dac79ff2f70735f2e696e9e8d11 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Markus Rechberger87d37a42007-06-04 18:45:44 +020026MODULE_AUTHOR("Manuel Estrada Sainz");
Linus Torvalds1da177e2005-04-16 15:20:36 -070027MODULE_DESCRIPTION("Multi purpose firmware loading support");
28MODULE_LICENSE("GPL");
29
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080030/* Builtin firmware support */
31
32#ifdef CONFIG_FW_LOADER
33
34extern struct builtin_fw __start_builtin_fw[];
35extern struct builtin_fw __end_builtin_fw[];
36
37static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
38{
39 struct builtin_fw *b_fw;
40
41 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
42 if (strcmp(name, b_fw->name) == 0) {
43 fw->size = b_fw->size;
44 fw->data = b_fw->data;
45 return true;
46 }
47 }
48
49 return false;
50}
51
52static bool fw_is_builtin_firmware(const struct firmware *fw)
53{
54 struct builtin_fw *b_fw;
55
56 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
57 if (fw->data == b_fw->data)
58 return true;
59
60 return false;
61}
62
63#else /* Module case - no builtin firmware support */
64
65static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
66{
67 return false;
68}
69
70static inline bool fw_is_builtin_firmware(const struct firmware *fw)
71{
72 return false;
73}
74#endif
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076enum {
77 FW_STATUS_LOADING,
78 FW_STATUS_DONE,
79 FW_STATUS_ABORT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080};
81
Dave Jones2f651682007-01-25 15:56:15 -050082static int loading_timeout = 60; /* In seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +020084static inline long firmware_loading_timeout(void)
85{
86 return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
87}
88
Ming Lei1f2b7952012-08-04 12:01:21 +080089struct firmware_cache {
90 /* firmware_buf instance will be added into the below list */
91 spinlock_t lock;
92 struct list_head head;
93};
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Ming Lei12446912012-08-04 12:01:20 +080095struct firmware_buf {
Ming Lei1f2b7952012-08-04 12:01:21 +080096 struct kref ref;
97 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 struct completion completion;
Ming Lei1f2b7952012-08-04 12:01:21 +080099 struct firmware_cache *fwc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 unsigned long status;
Ming Lei65710cb2012-08-04 12:01:16 +0800101 void *data;
102 size_t size;
David Woodhouse6e03a202009-04-09 22:04:07 -0700103 struct page **pages;
104 int nr_pages;
105 int page_array_size;
Dmitry Torokhove1771232010-03-13 23:49:23 -0800106 char fw_id[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
108
Ming Lei12446912012-08-04 12:01:20 +0800109struct firmware_priv {
110 struct timer_list timeout;
111 bool nowait;
112 struct device dev;
113 struct firmware_buf *buf;
Ming Lei1f2b7952012-08-04 12:01:21 +0800114 struct firmware *fw;
Ming Lei12446912012-08-04 12:01:20 +0800115};
116
Ming Leif531f05a2012-08-04 12:01:25 +0800117struct fw_name_devm {
118 unsigned long magic;
119 char name[];
120};
121
Ming Lei1f2b7952012-08-04 12:01:21 +0800122#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
123
124/* fw_lock could be moved to 'struct firmware_priv' but since it is just
125 * guarding for corner cases a global lock should be OK */
126static DEFINE_MUTEX(fw_lock);
127
128static struct firmware_cache fw_cache;
129
130static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
131 struct firmware_cache *fwc)
132{
133 struct firmware_buf *buf;
134
135 buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
136
137 if (!buf)
138 return buf;
139
140 kref_init(&buf->ref);
141 strcpy(buf->fw_id, fw_name);
142 buf->fwc = fwc;
143 init_completion(&buf->completion);
144
145 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
146
147 return buf;
148}
149
Ming Lei2887b392012-08-04 12:01:22 +0800150static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
151{
152 struct firmware_buf *tmp;
153 struct firmware_cache *fwc = &fw_cache;
154
155 list_for_each_entry(tmp, &fwc->head, list)
156 if (!strcmp(tmp->fw_id, fw_name))
157 return tmp;
158 return NULL;
159}
160
Ming Lei1f2b7952012-08-04 12:01:21 +0800161static int fw_lookup_and_allocate_buf(const char *fw_name,
162 struct firmware_cache *fwc,
163 struct firmware_buf **buf)
164{
165 struct firmware_buf *tmp;
166
167 spin_lock(&fwc->lock);
Ming Lei2887b392012-08-04 12:01:22 +0800168 tmp = __fw_lookup_buf(fw_name);
169 if (tmp) {
170 kref_get(&tmp->ref);
171 spin_unlock(&fwc->lock);
172 *buf = tmp;
173 return 1;
174 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800175 tmp = __allocate_fw_buf(fw_name, fwc);
176 if (tmp)
177 list_add(&tmp->list, &fwc->head);
178 spin_unlock(&fwc->lock);
179
180 *buf = tmp;
181
182 return tmp ? 0 : -ENOMEM;
183}
184
Ming Lei2887b392012-08-04 12:01:22 +0800185static struct firmware_buf *fw_lookup_buf(const char *fw_name)
186{
187 struct firmware_buf *tmp;
188 struct firmware_cache *fwc = &fw_cache;
189
190 spin_lock(&fwc->lock);
191 tmp = __fw_lookup_buf(fw_name);
192 spin_unlock(&fwc->lock);
193
194 return tmp;
195}
196
Ming Lei1f2b7952012-08-04 12:01:21 +0800197static void __fw_free_buf(struct kref *ref)
198{
199 struct firmware_buf *buf = to_fwbuf(ref);
200 struct firmware_cache *fwc = buf->fwc;
201 int i;
202
203 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
204 __func__, buf->fw_id, buf, buf->data,
205 (unsigned int)buf->size);
206
207 spin_lock(&fwc->lock);
208 list_del(&buf->list);
209 spin_unlock(&fwc->lock);
210
211 vunmap(buf->data);
212 for (i = 0; i < buf->nr_pages; i++)
213 __free_page(buf->pages[i]);
214 kfree(buf->pages);
215 kfree(buf);
216}
217
218static void fw_free_buf(struct firmware_buf *buf)
219{
220 kref_put(&buf->ref, __fw_free_buf);
221}
222
223static void __init fw_cache_init(void)
224{
225 spin_lock_init(&fw_cache.lock);
226 INIT_LIST_HEAD(&fw_cache.head);
227}
228
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700229static struct firmware_priv *to_firmware_priv(struct device *dev)
230{
231 return container_of(dev, struct firmware_priv, dev);
232}
233
234static void fw_load_abort(struct firmware_priv *fw_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Ming Lei12446912012-08-04 12:01:20 +0800236 struct firmware_buf *buf = fw_priv->buf;
237
238 set_bit(FW_STATUS_ABORT, &buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800239 complete_all(&buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700242static ssize_t firmware_timeout_show(struct class *class,
243 struct class_attribute *attr,
244 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 return sprintf(buf, "%d\n", loading_timeout);
247}
248
249/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800250 * firmware_timeout_store - set number of seconds to wait for firmware
251 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800252 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800253 * @buf: buffer to scan for timeout value
254 * @count: number of bytes in @buf
255 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800257 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 * firmware will be provided.
259 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800260 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700262static ssize_t firmware_timeout_store(struct class *class,
263 struct class_attribute *attr,
264 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700267 if (loading_timeout < 0)
268 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return count;
271}
272
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800273static struct class_attribute firmware_class_attrs[] = {
274 __ATTR(timeout, S_IWUSR | S_IRUGO,
275 firmware_timeout_show, firmware_timeout_store),
276 __ATTR_NULL
277};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800279static void fw_dev_release(struct device *dev)
280{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700281 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800282
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800283 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800284
285 module_put(THIS_MODULE);
286}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Kay Sievers7eff2e72007-08-14 15:15:12 +0200288static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700290 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Ming Lei12446912012-08-04 12:01:20 +0800292 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200294 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700295 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200296 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
297 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 return 0;
300}
301
Adrian Bunk1b81d662006-05-20 15:00:16 -0700302static struct class firmware_class = {
303 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800304 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700305 .dev_uevent = firmware_uevent,
306 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700307};
308
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700309static ssize_t firmware_loading_show(struct device *dev,
310 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700312 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800313 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return sprintf(buf, "%d\n", loading);
316}
317
Ming Lei65710cb2012-08-04 12:01:16 +0800318/* firmware holds the ownership of pages */
David Woodhousedd336c52010-05-02 11:21:21 +0300319static void firmware_free_data(const struct firmware *fw)
320{
Ming Lei1f2b7952012-08-04 12:01:21 +0800321 WARN_ON(!fw->priv);
322 fw_free_buf(fw->priv);
David Woodhousedd336c52010-05-02 11:21:21 +0300323}
324
David Woodhouse6e03a202009-04-09 22:04:07 -0700325/* Some architectures don't have PAGE_KERNEL_RO */
326#ifndef PAGE_KERNEL_RO
327#define PAGE_KERNEL_RO PAGE_KERNEL
328#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800330 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700331 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800332 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800333 * @buf: buffer to scan for loading control value
334 * @count: number of bytes in @buf
335 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 * The relevant values are:
337 *
338 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800339 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 * -1: Conclude the load with an error and discard any written data.
341 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700342static ssize_t firmware_loading_store(struct device *dev,
343 struct device_attribute *attr,
344 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700346 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800347 struct firmware_buf *fw_buf = fw_priv->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700349 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Neil Hormaneea915b2012-01-02 15:31:23 -0500351 mutex_lock(&fw_lock);
352
Ming Lei12446912012-08-04 12:01:20 +0800353 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500354 goto out;
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 switch (loading) {
357 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800358 /* discarding any previous partial load */
Ming Lei12446912012-08-04 12:01:20 +0800359 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
360 for (i = 0; i < fw_buf->nr_pages; i++)
361 __free_page(fw_buf->pages[i]);
362 kfree(fw_buf->pages);
363 fw_buf->pages = NULL;
364 fw_buf->page_array_size = 0;
365 fw_buf->nr_pages = 0;
366 set_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei28eefa72012-08-04 12:01:17 +0800367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 break;
369 case 0:
Ming Lei12446912012-08-04 12:01:20 +0800370 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
371 set_bit(FW_STATUS_DONE, &fw_buf->status);
372 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800373 complete_all(&fw_buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 break;
375 }
376 /* fallthrough */
377 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700378 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /* fallthrough */
380 case -1:
381 fw_load_abort(fw_priv);
382 break;
383 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500384out:
385 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return count;
387}
388
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700389static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700391static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
392 struct bin_attribute *bin_attr,
393 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200395 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700396 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800397 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700398 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Laura Garciacad1e552006-05-23 23:22:38 +0200400 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800401 buf = fw_priv->buf;
402 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 ret_count = -ENODEV;
404 goto out;
405 }
Ming Lei12446912012-08-04 12:01:20 +0800406 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200407 ret_count = 0;
408 goto out;
409 }
Ming Lei12446912012-08-04 12:01:20 +0800410 if (count > buf->size - offset)
411 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700412
413 ret_count = count;
414
415 while (count) {
416 void *page_data;
417 int page_nr = offset >> PAGE_SHIFT;
418 int page_ofs = offset & (PAGE_SIZE-1);
419 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
420
Ming Lei12446912012-08-04 12:01:20 +0800421 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700422
423 memcpy(buffer, page_data + page_ofs, page_cnt);
424
Ming Lei12446912012-08-04 12:01:20 +0800425 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700426 buffer += page_cnt;
427 offset += page_cnt;
428 count -= page_cnt;
429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430out:
Laura Garciacad1e552006-05-23 23:22:38 +0200431 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return ret_count;
433}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800434
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700435static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Ming Lei12446912012-08-04 12:01:20 +0800437 struct firmware_buf *buf = fw_priv->buf;
David Woodhouse6e03a202009-04-09 22:04:07 -0700438 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
David Woodhouse6e03a202009-04-09 22:04:07 -0700440 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800441 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700442 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800443 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700444 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
David Woodhouse6e03a202009-04-09 22:04:07 -0700446 new_pages = kmalloc(new_array_size * sizeof(void *),
447 GFP_KERNEL);
448 if (!new_pages) {
449 fw_load_abort(fw_priv);
450 return -ENOMEM;
451 }
Ming Lei12446912012-08-04 12:01:20 +0800452 memcpy(new_pages, buf->pages,
453 buf->page_array_size * sizeof(void *));
454 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
455 (new_array_size - buf->page_array_size));
456 kfree(buf->pages);
457 buf->pages = new_pages;
458 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700460
Ming Lei12446912012-08-04 12:01:20 +0800461 while (buf->nr_pages < pages_needed) {
462 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700463 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
464
Ming Lei12446912012-08-04 12:01:20 +0800465 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700466 fw_load_abort(fw_priv);
467 return -ENOMEM;
468 }
Ming Lei12446912012-08-04 12:01:20 +0800469 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return 0;
472}
473
474/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800475 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700476 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700477 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700478 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800479 * @buffer: buffer being written
480 * @offset: buffer offset for write in total data store area
481 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800483 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 * the driver as a firmware image.
485 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700486static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
487 struct bin_attribute *bin_attr,
488 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200490 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700491 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800492 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 ssize_t retval;
494
495 if (!capable(CAP_SYS_RAWIO))
496 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700497
Laura Garciacad1e552006-05-23 23:22:38 +0200498 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800499 buf = fw_priv->buf;
500 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 retval = -ENODEV;
502 goto out;
503 }
Ming Lei65710cb2012-08-04 12:01:16 +0800504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 retval = fw_realloc_buffer(fw_priv, offset + count);
506 if (retval)
507 goto out;
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700510
511 while (count) {
512 void *page_data;
513 int page_nr = offset >> PAGE_SHIFT;
514 int page_ofs = offset & (PAGE_SIZE - 1);
515 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
516
Ming Lei12446912012-08-04 12:01:20 +0800517 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700518
519 memcpy(page_data + page_ofs, buffer, page_cnt);
520
Ming Lei12446912012-08-04 12:01:20 +0800521 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700522 buffer += page_cnt;
523 offset += page_cnt;
524 count -= page_cnt;
525 }
526
Ming Lei12446912012-08-04 12:01:20 +0800527 buf->size = max_t(size_t, offset, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528out:
Laura Garciacad1e552006-05-23 23:22:38 +0200529 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return retval;
531}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800532
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700533static struct bin_attribute firmware_attr_data = {
534 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 .size = 0,
536 .read = firmware_data_read,
537 .write = firmware_data_write,
538};
539
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700540static void firmware_class_timeout(u_long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 fw_load_abort(fw_priv);
545}
546
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700547static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200548fw_create_instance(struct firmware *firmware, const char *fw_name,
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700549 struct device *device, bool uevent, bool nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700551 struct firmware_priv *fw_priv;
552 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Ming Lei12446912012-08-04 12:01:20 +0800554 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700555 if (!fw_priv) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700556 dev_err(device, "%s: kmalloc failed\n", __func__);
Ming Lei12446912012-08-04 12:01:20 +0800557 fw_priv = ERR_PTR(-ENOMEM);
558 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700561 fw_priv->nowait = nowait;
Ming Lei1f2b7952012-08-04 12:01:21 +0800562 fw_priv->fw = firmware;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700563 setup_timer(&fw_priv->timeout,
564 firmware_class_timeout, (u_long) fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700566 f_dev = &fw_priv->dev;
567
568 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800569 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700570 f_dev->parent = device;
571 f_dev->class = &firmware_class;
Ming Lei12446912012-08-04 12:01:20 +0800572exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700573 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Ming Lei1f2b7952012-08-04 12:01:21 +0800576/* one pages buffer is mapped/unmapped only once */
577static int fw_map_pages_buf(struct firmware_buf *buf)
578{
579 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
580 if (!buf->data)
581 return -ENOMEM;
582 return 0;
583}
584
585/* store the pages buffer info firmware from buf */
586static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
587{
588 fw->priv = buf;
589 fw->pages = buf->pages;
590 fw->size = buf->size;
591 fw->data = buf->data;
592
593 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
594 __func__, buf->fw_id, buf, buf->data,
595 (unsigned int)buf->size);
596}
597
Ming Leif531f05a2012-08-04 12:01:25 +0800598static void fw_name_devm_release(struct device *dev, void *res)
599{
600 struct fw_name_devm *fwn = res;
601
602 if (fwn->magic == (unsigned long)&fw_cache)
603 pr_debug("%s: fw_name-%s devm-%p released\n",
604 __func__, fwn->name, res);
605}
606
607static int fw_devm_match(struct device *dev, void *res,
608 void *match_data)
609{
610 struct fw_name_devm *fwn = res;
611
612 return (fwn->magic == (unsigned long)&fw_cache) &&
613 !strcmp(fwn->name, match_data);
614}
615
616static struct fw_name_devm *fw_find_devm_name(struct device *dev,
617 const char *name)
618{
619 struct fw_name_devm *fwn;
620
621 fwn = devres_find(dev, fw_name_devm_release,
622 fw_devm_match, (void *)name);
623 return fwn;
624}
625
626/* add firmware name into devres list */
627static int fw_add_devm_name(struct device *dev, const char *name)
628{
629 struct fw_name_devm *fwn;
630
631 fwn = fw_find_devm_name(dev, name);
632 if (fwn)
633 return 1;
634
635 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
636 strlen(name) + 1, GFP_KERNEL);
637 if (!fwn)
638 return -ENOMEM;
639
640 fwn->magic = (unsigned long)&fw_cache;
641 strcpy(fwn->name, name);
642 devres_add(dev, fwn);
643
644 return 0;
645}
646
Ming Lei1f2b7952012-08-04 12:01:21 +0800647static void _request_firmware_cleanup(const struct firmware **firmware_p)
648{
649 release_firmware(*firmware_p);
650 *firmware_p = NULL;
651}
652
Stephen Boyddddb5542012-03-28 23:30:43 +0200653static struct firmware_priv *
654_request_firmware_prepare(const struct firmware **firmware_p, const char *name,
655 struct device *device, bool uevent, bool nowait)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700656{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +0800658 struct firmware_priv *fw_priv = NULL;
659 struct firmware_buf *buf;
660 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 if (!firmware_p)
Stephen Boyddddb5542012-03-28 23:30:43 +0200663 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Jiri Slaby4aed0642005-09-13 01:25:01 -0700665 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700667 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
668 __func__);
Stephen Boyddddb5542012-03-28 23:30:43 +0200669 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800672 if (fw_get_builtin_firmware(firmware, name)) {
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100673 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
Stephen Boyddddb5542012-03-28 23:30:43 +0200674 return NULL;
David Woodhouse5658c762008-05-23 13:52:42 +0100675 }
676
Ming Lei1f2b7952012-08-04 12:01:21 +0800677 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
678 if (!ret)
679 fw_priv = fw_create_instance(firmware, name, device,
680 uevent, nowait);
681
682 if (IS_ERR(fw_priv) || ret < 0) {
683 kfree(firmware);
Stephen Boyddddb5542012-03-28 23:30:43 +0200684 *firmware_p = NULL;
Ming Lei1f2b7952012-08-04 12:01:21 +0800685 return ERR_PTR(-ENOMEM);
686 } else if (fw_priv) {
687 fw_priv->buf = buf;
688
689 /*
690 * bind with 'buf' now to avoid warning in failure path
691 * of requesting firmware.
692 */
693 firmware->priv = buf;
694 return fw_priv;
Stephen Boyddddb5542012-03-28 23:30:43 +0200695 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800696
697 /* share the cached buf, which is inprogessing or completed */
698 check_status:
699 mutex_lock(&fw_lock);
700 if (test_bit(FW_STATUS_ABORT, &buf->status)) {
701 fw_priv = ERR_PTR(-ENOENT);
702 _request_firmware_cleanup(firmware_p);
703 goto exit;
704 } else if (test_bit(FW_STATUS_DONE, &buf->status)) {
705 fw_priv = NULL;
706 fw_set_page_data(buf, firmware);
707 goto exit;
708 }
709 mutex_unlock(&fw_lock);
710 wait_for_completion(&buf->completion);
711 goto check_status;
712
713exit:
714 mutex_unlock(&fw_lock);
Stephen Boyddddb5542012-03-28 23:30:43 +0200715 return fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200716}
717
Stephen Boyddddb5542012-03-28 23:30:43 +0200718static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
719 long timeout)
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200720{
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200721 int retval = 0;
Stephen Boyddddb5542012-03-28 23:30:43 +0200722 struct device *f_dev = &fw_priv->dev;
Ming Lei12446912012-08-04 12:01:20 +0800723 struct firmware_buf *buf = fw_priv->buf;
Linus Torvaldscaca9512011-08-24 15:55:30 -0700724
Stephen Boyddddb5542012-03-28 23:30:43 +0200725 dev_set_uevent_suppress(f_dev, true);
David Woodhouse5658c762008-05-23 13:52:42 +0100726
Stephen Boyddddb5542012-03-28 23:30:43 +0200727 /* Need to pin this module until class device is destroyed */
728 __module_get(THIS_MODULE);
729
730 retval = device_add(f_dev);
731 if (retval) {
732 dev_err(f_dev, "%s: device_register failed\n", __func__);
733 goto err_put_dev;
734 }
735
736 retval = device_create_bin_file(f_dev, &firmware_attr_data);
737 if (retval) {
738 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
739 goto err_del_dev;
740 }
741
742 retval = device_create_file(f_dev, &dev_attr_loading);
743 if (retval) {
744 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
745 goto err_del_bin_attr;
746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Kay Sievers312c0042005-11-16 09:00:00 +0100748 if (uevent) {
Stephen Boyddddb5542012-03-28 23:30:43 +0200749 dev_set_uevent_suppress(f_dev, false);
Ming Lei12446912012-08-04 12:01:20 +0800750 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200751 if (timeout != MAX_SCHEDULE_TIMEOUT)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700752 mod_timer(&fw_priv->timeout,
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200753 round_jiffies_up(jiffies + timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700755 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
756 }
757
Ming Lei12446912012-08-04 12:01:20 +0800758 wait_for_completion(&buf->completion);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700759
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700760 del_timer_sync(&fw_priv->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Laura Garciacad1e552006-05-23 23:22:38 +0200762 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800763 if (!buf->size || test_bit(FW_STATUS_ABORT, &buf->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 retval = -ENOENT;
Ming Lei65710cb2012-08-04 12:01:16 +0800765
Ming Leif531f05a2012-08-04 12:01:25 +0800766 /*
767 * add firmware name into devres list so that we can auto cache
768 * and uncache firmware for device.
769 *
770 * f_dev->parent may has been deleted already, but the problem
771 * should be fixed in devres or driver core.
772 */
773 if (!retval && f_dev->parent)
774 fw_add_devm_name(f_dev->parent, buf->fw_id);
775
Ming Lei65710cb2012-08-04 12:01:16 +0800776 if (!retval)
Ming Lei1f2b7952012-08-04 12:01:21 +0800777 retval = fw_map_pages_buf(buf);
Ming Lei12446912012-08-04 12:01:20 +0800778
Ming Lei1f2b7952012-08-04 12:01:21 +0800779 /* pass the pages buffer to driver at the last minute */
780 fw_set_page_data(buf, fw_priv->fw);
781
Ming Lei12446912012-08-04 12:01:20 +0800782 fw_priv->buf = NULL;
Laura Garciacad1e552006-05-23 23:22:38 +0200783 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Stephen Boyddddb5542012-03-28 23:30:43 +0200785 device_remove_file(f_dev, &dev_attr_loading);
786err_del_bin_attr:
787 device_remove_bin_file(f_dev, &firmware_attr_data);
788err_del_dev:
789 device_del(f_dev);
790err_put_dev:
791 put_device(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return retval;
793}
794
795/**
Kay Sievers312c0042005-11-16 09:00:00 +0100796 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800797 * @firmware_p: pointer to firmware image
798 * @name: name of firmware file
799 * @device: device for which firmware is being loaded
800 *
801 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700802 * of @name for device @device.
803 *
804 * Should be called from user context where sleeping is allowed.
805 *
Kay Sievers312c0042005-11-16 09:00:00 +0100806 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700807 * should be distinctive enough not to be confused with any other
808 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +0800809 *
810 * Caller must hold the reference count of @device.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700811 **/
812int
813request_firmware(const struct firmware **firmware_p, const char *name,
814 struct device *device)
815{
Stephen Boyddddb5542012-03-28 23:30:43 +0200816 struct firmware_priv *fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200817 int ret;
818
Stephen Boyddddb5542012-03-28 23:30:43 +0200819 fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
820 false);
821 if (IS_ERR_OR_NULL(fw_priv))
822 return PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200823
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200824 ret = usermodehelper_read_trylock();
825 if (WARN_ON(ret)) {
826 dev_err(device, "firmware: %s will not be loaded\n", name);
827 } else {
Stephen Boyddddb5542012-03-28 23:30:43 +0200828 ret = _request_firmware_load(fw_priv, true,
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200829 firmware_loading_timeout());
830 usermodehelper_read_unlock();
831 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200832 if (ret)
833 _request_firmware_cleanup(firmware_p);
834
835 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700836}
837
838/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800840 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800842void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
844 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800845 if (!fw_is_builtin_firmware(fw))
846 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 kfree(fw);
848 }
849}
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851/* Async support */
852struct firmware_work {
853 struct work_struct work;
854 struct module *module;
855 const char *name;
856 struct device *device;
857 void *context;
858 void (*cont)(const struct firmware *fw, void *context);
Bob Liu072fc8f2011-01-26 18:33:32 +0800859 bool uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860};
861
Stephen Boyda36cf842012-03-28 23:31:00 +0200862static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Stephen Boyda36cf842012-03-28 23:31:00 +0200864 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 const struct firmware *fw;
Stephen Boyddddb5542012-03-28 23:30:43 +0200866 struct firmware_priv *fw_priv;
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200867 long timeout;
matthieu castet113fab12005-11-13 16:07:39 -0800868 int ret;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700869
Stephen Boyda36cf842012-03-28 23:31:00 +0200870 fw_work = container_of(work, struct firmware_work, work);
Stephen Boyddddb5542012-03-28 23:30:43 +0200871 fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
872 fw_work->uevent, true);
873 if (IS_ERR_OR_NULL(fw_priv)) {
874 ret = PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200875 goto out;
Stephen Boyddddb5542012-03-28 23:30:43 +0200876 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200877
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200878 timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
879 if (timeout) {
Stephen Boyddddb5542012-03-28 23:30:43 +0200880 ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200881 usermodehelper_read_unlock();
882 } else {
883 dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
884 fw_work->name);
885 ret = -EAGAIN;
886 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200887 if (ret)
888 _request_firmware_cleanup(&fw);
889
890 out:
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100891 fw_work->cont(fw, fw_work->context);
Ming Lei0cfc1e12012-08-04 12:01:23 +0800892 put_device(fw_work->device);
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 module_put(fw_work->module);
895 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896}
897
898/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000899 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800900 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +0100901 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800902 * is non-zero else the firmware copy must be done manually.
903 * @name: name of firmware file
904 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100905 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800906 * @context: will be passed over to @cont, and
907 * @fw may be %NULL if firmware request fails.
908 * @cont: function will be called asynchronously when the firmware
909 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 *
Ming Lei0cfc1e12012-08-04 12:01:23 +0800911 * Caller must hold the reference count of @device.
912 *
Ming Lei6f21a622012-08-04 12:01:24 +0800913 * Asynchronous variant of request_firmware() for user contexts:
914 * - sleep for as small periods as possible since it may
915 * increase kernel boot time of built-in device drivers
916 * requesting firmware in their ->probe() methods, if
917 * @gfp is GFP_KERNEL.
918 *
919 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 **/
921int
922request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +0800923 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100924 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 void (*cont)(const struct firmware *fw, void *context))
926{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700927 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700929 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (!fw_work)
931 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700932
933 fw_work->module = module;
934 fw_work->name = name;
935 fw_work->device = device;
936 fw_work->context = context;
937 fw_work->cont = cont;
938 fw_work->uevent = uevent;
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (!try_module_get(module)) {
941 kfree(fw_work);
942 return -EFAULT;
943 }
944
Ming Lei0cfc1e12012-08-04 12:01:23 +0800945 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +0200946 INIT_WORK(&fw_work->work, request_firmware_work_func);
947 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return 0;
949}
950
Ming Lei2887b392012-08-04 12:01:22 +0800951/**
952 * cache_firmware - cache one firmware image in kernel memory space
953 * @fw_name: the firmware image name
954 *
955 * Cache firmware in kernel memory so that drivers can use it when
956 * system isn't ready for them to request firmware image from userspace.
957 * Once it returns successfully, driver can use request_firmware or its
958 * nowait version to get the cached firmware without any interacting
959 * with userspace
960 *
961 * Return 0 if the firmware image has been cached successfully
962 * Return !0 otherwise
963 *
964 */
965int cache_firmware(const char *fw_name)
966{
967 int ret;
968 const struct firmware *fw;
969
970 pr_debug("%s: %s\n", __func__, fw_name);
971
972 ret = request_firmware(&fw, fw_name, NULL);
973 if (!ret)
974 kfree(fw);
975
976 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
977
978 return ret;
979}
980
981/**
982 * uncache_firmware - remove one cached firmware image
983 * @fw_name: the firmware image name
984 *
985 * Uncache one firmware image which has been cached successfully
986 * before.
987 *
988 * Return 0 if the firmware cache has been removed successfully
989 * Return !0 otherwise
990 *
991 */
992int uncache_firmware(const char *fw_name)
993{
994 struct firmware_buf *buf;
995 struct firmware fw;
996
997 pr_debug("%s: %s\n", __func__, fw_name);
998
999 if (fw_get_builtin_firmware(&fw, fw_name))
1000 return 0;
1001
1002 buf = fw_lookup_buf(fw_name);
1003 if (buf) {
1004 fw_free_buf(buf);
1005 return 0;
1006 }
1007
1008 return -EINVAL;
1009}
1010
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001011static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Ming Lei1f2b7952012-08-04 12:01:21 +08001013 fw_cache_init();
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001014 return class_register(&firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001016
1017static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
1019 class_unregister(&firmware_class);
1020}
1021
Shaohua Lia30a6a22006-09-27 01:50:52 -07001022fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023module_exit(firmware_class_exit);
1024
1025EXPORT_SYMBOL(release_firmware);
1026EXPORT_SYMBOL(request_firmware);
1027EXPORT_SYMBOL(request_firmware_nowait);
Ming Lei2887b392012-08-04 12:01:22 +08001028EXPORT_SYMBOL_GPL(cache_firmware);
1029EXPORT_SYMBOL_GPL(uncache_firmware);