blob: 04c75b56f4fcec6e03495818bfb4af789c1264a8 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Markus Rechberger87d37a42007-06-04 18:45:44 +020025MODULE_AUTHOR("Manuel Estrada Sainz");
Linus Torvalds1da177e2005-04-16 15:20:36 -070026MODULE_DESCRIPTION("Multi purpose firmware loading support");
27MODULE_LICENSE("GPL");
28
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080029/* Builtin firmware support */
30
31#ifdef CONFIG_FW_LOADER
32
33extern struct builtin_fw __start_builtin_fw[];
34extern struct builtin_fw __end_builtin_fw[];
35
36static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
37{
38 struct builtin_fw *b_fw;
39
40 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
41 if (strcmp(name, b_fw->name) == 0) {
42 fw->size = b_fw->size;
43 fw->data = b_fw->data;
44 return true;
45 }
46 }
47
48 return false;
49}
50
51static bool fw_is_builtin_firmware(const struct firmware *fw)
52{
53 struct builtin_fw *b_fw;
54
55 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
56 if (fw->data == b_fw->data)
57 return true;
58
59 return false;
60}
61
62#else /* Module case - no builtin firmware support */
63
64static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
65{
66 return false;
67}
68
69static inline bool fw_is_builtin_firmware(const struct firmware *fw)
70{
71 return false;
72}
73#endif
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075enum {
76 FW_STATUS_LOADING,
77 FW_STATUS_DONE,
78 FW_STATUS_ABORT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
Dave Jones2f651682007-01-25 15:56:15 -050081static int loading_timeout = 60; /* In seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +020083static inline long firmware_loading_timeout(void)
84{
85 return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
86}
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/* fw_lock could be moved to 'struct firmware_priv' but since it is just
89 * guarding for corner cases a global lock should be OK */
Laura Garciacad1e552006-05-23 23:22:38 +020090static DEFINE_MUTEX(fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92struct firmware_priv {
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 struct completion completion;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 struct firmware *fw;
95 unsigned long status;
Ming Lei65710cb2012-08-04 12:01:16 +080096 void *data;
97 size_t size;
David Woodhouse6e03a202009-04-09 22:04:07 -070098 struct page **pages;
99 int nr_pages;
100 int page_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 struct timer_list timeout;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700102 struct device dev;
Johannes Berge9045f92010-03-29 17:57:20 +0200103 bool nowait;
Dmitry Torokhove1771232010-03-13 23:49:23 -0800104 char fw_id[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105};
106
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700107static struct firmware_priv *to_firmware_priv(struct device *dev)
108{
109 return container_of(dev, struct firmware_priv, dev);
110}
111
112static void fw_load_abort(struct firmware_priv *fw_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 set_bit(FW_STATUS_ABORT, &fw_priv->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 complete(&fw_priv->completion);
116}
117
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700118static ssize_t firmware_timeout_show(struct class *class,
119 struct class_attribute *attr,
120 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 return sprintf(buf, "%d\n", loading_timeout);
123}
124
125/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800126 * firmware_timeout_store - set number of seconds to wait for firmware
127 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800128 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800129 * @buf: buffer to scan for timeout value
130 * @count: number of bytes in @buf
131 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800133 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 * firmware will be provided.
135 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800136 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700138static ssize_t firmware_timeout_store(struct class *class,
139 struct class_attribute *attr,
140 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700143 if (loading_timeout < 0)
144 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return count;
147}
148
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800149static struct class_attribute firmware_class_attrs[] = {
150 __ATTR(timeout, S_IWUSR | S_IRUGO,
151 firmware_timeout_show, firmware_timeout_store),
152 __ATTR_NULL
153};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800155static void fw_dev_release(struct device *dev)
156{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700157 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800158 int i;
159
Ming Lei65710cb2012-08-04 12:01:16 +0800160 /* free untransfered pages buffer */
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800161 for (i = 0; i < fw_priv->nr_pages; i++)
162 __free_page(fw_priv->pages[i]);
163 kfree(fw_priv->pages);
Ming Lei65710cb2012-08-04 12:01:16 +0800164
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800165 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800166
167 module_put(THIS_MODULE);
168}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Kay Sievers7eff2e72007-08-14 15:15:12 +0200170static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700172 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Kay Sievers7eff2e72007-08-14 15:15:12 +0200174 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200176 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700177 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200178 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
179 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 return 0;
182}
183
Adrian Bunk1b81d662006-05-20 15:00:16 -0700184static struct class firmware_class = {
185 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800186 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700187 .dev_uevent = firmware_uevent,
188 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700189};
190
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700191static ssize_t firmware_loading_show(struct device *dev,
192 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700194 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return sprintf(buf, "%d\n", loading);
198}
199
Ming Lei65710cb2012-08-04 12:01:16 +0800200/* firmware holds the ownership of pages */
David Woodhousedd336c52010-05-02 11:21:21 +0300201static void firmware_free_data(const struct firmware *fw)
202{
203 int i;
204 vunmap(fw->data);
205 if (fw->pages) {
206 for (i = 0; i < PFN_UP(fw->size); i++)
207 __free_page(fw->pages[i]);
208 kfree(fw->pages);
209 }
210}
211
David Woodhouse6e03a202009-04-09 22:04:07 -0700212/* Some architectures don't have PAGE_KERNEL_RO */
213#ifndef PAGE_KERNEL_RO
214#define PAGE_KERNEL_RO PAGE_KERNEL
215#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800217 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700218 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800219 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800220 * @buf: buffer to scan for loading control value
221 * @count: number of bytes in @buf
222 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 * The relevant values are:
224 *
225 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800226 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 * -1: Conclude the load with an error and discard any written data.
228 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700229static ssize_t firmware_loading_store(struct device *dev,
230 struct device_attribute *attr,
231 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700233 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700235 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Neil Hormaneea915b2012-01-02 15:31:23 -0500237 mutex_lock(&fw_lock);
238
239 if (!fw_priv->fw)
240 goto out;
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 switch (loading) {
243 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800244 /* discarding any previous partial load */
Ming Lei28eefa72012-08-04 12:01:17 +0800245 if (!test_bit(FW_STATUS_DONE, &fw_priv->status)) {
246 for (i = 0; i < fw_priv->nr_pages; i++)
247 __free_page(fw_priv->pages[i]);
248 kfree(fw_priv->pages);
249 fw_priv->pages = NULL;
250 fw_priv->page_array_size = 0;
251 fw_priv->nr_pages = 0;
252 set_bit(FW_STATUS_LOADING, &fw_priv->status);
253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 break;
255 case 0:
256 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
Ming Lei28eefa72012-08-04 12:01:17 +0800257 set_bit(FW_STATUS_DONE, &fw_priv->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
Ming Lei28eefa72012-08-04 12:01:17 +0800259 complete(&fw_priv->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 break;
261 }
262 /* fallthrough */
263 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700264 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 /* fallthrough */
266 case -1:
267 fw_load_abort(fw_priv);
268 break;
269 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500270out:
271 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return count;
273}
274
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700275static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700277static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
278 struct bin_attribute *bin_attr,
279 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200281 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700282 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 struct firmware *fw;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700284 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Laura Garciacad1e552006-05-23 23:22:38 +0200286 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700288 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 ret_count = -ENODEV;
290 goto out;
291 }
Ming Lei65710cb2012-08-04 12:01:16 +0800292 if (offset > fw_priv->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200293 ret_count = 0;
294 goto out;
295 }
Ming Lei65710cb2012-08-04 12:01:16 +0800296 if (count > fw_priv->size - offset)
297 count = fw_priv->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700298
299 ret_count = count;
300
301 while (count) {
302 void *page_data;
303 int page_nr = offset >> PAGE_SHIFT;
304 int page_ofs = offset & (PAGE_SIZE-1);
305 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
306
307 page_data = kmap(fw_priv->pages[page_nr]);
308
309 memcpy(buffer, page_data + page_ofs, page_cnt);
310
311 kunmap(fw_priv->pages[page_nr]);
312 buffer += page_cnt;
313 offset += page_cnt;
314 count -= page_cnt;
315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316out:
Laura Garciacad1e552006-05-23 23:22:38 +0200317 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return ret_count;
319}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800320
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700321static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
David Woodhouse6e03a202009-04-09 22:04:07 -0700323 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
David Woodhouse6e03a202009-04-09 22:04:07 -0700325 /* If the array of pages is too small, grow it... */
326 if (fw_priv->page_array_size < pages_needed) {
327 int new_array_size = max(pages_needed,
328 fw_priv->page_array_size * 2);
329 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
David Woodhouse6e03a202009-04-09 22:04:07 -0700331 new_pages = kmalloc(new_array_size * sizeof(void *),
332 GFP_KERNEL);
333 if (!new_pages) {
334 fw_load_abort(fw_priv);
335 return -ENOMEM;
336 }
337 memcpy(new_pages, fw_priv->pages,
338 fw_priv->page_array_size * sizeof(void *));
339 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
340 (new_array_size - fw_priv->page_array_size));
341 kfree(fw_priv->pages);
342 fw_priv->pages = new_pages;
343 fw_priv->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700345
346 while (fw_priv->nr_pages < pages_needed) {
347 fw_priv->pages[fw_priv->nr_pages] =
348 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
349
350 if (!fw_priv->pages[fw_priv->nr_pages]) {
351 fw_load_abort(fw_priv);
352 return -ENOMEM;
353 }
354 fw_priv->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return 0;
357}
358
359/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800360 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700361 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700362 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700363 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800364 * @buffer: buffer being written
365 * @offset: buffer offset for write in total data store area
366 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800368 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 * the driver as a firmware image.
370 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700371static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
372 struct bin_attribute *bin_attr,
373 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200375 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700376 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 struct firmware *fw;
378 ssize_t retval;
379
380 if (!capable(CAP_SYS_RAWIO))
381 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700382
Laura Garciacad1e552006-05-23 23:22:38 +0200383 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700385 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 retval = -ENODEV;
387 goto out;
388 }
Ming Lei65710cb2012-08-04 12:01:16 +0800389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 retval = fw_realloc_buffer(fw_priv, offset + count);
391 if (retval)
392 goto out;
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700395
396 while (count) {
397 void *page_data;
398 int page_nr = offset >> PAGE_SHIFT;
399 int page_ofs = offset & (PAGE_SIZE - 1);
400 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
401
402 page_data = kmap(fw_priv->pages[page_nr]);
403
404 memcpy(page_data + page_ofs, buffer, page_cnt);
405
406 kunmap(fw_priv->pages[page_nr]);
407 buffer += page_cnt;
408 offset += page_cnt;
409 count -= page_cnt;
410 }
411
Ming Lei65710cb2012-08-04 12:01:16 +0800412 fw_priv->size = max_t(size_t, offset, fw_priv->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413out:
Laura Garciacad1e552006-05-23 23:22:38 +0200414 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return retval;
416}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800417
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700418static struct bin_attribute firmware_attr_data = {
419 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 .size = 0,
421 .read = firmware_data_read,
422 .write = firmware_data_write,
423};
424
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700425static void firmware_class_timeout(u_long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 fw_load_abort(fw_priv);
430}
431
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700432static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200433fw_create_instance(struct firmware *firmware, const char *fw_name,
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700434 struct device *device, bool uevent, bool nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700436 struct firmware_priv *fw_priv;
437 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700439 fw_priv = kzalloc(sizeof(*fw_priv) + strlen(fw_name) + 1 , GFP_KERNEL);
440 if (!fw_priv) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700441 dev_err(device, "%s: kmalloc failed\n", __func__);
Stephen Boyddddb5542012-03-28 23:30:43 +0200442 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Stephen Boyddddb5542012-03-28 23:30:43 +0200445 fw_priv->fw = firmware;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700446 fw_priv->nowait = nowait;
Dmitry Torokhove1771232010-03-13 23:49:23 -0800447 strcpy(fw_priv->fw_id, fw_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 init_completion(&fw_priv->completion);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700449 setup_timer(&fw_priv->timeout,
450 firmware_class_timeout, (u_long) fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700452 f_dev = &fw_priv->dev;
453
454 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800455 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700456 f_dev->parent = device;
457 f_dev->class = &firmware_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700459 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
Stephen Boyddddb5542012-03-28 23:30:43 +0200462static struct firmware_priv *
463_request_firmware_prepare(const struct firmware **firmware_p, const char *name,
464 struct device *device, bool uevent, bool nowait)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700465{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 struct firmware *firmware;
Stephen Boyddddb5542012-03-28 23:30:43 +0200467 struct firmware_priv *fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 if (!firmware_p)
Stephen Boyddddb5542012-03-28 23:30:43 +0200470 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Jiri Slaby4aed0642005-09-13 01:25:01 -0700472 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700474 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
475 __func__);
Stephen Boyddddb5542012-03-28 23:30:43 +0200476 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800479 if (fw_get_builtin_firmware(firmware, name)) {
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100480 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
Stephen Boyddddb5542012-03-28 23:30:43 +0200481 return NULL;
David Woodhouse5658c762008-05-23 13:52:42 +0100482 }
483
Stephen Boyddddb5542012-03-28 23:30:43 +0200484 fw_priv = fw_create_instance(firmware, name, device, uevent, nowait);
485 if (IS_ERR(fw_priv)) {
486 release_firmware(firmware);
487 *firmware_p = NULL;
488 }
489 return fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200490}
491
492static void _request_firmware_cleanup(const struct firmware **firmware_p)
493{
494 release_firmware(*firmware_p);
495 *firmware_p = NULL;
496}
497
Ming Lei65710cb2012-08-04 12:01:16 +0800498/* transfer the ownership of pages to firmware */
499static int fw_set_page_data(struct firmware_priv *fw_priv)
500{
501 struct firmware *fw = fw_priv->fw;
502
503 fw_priv->data = vmap(fw_priv->pages, fw_priv->nr_pages,
504 0, PAGE_KERNEL_RO);
505 if (!fw_priv->data)
506 return -ENOMEM;
507
508 fw->data = fw_priv->data;
509 fw->pages = fw_priv->pages;
510 fw->size = fw_priv->size;
511
512 WARN_ON(PFN_UP(fw->size) != fw_priv->nr_pages);
513
514 fw_priv->nr_pages = 0;
515 fw_priv->pages = NULL;
516 fw_priv->data = NULL;
517
518 return 0;
519}
520
Stephen Boyddddb5542012-03-28 23:30:43 +0200521static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
522 long timeout)
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200523{
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200524 int retval = 0;
Stephen Boyddddb5542012-03-28 23:30:43 +0200525 struct device *f_dev = &fw_priv->dev;
Linus Torvaldscaca9512011-08-24 15:55:30 -0700526
Stephen Boyddddb5542012-03-28 23:30:43 +0200527 dev_set_uevent_suppress(f_dev, true);
David Woodhouse5658c762008-05-23 13:52:42 +0100528
Stephen Boyddddb5542012-03-28 23:30:43 +0200529 /* Need to pin this module until class device is destroyed */
530 __module_get(THIS_MODULE);
531
532 retval = device_add(f_dev);
533 if (retval) {
534 dev_err(f_dev, "%s: device_register failed\n", __func__);
535 goto err_put_dev;
536 }
537
538 retval = device_create_bin_file(f_dev, &firmware_attr_data);
539 if (retval) {
540 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
541 goto err_del_dev;
542 }
543
544 retval = device_create_file(f_dev, &dev_attr_loading);
545 if (retval) {
546 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
547 goto err_del_bin_attr;
548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Kay Sievers312c0042005-11-16 09:00:00 +0100550 if (uevent) {
Stephen Boyddddb5542012-03-28 23:30:43 +0200551 dev_set_uevent_suppress(f_dev, false);
552 dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_id);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200553 if (timeout != MAX_SCHEDULE_TIMEOUT)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700554 mod_timer(&fw_priv->timeout,
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200555 round_jiffies_up(jiffies + timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700557 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
558 }
559
560 wait_for_completion(&fw_priv->completion);
561
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700562 del_timer_sync(&fw_priv->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Laura Garciacad1e552006-05-23 23:22:38 +0200564 mutex_lock(&fw_lock);
Ming Lei65710cb2012-08-04 12:01:16 +0800565 if (!fw_priv->size || test_bit(FW_STATUS_ABORT, &fw_priv->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 retval = -ENOENT;
Ming Lei65710cb2012-08-04 12:01:16 +0800567
568 /* transfer pages ownership at the last minute */
569 if (!retval)
570 retval = fw_set_page_data(fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 fw_priv->fw = NULL;
Laura Garciacad1e552006-05-23 23:22:38 +0200572 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Stephen Boyddddb5542012-03-28 23:30:43 +0200574 device_remove_file(f_dev, &dev_attr_loading);
575err_del_bin_attr:
576 device_remove_bin_file(f_dev, &firmware_attr_data);
577err_del_dev:
578 device_del(f_dev);
579err_put_dev:
580 put_device(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return retval;
582}
583
584/**
Kay Sievers312c0042005-11-16 09:00:00 +0100585 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800586 * @firmware_p: pointer to firmware image
587 * @name: name of firmware file
588 * @device: device for which firmware is being loaded
589 *
590 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700591 * of @name for device @device.
592 *
593 * Should be called from user context where sleeping is allowed.
594 *
Kay Sievers312c0042005-11-16 09:00:00 +0100595 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700596 * should be distinctive enough not to be confused with any other
597 * firmware image for this or any other device.
598 **/
599int
600request_firmware(const struct firmware **firmware_p, const char *name,
601 struct device *device)
602{
Stephen Boyddddb5542012-03-28 23:30:43 +0200603 struct firmware_priv *fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200604 int ret;
605
Stephen Boyddddb5542012-03-28 23:30:43 +0200606 fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
607 false);
608 if (IS_ERR_OR_NULL(fw_priv))
609 return PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200610
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200611 ret = usermodehelper_read_trylock();
612 if (WARN_ON(ret)) {
613 dev_err(device, "firmware: %s will not be loaded\n", name);
614 } else {
Stephen Boyddddb5542012-03-28 23:30:43 +0200615 ret = _request_firmware_load(fw_priv, true,
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200616 firmware_loading_timeout());
617 usermodehelper_read_unlock();
618 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200619 if (ret)
620 _request_firmware_cleanup(firmware_p);
621
622 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700623}
624
625/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800627 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800629void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800632 if (!fw_is_builtin_firmware(fw))
633 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 kfree(fw);
635 }
636}
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638/* Async support */
639struct firmware_work {
640 struct work_struct work;
641 struct module *module;
642 const char *name;
643 struct device *device;
644 void *context;
645 void (*cont)(const struct firmware *fw, void *context);
Bob Liu072fc8f2011-01-26 18:33:32 +0800646 bool uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647};
648
Stephen Boyda36cf842012-03-28 23:31:00 +0200649static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Stephen Boyda36cf842012-03-28 23:31:00 +0200651 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 const struct firmware *fw;
Stephen Boyddddb5542012-03-28 23:30:43 +0200653 struct firmware_priv *fw_priv;
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200654 long timeout;
matthieu castet113fab12005-11-13 16:07:39 -0800655 int ret;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700656
Stephen Boyda36cf842012-03-28 23:31:00 +0200657 fw_work = container_of(work, struct firmware_work, work);
Stephen Boyddddb5542012-03-28 23:30:43 +0200658 fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
659 fw_work->uevent, true);
660 if (IS_ERR_OR_NULL(fw_priv)) {
661 ret = PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200662 goto out;
Stephen Boyddddb5542012-03-28 23:30:43 +0200663 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200664
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200665 timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
666 if (timeout) {
Stephen Boyddddb5542012-03-28 23:30:43 +0200667 ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200668 usermodehelper_read_unlock();
669 } else {
670 dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
671 fw_work->name);
672 ret = -EAGAIN;
673 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200674 if (ret)
675 _request_firmware_cleanup(&fw);
676
677 out:
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100678 fw_work->cont(fw, fw_work->context);
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 module_put(fw_work->module);
681 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
684/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000685 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800686 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +0100687 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800688 * is non-zero else the firmware copy must be done manually.
689 * @name: name of firmware file
690 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100691 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800692 * @context: will be passed over to @cont, and
693 * @fw may be %NULL if firmware request fails.
694 * @cont: function will be called asynchronously when the firmware
695 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 *
Ming Lei7fcab092009-05-29 11:33:19 +0800697 * Asynchronous variant of request_firmware() for user contexts where
698 * it is not possible to sleep for long time. It can't be called
699 * in atomic contexts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 **/
701int
702request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +0800703 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100704 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 void (*cont)(const struct firmware *fw, void *context))
706{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700707 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700709 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (!fw_work)
711 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700712
713 fw_work->module = module;
714 fw_work->name = name;
715 fw_work->device = device;
716 fw_work->context = context;
717 fw_work->cont = cont;
718 fw_work->uevent = uevent;
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (!try_module_get(module)) {
721 kfree(fw_work);
722 return -EFAULT;
723 }
724
Stephen Boyda36cf842012-03-28 23:31:00 +0200725 INIT_WORK(&fw_work->work, request_firmware_work_func);
726 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return 0;
728}
729
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800730static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800732 return class_register(&firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800734
735static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 class_unregister(&firmware_class);
738}
739
Shaohua Lia30a6a22006-09-27 01:50:52 -0700740fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741module_exit(firmware_class_exit);
742
743EXPORT_SYMBOL(release_firmware);
744EXPORT_SYMBOL(request_firmware);
745EXPORT_SYMBOL(request_firmware_nowait);