blob: 848ad97e8d79be5f87d47f2fec023e9cf01ef1df [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
145static int fw_lookup_and_allocate_buf(const char *fw_name,
146 struct firmware_cache *fwc,
147 struct firmware_buf **buf)
148{
149 struct firmware_buf *tmp;
150
151 spin_lock(&fwc->lock);
152 list_for_each_entry(tmp, &fwc->head, list)
153 if (!strcmp(tmp->fw_id, fw_name)) {
154 kref_get(&tmp->ref);
155 spin_unlock(&fwc->lock);
156 *buf = tmp;
157 return 1;
158 }
159
160 tmp = __allocate_fw_buf(fw_name, fwc);
161 if (tmp)
162 list_add(&tmp->list, &fwc->head);
163 spin_unlock(&fwc->lock);
164
165 *buf = tmp;
166
167 return tmp ? 0 : -ENOMEM;
168}
169
170static void __fw_free_buf(struct kref *ref)
171{
172 struct firmware_buf *buf = to_fwbuf(ref);
173 struct firmware_cache *fwc = buf->fwc;
174 int i;
175
176 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
177 __func__, buf->fw_id, buf, buf->data,
178 (unsigned int)buf->size);
179
180 spin_lock(&fwc->lock);
181 list_del(&buf->list);
182 spin_unlock(&fwc->lock);
183
184 vunmap(buf->data);
185 for (i = 0; i < buf->nr_pages; i++)
186 __free_page(buf->pages[i]);
187 kfree(buf->pages);
188 kfree(buf);
189}
190
191static void fw_free_buf(struct firmware_buf *buf)
192{
193 kref_put(&buf->ref, __fw_free_buf);
194}
195
196static void __init fw_cache_init(void)
197{
198 spin_lock_init(&fw_cache.lock);
199 INIT_LIST_HEAD(&fw_cache.head);
200}
201
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700202static struct firmware_priv *to_firmware_priv(struct device *dev)
203{
204 return container_of(dev, struct firmware_priv, dev);
205}
206
207static void fw_load_abort(struct firmware_priv *fw_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Ming Lei12446912012-08-04 12:01:20 +0800209 struct firmware_buf *buf = fw_priv->buf;
210
211 set_bit(FW_STATUS_ABORT, &buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800212 complete_all(&buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700215static ssize_t firmware_timeout_show(struct class *class,
216 struct class_attribute *attr,
217 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 return sprintf(buf, "%d\n", loading_timeout);
220}
221
222/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800223 * firmware_timeout_store - set number of seconds to wait for firmware
224 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800225 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800226 * @buf: buffer to scan for timeout value
227 * @count: number of bytes in @buf
228 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800230 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 * firmware will be provided.
232 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800233 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700235static ssize_t firmware_timeout_store(struct class *class,
236 struct class_attribute *attr,
237 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700240 if (loading_timeout < 0)
241 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return count;
244}
245
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800246static struct class_attribute firmware_class_attrs[] = {
247 __ATTR(timeout, S_IWUSR | S_IRUGO,
248 firmware_timeout_show, firmware_timeout_store),
249 __ATTR_NULL
250};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800252static void fw_dev_release(struct device *dev)
253{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700254 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800255
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800256 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800257
258 module_put(THIS_MODULE);
259}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Kay Sievers7eff2e72007-08-14 15:15:12 +0200261static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700263 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Ming Lei12446912012-08-04 12:01:20 +0800265 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200267 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700268 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200269 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
270 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 return 0;
273}
274
Adrian Bunk1b81d662006-05-20 15:00:16 -0700275static struct class firmware_class = {
276 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800277 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700278 .dev_uevent = firmware_uevent,
279 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700280};
281
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700282static ssize_t firmware_loading_show(struct device *dev,
283 struct device_attribute *attr, char *buf)
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);
Ming Lei12446912012-08-04 12:01:20 +0800286 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return sprintf(buf, "%d\n", loading);
289}
290
Ming Lei65710cb2012-08-04 12:01:16 +0800291/* firmware holds the ownership of pages */
David Woodhousedd336c52010-05-02 11:21:21 +0300292static void firmware_free_data(const struct firmware *fw)
293{
Ming Lei1f2b7952012-08-04 12:01:21 +0800294 WARN_ON(!fw->priv);
295 fw_free_buf(fw->priv);
David Woodhousedd336c52010-05-02 11:21:21 +0300296}
297
David Woodhouse6e03a202009-04-09 22:04:07 -0700298/* Some architectures don't have PAGE_KERNEL_RO */
299#ifndef PAGE_KERNEL_RO
300#define PAGE_KERNEL_RO PAGE_KERNEL
301#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800303 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700304 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800305 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800306 * @buf: buffer to scan for loading control value
307 * @count: number of bytes in @buf
308 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 * The relevant values are:
310 *
311 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800312 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 * -1: Conclude the load with an error and discard any written data.
314 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700315static ssize_t firmware_loading_store(struct device *dev,
316 struct device_attribute *attr,
317 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700319 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800320 struct firmware_buf *fw_buf = fw_priv->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700322 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Neil Hormaneea915b2012-01-02 15:31:23 -0500324 mutex_lock(&fw_lock);
325
Ming Lei12446912012-08-04 12:01:20 +0800326 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500327 goto out;
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 switch (loading) {
330 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800331 /* discarding any previous partial load */
Ming Lei12446912012-08-04 12:01:20 +0800332 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
333 for (i = 0; i < fw_buf->nr_pages; i++)
334 __free_page(fw_buf->pages[i]);
335 kfree(fw_buf->pages);
336 fw_buf->pages = NULL;
337 fw_buf->page_array_size = 0;
338 fw_buf->nr_pages = 0;
339 set_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei28eefa72012-08-04 12:01:17 +0800340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 break;
342 case 0:
Ming Lei12446912012-08-04 12:01:20 +0800343 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
344 set_bit(FW_STATUS_DONE, &fw_buf->status);
345 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800346 complete_all(&fw_buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 break;
348 }
349 /* fallthrough */
350 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700351 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 /* fallthrough */
353 case -1:
354 fw_load_abort(fw_priv);
355 break;
356 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500357out:
358 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return count;
360}
361
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700362static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700364static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
365 struct bin_attribute *bin_attr,
366 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200368 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700369 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800370 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700371 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Laura Garciacad1e552006-05-23 23:22:38 +0200373 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800374 buf = fw_priv->buf;
375 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 ret_count = -ENODEV;
377 goto out;
378 }
Ming Lei12446912012-08-04 12:01:20 +0800379 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200380 ret_count = 0;
381 goto out;
382 }
Ming Lei12446912012-08-04 12:01:20 +0800383 if (count > buf->size - offset)
384 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700385
386 ret_count = count;
387
388 while (count) {
389 void *page_data;
390 int page_nr = offset >> PAGE_SHIFT;
391 int page_ofs = offset & (PAGE_SIZE-1);
392 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
393
Ming Lei12446912012-08-04 12:01:20 +0800394 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700395
396 memcpy(buffer, page_data + page_ofs, page_cnt);
397
Ming Lei12446912012-08-04 12:01:20 +0800398 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700399 buffer += page_cnt;
400 offset += page_cnt;
401 count -= page_cnt;
402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403out:
Laura Garciacad1e552006-05-23 23:22:38 +0200404 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return ret_count;
406}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800407
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700408static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Ming Lei12446912012-08-04 12:01:20 +0800410 struct firmware_buf *buf = fw_priv->buf;
David Woodhouse6e03a202009-04-09 22:04:07 -0700411 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
David Woodhouse6e03a202009-04-09 22:04:07 -0700413 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800414 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700415 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800416 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700417 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
David Woodhouse6e03a202009-04-09 22:04:07 -0700419 new_pages = kmalloc(new_array_size * sizeof(void *),
420 GFP_KERNEL);
421 if (!new_pages) {
422 fw_load_abort(fw_priv);
423 return -ENOMEM;
424 }
Ming Lei12446912012-08-04 12:01:20 +0800425 memcpy(new_pages, buf->pages,
426 buf->page_array_size * sizeof(void *));
427 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
428 (new_array_size - buf->page_array_size));
429 kfree(buf->pages);
430 buf->pages = new_pages;
431 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700433
Ming Lei12446912012-08-04 12:01:20 +0800434 while (buf->nr_pages < pages_needed) {
435 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700436 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
437
Ming Lei12446912012-08-04 12:01:20 +0800438 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700439 fw_load_abort(fw_priv);
440 return -ENOMEM;
441 }
Ming Lei12446912012-08-04 12:01:20 +0800442 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return 0;
445}
446
447/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800448 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700449 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700450 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700451 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800452 * @buffer: buffer being written
453 * @offset: buffer offset for write in total data store area
454 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800456 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 * the driver as a firmware image.
458 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700459static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
460 struct bin_attribute *bin_attr,
461 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200463 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700464 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800465 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 ssize_t retval;
467
468 if (!capable(CAP_SYS_RAWIO))
469 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700470
Laura Garciacad1e552006-05-23 23:22:38 +0200471 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800472 buf = fw_priv->buf;
473 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 retval = -ENODEV;
475 goto out;
476 }
Ming Lei65710cb2012-08-04 12:01:16 +0800477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 retval = fw_realloc_buffer(fw_priv, offset + count);
479 if (retval)
480 goto out;
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700483
484 while (count) {
485 void *page_data;
486 int page_nr = offset >> PAGE_SHIFT;
487 int page_ofs = offset & (PAGE_SIZE - 1);
488 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
489
Ming Lei12446912012-08-04 12:01:20 +0800490 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700491
492 memcpy(page_data + page_ofs, buffer, page_cnt);
493
Ming Lei12446912012-08-04 12:01:20 +0800494 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700495 buffer += page_cnt;
496 offset += page_cnt;
497 count -= page_cnt;
498 }
499
Ming Lei12446912012-08-04 12:01:20 +0800500 buf->size = max_t(size_t, offset, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501out:
Laura Garciacad1e552006-05-23 23:22:38 +0200502 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return retval;
504}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800505
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700506static struct bin_attribute firmware_attr_data = {
507 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 .size = 0,
509 .read = firmware_data_read,
510 .write = firmware_data_write,
511};
512
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700513static void firmware_class_timeout(u_long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 fw_load_abort(fw_priv);
518}
519
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700520static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200521fw_create_instance(struct firmware *firmware, const char *fw_name,
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700522 struct device *device, bool uevent, bool nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700524 struct firmware_priv *fw_priv;
525 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Ming Lei12446912012-08-04 12:01:20 +0800527 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700528 if (!fw_priv) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700529 dev_err(device, "%s: kmalloc failed\n", __func__);
Ming Lei12446912012-08-04 12:01:20 +0800530 fw_priv = ERR_PTR(-ENOMEM);
531 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700534 fw_priv->nowait = nowait;
Ming Lei1f2b7952012-08-04 12:01:21 +0800535 fw_priv->fw = firmware;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700536 setup_timer(&fw_priv->timeout,
537 firmware_class_timeout, (u_long) fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700539 f_dev = &fw_priv->dev;
540
541 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800542 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700543 f_dev->parent = device;
544 f_dev->class = &firmware_class;
Ming Lei12446912012-08-04 12:01:20 +0800545exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700546 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Ming Lei1f2b7952012-08-04 12:01:21 +0800549/* one pages buffer is mapped/unmapped only once */
550static int fw_map_pages_buf(struct firmware_buf *buf)
551{
552 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
553 if (!buf->data)
554 return -ENOMEM;
555 return 0;
556}
557
558/* store the pages buffer info firmware from buf */
559static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
560{
561 fw->priv = buf;
562 fw->pages = buf->pages;
563 fw->size = buf->size;
564 fw->data = buf->data;
565
566 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
567 __func__, buf->fw_id, buf, buf->data,
568 (unsigned int)buf->size);
569}
570
571static void _request_firmware_cleanup(const struct firmware **firmware_p)
572{
573 release_firmware(*firmware_p);
574 *firmware_p = NULL;
575}
576
Stephen Boyddddb5542012-03-28 23:30:43 +0200577static struct firmware_priv *
578_request_firmware_prepare(const struct firmware **firmware_p, const char *name,
579 struct device *device, bool uevent, bool nowait)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700580{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +0800582 struct firmware_priv *fw_priv = NULL;
583 struct firmware_buf *buf;
584 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 if (!firmware_p)
Stephen Boyddddb5542012-03-28 23:30:43 +0200587 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Jiri Slaby4aed0642005-09-13 01:25:01 -0700589 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700591 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
592 __func__);
Stephen Boyddddb5542012-03-28 23:30:43 +0200593 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800596 if (fw_get_builtin_firmware(firmware, name)) {
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100597 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
Stephen Boyddddb5542012-03-28 23:30:43 +0200598 return NULL;
David Woodhouse5658c762008-05-23 13:52:42 +0100599 }
600
Ming Lei1f2b7952012-08-04 12:01:21 +0800601 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
602 if (!ret)
603 fw_priv = fw_create_instance(firmware, name, device,
604 uevent, nowait);
605
606 if (IS_ERR(fw_priv) || ret < 0) {
607 kfree(firmware);
Stephen Boyddddb5542012-03-28 23:30:43 +0200608 *firmware_p = NULL;
Ming Lei1f2b7952012-08-04 12:01:21 +0800609 return ERR_PTR(-ENOMEM);
610 } else if (fw_priv) {
611 fw_priv->buf = buf;
612
613 /*
614 * bind with 'buf' now to avoid warning in failure path
615 * of requesting firmware.
616 */
617 firmware->priv = buf;
618 return fw_priv;
Stephen Boyddddb5542012-03-28 23:30:43 +0200619 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800620
621 /* share the cached buf, which is inprogessing or completed */
622 check_status:
623 mutex_lock(&fw_lock);
624 if (test_bit(FW_STATUS_ABORT, &buf->status)) {
625 fw_priv = ERR_PTR(-ENOENT);
626 _request_firmware_cleanup(firmware_p);
627 goto exit;
628 } else if (test_bit(FW_STATUS_DONE, &buf->status)) {
629 fw_priv = NULL;
630 fw_set_page_data(buf, firmware);
631 goto exit;
632 }
633 mutex_unlock(&fw_lock);
634 wait_for_completion(&buf->completion);
635 goto check_status;
636
637exit:
638 mutex_unlock(&fw_lock);
Stephen Boyddddb5542012-03-28 23:30:43 +0200639 return fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200640}
641
Stephen Boyddddb5542012-03-28 23:30:43 +0200642static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
643 long timeout)
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200644{
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200645 int retval = 0;
Stephen Boyddddb5542012-03-28 23:30:43 +0200646 struct device *f_dev = &fw_priv->dev;
Ming Lei12446912012-08-04 12:01:20 +0800647 struct firmware_buf *buf = fw_priv->buf;
Linus Torvaldscaca9512011-08-24 15:55:30 -0700648
Stephen Boyddddb5542012-03-28 23:30:43 +0200649 dev_set_uevent_suppress(f_dev, true);
David Woodhouse5658c762008-05-23 13:52:42 +0100650
Stephen Boyddddb5542012-03-28 23:30:43 +0200651 /* Need to pin this module until class device is destroyed */
652 __module_get(THIS_MODULE);
653
654 retval = device_add(f_dev);
655 if (retval) {
656 dev_err(f_dev, "%s: device_register failed\n", __func__);
657 goto err_put_dev;
658 }
659
660 retval = device_create_bin_file(f_dev, &firmware_attr_data);
661 if (retval) {
662 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
663 goto err_del_dev;
664 }
665
666 retval = device_create_file(f_dev, &dev_attr_loading);
667 if (retval) {
668 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
669 goto err_del_bin_attr;
670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Kay Sievers312c0042005-11-16 09:00:00 +0100672 if (uevent) {
Stephen Boyddddb5542012-03-28 23:30:43 +0200673 dev_set_uevent_suppress(f_dev, false);
Ming Lei12446912012-08-04 12:01:20 +0800674 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200675 if (timeout != MAX_SCHEDULE_TIMEOUT)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700676 mod_timer(&fw_priv->timeout,
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200677 round_jiffies_up(jiffies + timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700679 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
680 }
681
Ming Lei12446912012-08-04 12:01:20 +0800682 wait_for_completion(&buf->completion);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700683
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700684 del_timer_sync(&fw_priv->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Laura Garciacad1e552006-05-23 23:22:38 +0200686 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800687 if (!buf->size || test_bit(FW_STATUS_ABORT, &buf->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 retval = -ENOENT;
Ming Lei65710cb2012-08-04 12:01:16 +0800689
Ming Lei65710cb2012-08-04 12:01:16 +0800690 if (!retval)
Ming Lei1f2b7952012-08-04 12:01:21 +0800691 retval = fw_map_pages_buf(buf);
Ming Lei12446912012-08-04 12:01:20 +0800692
Ming Lei1f2b7952012-08-04 12:01:21 +0800693 /* pass the pages buffer to driver at the last minute */
694 fw_set_page_data(buf, fw_priv->fw);
695
Ming Lei12446912012-08-04 12:01:20 +0800696 fw_priv->buf = NULL;
Laura Garciacad1e552006-05-23 23:22:38 +0200697 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Stephen Boyddddb5542012-03-28 23:30:43 +0200699 device_remove_file(f_dev, &dev_attr_loading);
700err_del_bin_attr:
701 device_remove_bin_file(f_dev, &firmware_attr_data);
702err_del_dev:
703 device_del(f_dev);
704err_put_dev:
705 put_device(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return retval;
707}
708
709/**
Kay Sievers312c0042005-11-16 09:00:00 +0100710 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800711 * @firmware_p: pointer to firmware image
712 * @name: name of firmware file
713 * @device: device for which firmware is being loaded
714 *
715 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700716 * of @name for device @device.
717 *
718 * Should be called from user context where sleeping is allowed.
719 *
Kay Sievers312c0042005-11-16 09:00:00 +0100720 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700721 * should be distinctive enough not to be confused with any other
722 * firmware image for this or any other device.
723 **/
724int
725request_firmware(const struct firmware **firmware_p, const char *name,
726 struct device *device)
727{
Stephen Boyddddb5542012-03-28 23:30:43 +0200728 struct firmware_priv *fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200729 int ret;
730
Stephen Boyddddb5542012-03-28 23:30:43 +0200731 fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
732 false);
733 if (IS_ERR_OR_NULL(fw_priv))
734 return PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200735
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200736 ret = usermodehelper_read_trylock();
737 if (WARN_ON(ret)) {
738 dev_err(device, "firmware: %s will not be loaded\n", name);
739 } else {
Stephen Boyddddb5542012-03-28 23:30:43 +0200740 ret = _request_firmware_load(fw_priv, true,
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200741 firmware_loading_timeout());
742 usermodehelper_read_unlock();
743 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200744 if (ret)
745 _request_firmware_cleanup(firmware_p);
746
747 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700748}
749
750/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800752 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800754void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
756 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800757 if (!fw_is_builtin_firmware(fw))
758 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 kfree(fw);
760 }
761}
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763/* Async support */
764struct firmware_work {
765 struct work_struct work;
766 struct module *module;
767 const char *name;
768 struct device *device;
769 void *context;
770 void (*cont)(const struct firmware *fw, void *context);
Bob Liu072fc8f2011-01-26 18:33:32 +0800771 bool uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772};
773
Stephen Boyda36cf842012-03-28 23:31:00 +0200774static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Stephen Boyda36cf842012-03-28 23:31:00 +0200776 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 const struct firmware *fw;
Stephen Boyddddb5542012-03-28 23:30:43 +0200778 struct firmware_priv *fw_priv;
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200779 long timeout;
matthieu castet113fab12005-11-13 16:07:39 -0800780 int ret;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700781
Stephen Boyda36cf842012-03-28 23:31:00 +0200782 fw_work = container_of(work, struct firmware_work, work);
Stephen Boyddddb5542012-03-28 23:30:43 +0200783 fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
784 fw_work->uevent, true);
785 if (IS_ERR_OR_NULL(fw_priv)) {
786 ret = PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200787 goto out;
Stephen Boyddddb5542012-03-28 23:30:43 +0200788 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200789
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200790 timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
791 if (timeout) {
Stephen Boyddddb5542012-03-28 23:30:43 +0200792 ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200793 usermodehelper_read_unlock();
794 } else {
795 dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
796 fw_work->name);
797 ret = -EAGAIN;
798 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200799 if (ret)
800 _request_firmware_cleanup(&fw);
801
802 out:
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100803 fw_work->cont(fw, fw_work->context);
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 module_put(fw_work->module);
806 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
809/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000810 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800811 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +0100812 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800813 * is non-zero else the firmware copy must be done manually.
814 * @name: name of firmware file
815 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100816 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800817 * @context: will be passed over to @cont, and
818 * @fw may be %NULL if firmware request fails.
819 * @cont: function will be called asynchronously when the firmware
820 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 *
Ming Lei7fcab092009-05-29 11:33:19 +0800822 * Asynchronous variant of request_firmware() for user contexts where
823 * it is not possible to sleep for long time. It can't be called
824 * in atomic contexts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 **/
826int
827request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +0800828 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100829 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 void (*cont)(const struct firmware *fw, void *context))
831{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700832 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700834 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (!fw_work)
836 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700837
838 fw_work->module = module;
839 fw_work->name = name;
840 fw_work->device = device;
841 fw_work->context = context;
842 fw_work->cont = cont;
843 fw_work->uevent = uevent;
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (!try_module_get(module)) {
846 kfree(fw_work);
847 return -EFAULT;
848 }
849
Stephen Boyda36cf842012-03-28 23:31:00 +0200850 INIT_WORK(&fw_work->work, request_firmware_work_func);
851 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return 0;
853}
854
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800855static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Ming Lei1f2b7952012-08-04 12:01:21 +0800857 fw_cache_init();
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800858 return class_register(&firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800860
861static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 class_unregister(&firmware_class);
864}
865
Shaohua Lia30a6a22006-09-27 01:50:52 -0700866fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867module_exit(firmware_class_exit);
868
869EXPORT_SYMBOL(release_firmware);
870EXPORT_SYMBOL(request_firmware);
871EXPORT_SYMBOL(request_firmware_nowait);