blob: 7d3a83bb13186c479ffb8a7e9e506f1a6db3af6d [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 Lei1f2b7952012-08-04 12:01:21 +0800117#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
118
119/* fw_lock could be moved to 'struct firmware_priv' but since it is just
120 * guarding for corner cases a global lock should be OK */
121static DEFINE_MUTEX(fw_lock);
122
123static struct firmware_cache fw_cache;
124
125static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
126 struct firmware_cache *fwc)
127{
128 struct firmware_buf *buf;
129
130 buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
131
132 if (!buf)
133 return buf;
134
135 kref_init(&buf->ref);
136 strcpy(buf->fw_id, fw_name);
137 buf->fwc = fwc;
138 init_completion(&buf->completion);
139
140 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
141
142 return buf;
143}
144
Ming Lei2887b392012-08-04 12:01:22 +0800145static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
146{
147 struct firmware_buf *tmp;
148 struct firmware_cache *fwc = &fw_cache;
149
150 list_for_each_entry(tmp, &fwc->head, list)
151 if (!strcmp(tmp->fw_id, fw_name))
152 return tmp;
153 return NULL;
154}
155
Ming Lei1f2b7952012-08-04 12:01:21 +0800156static int fw_lookup_and_allocate_buf(const char *fw_name,
157 struct firmware_cache *fwc,
158 struct firmware_buf **buf)
159{
160 struct firmware_buf *tmp;
161
162 spin_lock(&fwc->lock);
Ming Lei2887b392012-08-04 12:01:22 +0800163 tmp = __fw_lookup_buf(fw_name);
164 if (tmp) {
165 kref_get(&tmp->ref);
166 spin_unlock(&fwc->lock);
167 *buf = tmp;
168 return 1;
169 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800170 tmp = __allocate_fw_buf(fw_name, fwc);
171 if (tmp)
172 list_add(&tmp->list, &fwc->head);
173 spin_unlock(&fwc->lock);
174
175 *buf = tmp;
176
177 return tmp ? 0 : -ENOMEM;
178}
179
Ming Lei2887b392012-08-04 12:01:22 +0800180static struct firmware_buf *fw_lookup_buf(const char *fw_name)
181{
182 struct firmware_buf *tmp;
183 struct firmware_cache *fwc = &fw_cache;
184
185 spin_lock(&fwc->lock);
186 tmp = __fw_lookup_buf(fw_name);
187 spin_unlock(&fwc->lock);
188
189 return tmp;
190}
191
Ming Lei1f2b7952012-08-04 12:01:21 +0800192static void __fw_free_buf(struct kref *ref)
193{
194 struct firmware_buf *buf = to_fwbuf(ref);
195 struct firmware_cache *fwc = buf->fwc;
196 int i;
197
198 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
199 __func__, buf->fw_id, buf, buf->data,
200 (unsigned int)buf->size);
201
202 spin_lock(&fwc->lock);
203 list_del(&buf->list);
204 spin_unlock(&fwc->lock);
205
206 vunmap(buf->data);
207 for (i = 0; i < buf->nr_pages; i++)
208 __free_page(buf->pages[i]);
209 kfree(buf->pages);
210 kfree(buf);
211}
212
213static void fw_free_buf(struct firmware_buf *buf)
214{
215 kref_put(&buf->ref, __fw_free_buf);
216}
217
218static void __init fw_cache_init(void)
219{
220 spin_lock_init(&fw_cache.lock);
221 INIT_LIST_HEAD(&fw_cache.head);
222}
223
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700224static struct firmware_priv *to_firmware_priv(struct device *dev)
225{
226 return container_of(dev, struct firmware_priv, dev);
227}
228
229static void fw_load_abort(struct firmware_priv *fw_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Ming Lei12446912012-08-04 12:01:20 +0800231 struct firmware_buf *buf = fw_priv->buf;
232
233 set_bit(FW_STATUS_ABORT, &buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800234 complete_all(&buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700237static ssize_t firmware_timeout_show(struct class *class,
238 struct class_attribute *attr,
239 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 return sprintf(buf, "%d\n", loading_timeout);
242}
243
244/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800245 * firmware_timeout_store - set number of seconds to wait for firmware
246 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800247 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800248 * @buf: buffer to scan for timeout value
249 * @count: number of bytes in @buf
250 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800252 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 * firmware will be provided.
254 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800255 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700257static ssize_t firmware_timeout_store(struct class *class,
258 struct class_attribute *attr,
259 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700262 if (loading_timeout < 0)
263 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return count;
266}
267
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800268static struct class_attribute firmware_class_attrs[] = {
269 __ATTR(timeout, S_IWUSR | S_IRUGO,
270 firmware_timeout_show, firmware_timeout_store),
271 __ATTR_NULL
272};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800274static void fw_dev_release(struct device *dev)
275{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700276 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800277
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800278 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800279
280 module_put(THIS_MODULE);
281}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Kay Sievers7eff2e72007-08-14 15:15:12 +0200283static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700285 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Ming Lei12446912012-08-04 12:01:20 +0800287 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200289 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700290 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200291 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
292 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 return 0;
295}
296
Adrian Bunk1b81d662006-05-20 15:00:16 -0700297static struct class firmware_class = {
298 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800299 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700300 .dev_uevent = firmware_uevent,
301 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700302};
303
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700304static ssize_t firmware_loading_show(struct device *dev,
305 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700307 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800308 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return sprintf(buf, "%d\n", loading);
311}
312
Ming Lei65710cb2012-08-04 12:01:16 +0800313/* firmware holds the ownership of pages */
David Woodhousedd336c52010-05-02 11:21:21 +0300314static void firmware_free_data(const struct firmware *fw)
315{
Ming Lei1f2b7952012-08-04 12:01:21 +0800316 WARN_ON(!fw->priv);
317 fw_free_buf(fw->priv);
David Woodhousedd336c52010-05-02 11:21:21 +0300318}
319
David Woodhouse6e03a202009-04-09 22:04:07 -0700320/* Some architectures don't have PAGE_KERNEL_RO */
321#ifndef PAGE_KERNEL_RO
322#define PAGE_KERNEL_RO PAGE_KERNEL
323#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800325 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700326 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800327 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800328 * @buf: buffer to scan for loading control value
329 * @count: number of bytes in @buf
330 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 * The relevant values are:
332 *
333 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800334 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 * -1: Conclude the load with an error and discard any written data.
336 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700337static ssize_t firmware_loading_store(struct device *dev,
338 struct device_attribute *attr,
339 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700341 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800342 struct firmware_buf *fw_buf = fw_priv->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700344 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Neil Hormaneea915b2012-01-02 15:31:23 -0500346 mutex_lock(&fw_lock);
347
Ming Lei12446912012-08-04 12:01:20 +0800348 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500349 goto out;
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 switch (loading) {
352 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800353 /* discarding any previous partial load */
Ming Lei12446912012-08-04 12:01:20 +0800354 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
355 for (i = 0; i < fw_buf->nr_pages; i++)
356 __free_page(fw_buf->pages[i]);
357 kfree(fw_buf->pages);
358 fw_buf->pages = NULL;
359 fw_buf->page_array_size = 0;
360 fw_buf->nr_pages = 0;
361 set_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei28eefa72012-08-04 12:01:17 +0800362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 break;
364 case 0:
Ming Lei12446912012-08-04 12:01:20 +0800365 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
366 set_bit(FW_STATUS_DONE, &fw_buf->status);
367 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800368 complete_all(&fw_buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 break;
370 }
371 /* fallthrough */
372 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700373 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 /* fallthrough */
375 case -1:
376 fw_load_abort(fw_priv);
377 break;
378 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500379out:
380 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return count;
382}
383
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700384static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700386static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
387 struct bin_attribute *bin_attr,
388 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200390 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700391 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800392 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700393 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Laura Garciacad1e552006-05-23 23:22:38 +0200395 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800396 buf = fw_priv->buf;
397 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 ret_count = -ENODEV;
399 goto out;
400 }
Ming Lei12446912012-08-04 12:01:20 +0800401 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200402 ret_count = 0;
403 goto out;
404 }
Ming Lei12446912012-08-04 12:01:20 +0800405 if (count > buf->size - offset)
406 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700407
408 ret_count = count;
409
410 while (count) {
411 void *page_data;
412 int page_nr = offset >> PAGE_SHIFT;
413 int page_ofs = offset & (PAGE_SIZE-1);
414 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
415
Ming Lei12446912012-08-04 12:01:20 +0800416 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700417
418 memcpy(buffer, page_data + page_ofs, page_cnt);
419
Ming Lei12446912012-08-04 12:01:20 +0800420 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700421 buffer += page_cnt;
422 offset += page_cnt;
423 count -= page_cnt;
424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425out:
Laura Garciacad1e552006-05-23 23:22:38 +0200426 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return ret_count;
428}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800429
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700430static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Ming Lei12446912012-08-04 12:01:20 +0800432 struct firmware_buf *buf = fw_priv->buf;
David Woodhouse6e03a202009-04-09 22:04:07 -0700433 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
David Woodhouse6e03a202009-04-09 22:04:07 -0700435 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800436 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700437 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800438 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700439 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
David Woodhouse6e03a202009-04-09 22:04:07 -0700441 new_pages = kmalloc(new_array_size * sizeof(void *),
442 GFP_KERNEL);
443 if (!new_pages) {
444 fw_load_abort(fw_priv);
445 return -ENOMEM;
446 }
Ming Lei12446912012-08-04 12:01:20 +0800447 memcpy(new_pages, buf->pages,
448 buf->page_array_size * sizeof(void *));
449 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
450 (new_array_size - buf->page_array_size));
451 kfree(buf->pages);
452 buf->pages = new_pages;
453 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700455
Ming Lei12446912012-08-04 12:01:20 +0800456 while (buf->nr_pages < pages_needed) {
457 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700458 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
459
Ming Lei12446912012-08-04 12:01:20 +0800460 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700461 fw_load_abort(fw_priv);
462 return -ENOMEM;
463 }
Ming Lei12446912012-08-04 12:01:20 +0800464 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return 0;
467}
468
469/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800470 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700471 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700472 * @kobj: kobject for the device
Randy Dunlap42e61f4a2007-07-23 21:42:11 -0700473 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800474 * @buffer: buffer being written
475 * @offset: buffer offset for write in total data store area
476 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800478 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 * the driver as a firmware image.
480 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700481static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
482 struct bin_attribute *bin_attr,
483 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200485 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700486 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800487 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 ssize_t retval;
489
490 if (!capable(CAP_SYS_RAWIO))
491 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700492
Laura Garciacad1e552006-05-23 23:22:38 +0200493 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800494 buf = fw_priv->buf;
495 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 retval = -ENODEV;
497 goto out;
498 }
Ming Lei65710cb2012-08-04 12:01:16 +0800499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 retval = fw_realloc_buffer(fw_priv, offset + count);
501 if (retval)
502 goto out;
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700505
506 while (count) {
507 void *page_data;
508 int page_nr = offset >> PAGE_SHIFT;
509 int page_ofs = offset & (PAGE_SIZE - 1);
510 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
511
Ming Lei12446912012-08-04 12:01:20 +0800512 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700513
514 memcpy(page_data + page_ofs, buffer, page_cnt);
515
Ming Lei12446912012-08-04 12:01:20 +0800516 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700517 buffer += page_cnt;
518 offset += page_cnt;
519 count -= page_cnt;
520 }
521
Ming Lei12446912012-08-04 12:01:20 +0800522 buf->size = max_t(size_t, offset, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523out:
Laura Garciacad1e552006-05-23 23:22:38 +0200524 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return retval;
526}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800527
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700528static struct bin_attribute firmware_attr_data = {
529 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 .size = 0,
531 .read = firmware_data_read,
532 .write = firmware_data_write,
533};
534
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700535static void firmware_class_timeout(u_long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
537 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 fw_load_abort(fw_priv);
540}
541
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700542static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200543fw_create_instance(struct firmware *firmware, const char *fw_name,
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700544 struct device *device, bool uevent, bool nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700546 struct firmware_priv *fw_priv;
547 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Ming Lei12446912012-08-04 12:01:20 +0800549 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700550 if (!fw_priv) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700551 dev_err(device, "%s: kmalloc failed\n", __func__);
Ming Lei12446912012-08-04 12:01:20 +0800552 fw_priv = ERR_PTR(-ENOMEM);
553 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700556 fw_priv->nowait = nowait;
Ming Lei1f2b7952012-08-04 12:01:21 +0800557 fw_priv->fw = firmware;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700558 setup_timer(&fw_priv->timeout,
559 firmware_class_timeout, (u_long) fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700561 f_dev = &fw_priv->dev;
562
563 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800564 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700565 f_dev->parent = device;
566 f_dev->class = &firmware_class;
Ming Lei12446912012-08-04 12:01:20 +0800567exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700568 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
Ming Lei1f2b7952012-08-04 12:01:21 +0800571/* one pages buffer is mapped/unmapped only once */
572static int fw_map_pages_buf(struct firmware_buf *buf)
573{
574 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
575 if (!buf->data)
576 return -ENOMEM;
577 return 0;
578}
579
580/* store the pages buffer info firmware from buf */
581static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
582{
583 fw->priv = buf;
584 fw->pages = buf->pages;
585 fw->size = buf->size;
586 fw->data = buf->data;
587
588 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
589 __func__, buf->fw_id, buf, buf->data,
590 (unsigned int)buf->size);
591}
592
593static void _request_firmware_cleanup(const struct firmware **firmware_p)
594{
595 release_firmware(*firmware_p);
596 *firmware_p = NULL;
597}
598
Stephen Boyddddb5542012-03-28 23:30:43 +0200599static struct firmware_priv *
600_request_firmware_prepare(const struct firmware **firmware_p, const char *name,
601 struct device *device, bool uevent, bool nowait)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700602{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +0800604 struct firmware_priv *fw_priv = NULL;
605 struct firmware_buf *buf;
606 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 if (!firmware_p)
Stephen Boyddddb5542012-03-28 23:30:43 +0200609 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Jiri Slaby4aed0642005-09-13 01:25:01 -0700611 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700613 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
614 __func__);
Stephen Boyddddb5542012-03-28 23:30:43 +0200615 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800618 if (fw_get_builtin_firmware(firmware, name)) {
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100619 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
Stephen Boyddddb5542012-03-28 23:30:43 +0200620 return NULL;
David Woodhouse5658c762008-05-23 13:52:42 +0100621 }
622
Ming Lei1f2b7952012-08-04 12:01:21 +0800623 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
624 if (!ret)
625 fw_priv = fw_create_instance(firmware, name, device,
626 uevent, nowait);
627
628 if (IS_ERR(fw_priv) || ret < 0) {
629 kfree(firmware);
Stephen Boyddddb5542012-03-28 23:30:43 +0200630 *firmware_p = NULL;
Ming Lei1f2b7952012-08-04 12:01:21 +0800631 return ERR_PTR(-ENOMEM);
632 } else if (fw_priv) {
633 fw_priv->buf = buf;
634
635 /*
636 * bind with 'buf' now to avoid warning in failure path
637 * of requesting firmware.
638 */
639 firmware->priv = buf;
640 return fw_priv;
Stephen Boyddddb5542012-03-28 23:30:43 +0200641 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800642
643 /* share the cached buf, which is inprogessing or completed */
644 check_status:
645 mutex_lock(&fw_lock);
646 if (test_bit(FW_STATUS_ABORT, &buf->status)) {
647 fw_priv = ERR_PTR(-ENOENT);
648 _request_firmware_cleanup(firmware_p);
649 goto exit;
650 } else if (test_bit(FW_STATUS_DONE, &buf->status)) {
651 fw_priv = NULL;
652 fw_set_page_data(buf, firmware);
653 goto exit;
654 }
655 mutex_unlock(&fw_lock);
656 wait_for_completion(&buf->completion);
657 goto check_status;
658
659exit:
660 mutex_unlock(&fw_lock);
Stephen Boyddddb5542012-03-28 23:30:43 +0200661 return fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200662}
663
Stephen Boyddddb5542012-03-28 23:30:43 +0200664static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
665 long timeout)
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200666{
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200667 int retval = 0;
Stephen Boyddddb5542012-03-28 23:30:43 +0200668 struct device *f_dev = &fw_priv->dev;
Ming Lei12446912012-08-04 12:01:20 +0800669 struct firmware_buf *buf = fw_priv->buf;
Linus Torvaldscaca9512011-08-24 15:55:30 -0700670
Stephen Boyddddb5542012-03-28 23:30:43 +0200671 dev_set_uevent_suppress(f_dev, true);
David Woodhouse5658c762008-05-23 13:52:42 +0100672
Stephen Boyddddb5542012-03-28 23:30:43 +0200673 /* Need to pin this module until class device is destroyed */
674 __module_get(THIS_MODULE);
675
676 retval = device_add(f_dev);
677 if (retval) {
678 dev_err(f_dev, "%s: device_register failed\n", __func__);
679 goto err_put_dev;
680 }
681
682 retval = device_create_bin_file(f_dev, &firmware_attr_data);
683 if (retval) {
684 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
685 goto err_del_dev;
686 }
687
688 retval = device_create_file(f_dev, &dev_attr_loading);
689 if (retval) {
690 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
691 goto err_del_bin_attr;
692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Kay Sievers312c0042005-11-16 09:00:00 +0100694 if (uevent) {
Stephen Boyddddb5542012-03-28 23:30:43 +0200695 dev_set_uevent_suppress(f_dev, false);
Ming Lei12446912012-08-04 12:01:20 +0800696 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200697 if (timeout != MAX_SCHEDULE_TIMEOUT)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700698 mod_timer(&fw_priv->timeout,
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200699 round_jiffies_up(jiffies + timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700701 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
702 }
703
Ming Lei12446912012-08-04 12:01:20 +0800704 wait_for_completion(&buf->completion);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700705
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700706 del_timer_sync(&fw_priv->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Laura Garciacad1e552006-05-23 23:22:38 +0200708 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800709 if (!buf->size || test_bit(FW_STATUS_ABORT, &buf->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 retval = -ENOENT;
Ming Lei65710cb2012-08-04 12:01:16 +0800711
Ming Lei65710cb2012-08-04 12:01:16 +0800712 if (!retval)
Ming Lei1f2b7952012-08-04 12:01:21 +0800713 retval = fw_map_pages_buf(buf);
Ming Lei12446912012-08-04 12:01:20 +0800714
Ming Lei1f2b7952012-08-04 12:01:21 +0800715 /* pass the pages buffer to driver at the last minute */
716 fw_set_page_data(buf, fw_priv->fw);
717
Ming Lei12446912012-08-04 12:01:20 +0800718 fw_priv->buf = NULL;
Laura Garciacad1e552006-05-23 23:22:38 +0200719 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Stephen Boyddddb5542012-03-28 23:30:43 +0200721 device_remove_file(f_dev, &dev_attr_loading);
722err_del_bin_attr:
723 device_remove_bin_file(f_dev, &firmware_attr_data);
724err_del_dev:
725 device_del(f_dev);
726err_put_dev:
727 put_device(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return retval;
729}
730
731/**
Kay Sievers312c0042005-11-16 09:00:00 +0100732 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800733 * @firmware_p: pointer to firmware image
734 * @name: name of firmware file
735 * @device: device for which firmware is being loaded
736 *
737 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700738 * of @name for device @device.
739 *
740 * Should be called from user context where sleeping is allowed.
741 *
Kay Sievers312c0042005-11-16 09:00:00 +0100742 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700743 * should be distinctive enough not to be confused with any other
744 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +0800745 *
746 * Caller must hold the reference count of @device.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700747 **/
748int
749request_firmware(const struct firmware **firmware_p, const char *name,
750 struct device *device)
751{
Stephen Boyddddb5542012-03-28 23:30:43 +0200752 struct firmware_priv *fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200753 int ret;
754
Stephen Boyddddb5542012-03-28 23:30:43 +0200755 fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
756 false);
757 if (IS_ERR_OR_NULL(fw_priv))
758 return PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200759
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200760 ret = usermodehelper_read_trylock();
761 if (WARN_ON(ret)) {
762 dev_err(device, "firmware: %s will not be loaded\n", name);
763 } else {
Stephen Boyddddb5542012-03-28 23:30:43 +0200764 ret = _request_firmware_load(fw_priv, true,
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200765 firmware_loading_timeout());
766 usermodehelper_read_unlock();
767 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200768 if (ret)
769 _request_firmware_cleanup(firmware_p);
770
771 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700772}
773
774/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800776 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800778void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800781 if (!fw_is_builtin_firmware(fw))
782 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 kfree(fw);
784 }
785}
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787/* Async support */
788struct firmware_work {
789 struct work_struct work;
790 struct module *module;
791 const char *name;
792 struct device *device;
793 void *context;
794 void (*cont)(const struct firmware *fw, void *context);
Bob Liu072fc8f2011-01-26 18:33:32 +0800795 bool uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796};
797
Stephen Boyda36cf842012-03-28 23:31:00 +0200798static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Stephen Boyda36cf842012-03-28 23:31:00 +0200800 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 const struct firmware *fw;
Stephen Boyddddb5542012-03-28 23:30:43 +0200802 struct firmware_priv *fw_priv;
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200803 long timeout;
matthieu castet113fab12005-11-13 16:07:39 -0800804 int ret;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700805
Stephen Boyda36cf842012-03-28 23:31:00 +0200806 fw_work = container_of(work, struct firmware_work, work);
Stephen Boyddddb5542012-03-28 23:30:43 +0200807 fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
808 fw_work->uevent, true);
809 if (IS_ERR_OR_NULL(fw_priv)) {
810 ret = PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200811 goto out;
Stephen Boyddddb5542012-03-28 23:30:43 +0200812 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200813
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200814 timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
815 if (timeout) {
Stephen Boyddddb5542012-03-28 23:30:43 +0200816 ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200817 usermodehelper_read_unlock();
818 } else {
819 dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
820 fw_work->name);
821 ret = -EAGAIN;
822 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200823 if (ret)
824 _request_firmware_cleanup(&fw);
825
826 out:
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100827 fw_work->cont(fw, fw_work->context);
Ming Lei0cfc1e12012-08-04 12:01:23 +0800828 put_device(fw_work->device);
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 module_put(fw_work->module);
831 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
834/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000835 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800836 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +0100837 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800838 * is non-zero else the firmware copy must be done manually.
839 * @name: name of firmware file
840 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100841 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800842 * @context: will be passed over to @cont, and
843 * @fw may be %NULL if firmware request fails.
844 * @cont: function will be called asynchronously when the firmware
845 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 *
Ming Lei0cfc1e12012-08-04 12:01:23 +0800847 * Caller must hold the reference count of @device.
848 *
Ming Lei7fcab092009-05-29 11:33:19 +0800849 * Asynchronous variant of request_firmware() for user contexts where
850 * it is not possible to sleep for long time. It can't be called
851 * in atomic contexts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 **/
853int
854request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +0800855 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100856 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 void (*cont)(const struct firmware *fw, void *context))
858{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700859 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700861 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (!fw_work)
863 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700864
865 fw_work->module = module;
866 fw_work->name = name;
867 fw_work->device = device;
868 fw_work->context = context;
869 fw_work->cont = cont;
870 fw_work->uevent = uevent;
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (!try_module_get(module)) {
873 kfree(fw_work);
874 return -EFAULT;
875 }
876
Ming Lei0cfc1e12012-08-04 12:01:23 +0800877 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +0200878 INIT_WORK(&fw_work->work, request_firmware_work_func);
879 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return 0;
881}
882
Ming Lei2887b392012-08-04 12:01:22 +0800883/**
884 * cache_firmware - cache one firmware image in kernel memory space
885 * @fw_name: the firmware image name
886 *
887 * Cache firmware in kernel memory so that drivers can use it when
888 * system isn't ready for them to request firmware image from userspace.
889 * Once it returns successfully, driver can use request_firmware or its
890 * nowait version to get the cached firmware without any interacting
891 * with userspace
892 *
893 * Return 0 if the firmware image has been cached successfully
894 * Return !0 otherwise
895 *
896 */
897int cache_firmware(const char *fw_name)
898{
899 int ret;
900 const struct firmware *fw;
901
902 pr_debug("%s: %s\n", __func__, fw_name);
903
904 ret = request_firmware(&fw, fw_name, NULL);
905 if (!ret)
906 kfree(fw);
907
908 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
909
910 return ret;
911}
912
913/**
914 * uncache_firmware - remove one cached firmware image
915 * @fw_name: the firmware image name
916 *
917 * Uncache one firmware image which has been cached successfully
918 * before.
919 *
920 * Return 0 if the firmware cache has been removed successfully
921 * Return !0 otherwise
922 *
923 */
924int uncache_firmware(const char *fw_name)
925{
926 struct firmware_buf *buf;
927 struct firmware fw;
928
929 pr_debug("%s: %s\n", __func__, fw_name);
930
931 if (fw_get_builtin_firmware(&fw, fw_name))
932 return 0;
933
934 buf = fw_lookup_buf(fw_name);
935 if (buf) {
936 fw_free_buf(buf);
937 return 0;
938 }
939
940 return -EINVAL;
941}
942
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800943static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Ming Lei1f2b7952012-08-04 12:01:21 +0800945 fw_cache_init();
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800946 return class_register(&firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800948
949static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
951 class_unregister(&firmware_class);
952}
953
Shaohua Lia30a6a22006-09-27 01:50:52 -0700954fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955module_exit(firmware_class_exit);
956
957EXPORT_SYMBOL(release_firmware);
958EXPORT_SYMBOL(request_firmware);
959EXPORT_SYMBOL(request_firmware_nowait);
Ming Lei2887b392012-08-04 12:01:22 +0800960EXPORT_SYMBOL_GPL(cache_firmware);
961EXPORT_SYMBOL_GPL(uncache_firmware);