blob: 5f2076e5d5b1603172699c2db346208dedd88942 [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
Ming Lei12446912012-08-04 12:01:20 +080092struct firmware_buf {
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;
Dmitry Torokhove1771232010-03-13 23:49:23 -0800101 char fw_id[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
Ming Lei12446912012-08-04 12:01:20 +0800104struct firmware_priv {
105 struct timer_list timeout;
106 bool nowait;
107 struct device dev;
108 struct firmware_buf *buf;
109};
110
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700111static struct firmware_priv *to_firmware_priv(struct device *dev)
112{
113 return container_of(dev, struct firmware_priv, dev);
114}
115
116static void fw_load_abort(struct firmware_priv *fw_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Ming Lei12446912012-08-04 12:01:20 +0800118 struct firmware_buf *buf = fw_priv->buf;
119
120 set_bit(FW_STATUS_ABORT, &buf->status);
121 complete(&buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700124static ssize_t firmware_timeout_show(struct class *class,
125 struct class_attribute *attr,
126 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 return sprintf(buf, "%d\n", loading_timeout);
129}
130
131/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800132 * firmware_timeout_store - set number of seconds to wait for firmware
133 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800134 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800135 * @buf: buffer to scan for timeout value
136 * @count: number of bytes in @buf
137 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800139 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 * firmware will be provided.
141 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800142 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700144static ssize_t firmware_timeout_store(struct class *class,
145 struct class_attribute *attr,
146 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700149 if (loading_timeout < 0)
150 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 return count;
153}
154
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800155static struct class_attribute firmware_class_attrs[] = {
156 __ATTR(timeout, S_IWUSR | S_IRUGO,
157 firmware_timeout_show, firmware_timeout_store),
158 __ATTR_NULL
159};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Ming Lei12446912012-08-04 12:01:20 +0800161static void fw_free_buf(struct firmware_buf *buf)
162{
163 int i;
164
165 if (!buf)
166 return;
167
168 for (i = 0; i < buf->nr_pages; i++)
169 __free_page(buf->pages[i]);
170 kfree(buf->pages);
171}
172
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800173static void fw_dev_release(struct device *dev)
174{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700175 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800176
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800177 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800178
179 module_put(THIS_MODULE);
180}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Kay Sievers7eff2e72007-08-14 15:15:12 +0200182static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700184 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Ming Lei12446912012-08-04 12:01:20 +0800186 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200188 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700189 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200190 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
191 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 return 0;
194}
195
Adrian Bunk1b81d662006-05-20 15:00:16 -0700196static struct class firmware_class = {
197 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800198 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700199 .dev_uevent = firmware_uevent,
200 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700201};
202
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700203static ssize_t firmware_loading_show(struct device *dev,
204 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700206 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800207 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return sprintf(buf, "%d\n", loading);
210}
211
Ming Lei65710cb2012-08-04 12:01:16 +0800212/* firmware holds the ownership of pages */
David Woodhousedd336c52010-05-02 11:21:21 +0300213static void firmware_free_data(const struct firmware *fw)
214{
215 int i;
216 vunmap(fw->data);
217 if (fw->pages) {
218 for (i = 0; i < PFN_UP(fw->size); i++)
219 __free_page(fw->pages[i]);
220 kfree(fw->pages);
221 }
222}
223
David Woodhouse6e03a202009-04-09 22:04:07 -0700224/* Some architectures don't have PAGE_KERNEL_RO */
225#ifndef PAGE_KERNEL_RO
226#define PAGE_KERNEL_RO PAGE_KERNEL
227#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800229 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700230 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800231 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800232 * @buf: buffer to scan for loading control value
233 * @count: number of bytes in @buf
234 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 * The relevant values are:
236 *
237 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800238 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 * -1: Conclude the load with an error and discard any written data.
240 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700241static ssize_t firmware_loading_store(struct device *dev,
242 struct device_attribute *attr,
243 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700245 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800246 struct firmware_buf *fw_buf = fw_priv->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700248 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Neil Hormaneea915b2012-01-02 15:31:23 -0500250 mutex_lock(&fw_lock);
251
Ming Lei12446912012-08-04 12:01:20 +0800252 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500253 goto out;
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 switch (loading) {
256 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800257 /* discarding any previous partial load */
Ming Lei12446912012-08-04 12:01:20 +0800258 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
259 for (i = 0; i < fw_buf->nr_pages; i++)
260 __free_page(fw_buf->pages[i]);
261 kfree(fw_buf->pages);
262 fw_buf->pages = NULL;
263 fw_buf->page_array_size = 0;
264 fw_buf->nr_pages = 0;
265 set_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei28eefa72012-08-04 12:01:17 +0800266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 break;
268 case 0:
Ming Lei12446912012-08-04 12:01:20 +0800269 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
270 set_bit(FW_STATUS_DONE, &fw_buf->status);
271 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
272 complete(&fw_buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 break;
274 }
275 /* fallthrough */
276 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700277 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 /* fallthrough */
279 case -1:
280 fw_load_abort(fw_priv);
281 break;
282 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500283out:
284 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return count;
286}
287
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700288static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700290static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
291 struct bin_attribute *bin_attr,
292 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200294 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700295 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800296 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700297 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Laura Garciacad1e552006-05-23 23:22:38 +0200299 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800300 buf = fw_priv->buf;
301 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 ret_count = -ENODEV;
303 goto out;
304 }
Ming Lei12446912012-08-04 12:01:20 +0800305 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200306 ret_count = 0;
307 goto out;
308 }
Ming Lei12446912012-08-04 12:01:20 +0800309 if (count > buf->size - offset)
310 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700311
312 ret_count = count;
313
314 while (count) {
315 void *page_data;
316 int page_nr = offset >> PAGE_SHIFT;
317 int page_ofs = offset & (PAGE_SIZE-1);
318 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
319
Ming Lei12446912012-08-04 12:01:20 +0800320 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700321
322 memcpy(buffer, page_data + page_ofs, page_cnt);
323
Ming Lei12446912012-08-04 12:01:20 +0800324 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700325 buffer += page_cnt;
326 offset += page_cnt;
327 count -= page_cnt;
328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329out:
Laura Garciacad1e552006-05-23 23:22:38 +0200330 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return ret_count;
332}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800333
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700334static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Ming Lei12446912012-08-04 12:01:20 +0800336 struct firmware_buf *buf = fw_priv->buf;
David Woodhouse6e03a202009-04-09 22:04:07 -0700337 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
David Woodhouse6e03a202009-04-09 22:04:07 -0700339 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800340 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700341 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800342 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700343 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
David Woodhouse6e03a202009-04-09 22:04:07 -0700345 new_pages = kmalloc(new_array_size * sizeof(void *),
346 GFP_KERNEL);
347 if (!new_pages) {
348 fw_load_abort(fw_priv);
349 return -ENOMEM;
350 }
Ming Lei12446912012-08-04 12:01:20 +0800351 memcpy(new_pages, buf->pages,
352 buf->page_array_size * sizeof(void *));
353 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
354 (new_array_size - buf->page_array_size));
355 kfree(buf->pages);
356 buf->pages = new_pages;
357 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700359
Ming Lei12446912012-08-04 12:01:20 +0800360 while (buf->nr_pages < pages_needed) {
361 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700362 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
363
Ming Lei12446912012-08-04 12:01:20 +0800364 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700365 fw_load_abort(fw_priv);
366 return -ENOMEM;
367 }
Ming Lei12446912012-08-04 12:01:20 +0800368 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return 0;
371}
372
373/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800374 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700375 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700376 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700377 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800378 * @buffer: buffer being written
379 * @offset: buffer offset for write in total data store area
380 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800382 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 * the driver as a firmware image.
384 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700385static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
386 struct bin_attribute *bin_attr,
387 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200389 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700390 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800391 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 ssize_t retval;
393
394 if (!capable(CAP_SYS_RAWIO))
395 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700396
Laura Garciacad1e552006-05-23 23:22:38 +0200397 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800398 buf = fw_priv->buf;
399 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 retval = -ENODEV;
401 goto out;
402 }
Ming Lei65710cb2012-08-04 12:01:16 +0800403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 retval = fw_realloc_buffer(fw_priv, offset + count);
405 if (retval)
406 goto out;
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700409
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(page_data + page_ofs, buffer, 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 }
425
Ming Lei12446912012-08-04 12:01:20 +0800426 buf->size = max_t(size_t, offset, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427out:
Laura Garciacad1e552006-05-23 23:22:38 +0200428 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return retval;
430}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800431
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700432static struct bin_attribute firmware_attr_data = {
433 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 .size = 0,
435 .read = firmware_data_read,
436 .write = firmware_data_write,
437};
438
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700439static void firmware_class_timeout(u_long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 fw_load_abort(fw_priv);
444}
445
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700446static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200447fw_create_instance(struct firmware *firmware, const char *fw_name,
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700448 struct device *device, bool uevent, bool nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700450 struct firmware_priv *fw_priv;
Ming Lei12446912012-08-04 12:01:20 +0800451 struct firmware_buf *buf;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700452 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Ming Lei12446912012-08-04 12:01:20 +0800454 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700455 if (!fw_priv) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700456 dev_err(device, "%s: kmalloc failed\n", __func__);
Ming Lei12446912012-08-04 12:01:20 +0800457 fw_priv = ERR_PTR(-ENOMEM);
458 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Ming Lei12446912012-08-04 12:01:20 +0800461 buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1, GFP_KERNEL);
462 if (!buf) {
463 dev_err(device, "%s: kmalloc failed\n", __func__);
464 kfree(fw_priv);
465 fw_priv = ERR_PTR(-ENOMEM);
466 goto exit;
467 }
468
469 buf->fw = firmware;
470 fw_priv->buf = buf;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700471 fw_priv->nowait = nowait;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700472 setup_timer(&fw_priv->timeout,
473 firmware_class_timeout, (u_long) fw_priv);
Ming Lei12446912012-08-04 12:01:20 +0800474 strcpy(buf->fw_id, fw_name);
475 init_completion(&buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700477 f_dev = &fw_priv->dev;
478
479 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800480 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700481 f_dev->parent = device;
482 f_dev->class = &firmware_class;
Ming Lei12446912012-08-04 12:01:20 +0800483exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700484 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
Stephen Boyddddb5542012-03-28 23:30:43 +0200487static struct firmware_priv *
488_request_firmware_prepare(const struct firmware **firmware_p, const char *name,
489 struct device *device, bool uevent, bool nowait)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700490{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 struct firmware *firmware;
Stephen Boyddddb5542012-03-28 23:30:43 +0200492 struct firmware_priv *fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 if (!firmware_p)
Stephen Boyddddb5542012-03-28 23:30:43 +0200495 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Jiri Slaby4aed0642005-09-13 01:25:01 -0700497 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700499 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
500 __func__);
Stephen Boyddddb5542012-03-28 23:30:43 +0200501 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800504 if (fw_get_builtin_firmware(firmware, name)) {
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100505 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
Stephen Boyddddb5542012-03-28 23:30:43 +0200506 return NULL;
David Woodhouse5658c762008-05-23 13:52:42 +0100507 }
508
Stephen Boyddddb5542012-03-28 23:30:43 +0200509 fw_priv = fw_create_instance(firmware, name, device, uevent, nowait);
510 if (IS_ERR(fw_priv)) {
511 release_firmware(firmware);
512 *firmware_p = NULL;
513 }
514 return fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200515}
516
517static void _request_firmware_cleanup(const struct firmware **firmware_p)
518{
519 release_firmware(*firmware_p);
520 *firmware_p = NULL;
521}
522
Ming Lei65710cb2012-08-04 12:01:16 +0800523/* transfer the ownership of pages to firmware */
Ming Lei12446912012-08-04 12:01:20 +0800524static int fw_set_page_data(struct firmware_buf *buf)
Ming Lei65710cb2012-08-04 12:01:16 +0800525{
Ming Lei12446912012-08-04 12:01:20 +0800526 struct firmware *fw = buf->fw;
Ming Lei65710cb2012-08-04 12:01:16 +0800527
Ming Lei12446912012-08-04 12:01:20 +0800528 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
529 if (!buf->data)
Ming Lei65710cb2012-08-04 12:01:16 +0800530 return -ENOMEM;
531
Ming Lei12446912012-08-04 12:01:20 +0800532 fw->data = buf->data;
533 fw->pages = buf->pages;
534 fw->size = buf->size;
535 WARN_ON(PFN_UP(fw->size) != buf->nr_pages);
Ming Lei65710cb2012-08-04 12:01:16 +0800536
537 return 0;
538}
539
Stephen Boyddddb5542012-03-28 23:30:43 +0200540static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
541 long timeout)
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200542{
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200543 int retval = 0;
Stephen Boyddddb5542012-03-28 23:30:43 +0200544 struct device *f_dev = &fw_priv->dev;
Ming Lei12446912012-08-04 12:01:20 +0800545 struct firmware_buf *buf = fw_priv->buf;
Linus Torvaldscaca9512011-08-24 15:55:30 -0700546
Stephen Boyddddb5542012-03-28 23:30:43 +0200547 dev_set_uevent_suppress(f_dev, true);
David Woodhouse5658c762008-05-23 13:52:42 +0100548
Stephen Boyddddb5542012-03-28 23:30:43 +0200549 /* Need to pin this module until class device is destroyed */
550 __module_get(THIS_MODULE);
551
552 retval = device_add(f_dev);
553 if (retval) {
554 dev_err(f_dev, "%s: device_register failed\n", __func__);
555 goto err_put_dev;
556 }
557
558 retval = device_create_bin_file(f_dev, &firmware_attr_data);
559 if (retval) {
560 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
561 goto err_del_dev;
562 }
563
564 retval = device_create_file(f_dev, &dev_attr_loading);
565 if (retval) {
566 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
567 goto err_del_bin_attr;
568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Kay Sievers312c0042005-11-16 09:00:00 +0100570 if (uevent) {
Stephen Boyddddb5542012-03-28 23:30:43 +0200571 dev_set_uevent_suppress(f_dev, false);
Ming Lei12446912012-08-04 12:01:20 +0800572 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200573 if (timeout != MAX_SCHEDULE_TIMEOUT)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700574 mod_timer(&fw_priv->timeout,
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200575 round_jiffies_up(jiffies + timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700577 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
578 }
579
Ming Lei12446912012-08-04 12:01:20 +0800580 wait_for_completion(&buf->completion);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700581
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700582 del_timer_sync(&fw_priv->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Laura Garciacad1e552006-05-23 23:22:38 +0200584 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800585 if (!buf->size || test_bit(FW_STATUS_ABORT, &buf->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 retval = -ENOENT;
Ming Lei65710cb2012-08-04 12:01:16 +0800587
588 /* transfer pages ownership at the last minute */
589 if (!retval)
Ming Lei12446912012-08-04 12:01:20 +0800590 retval = fw_set_page_data(buf);
591 if (retval)
592 fw_free_buf(buf); /* free untransfered pages buffer */
593
594 kfree(buf);
595 fw_priv->buf = NULL;
Laura Garciacad1e552006-05-23 23:22:38 +0200596 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Stephen Boyddddb5542012-03-28 23:30:43 +0200598 device_remove_file(f_dev, &dev_attr_loading);
599err_del_bin_attr:
600 device_remove_bin_file(f_dev, &firmware_attr_data);
601err_del_dev:
602 device_del(f_dev);
603err_put_dev:
604 put_device(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return retval;
606}
607
608/**
Kay Sievers312c0042005-11-16 09:00:00 +0100609 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800610 * @firmware_p: pointer to firmware image
611 * @name: name of firmware file
612 * @device: device for which firmware is being loaded
613 *
614 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700615 * of @name for device @device.
616 *
617 * Should be called from user context where sleeping is allowed.
618 *
Kay Sievers312c0042005-11-16 09:00:00 +0100619 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700620 * should be distinctive enough not to be confused with any other
621 * firmware image for this or any other device.
622 **/
623int
624request_firmware(const struct firmware **firmware_p, const char *name,
625 struct device *device)
626{
Stephen Boyddddb5542012-03-28 23:30:43 +0200627 struct firmware_priv *fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200628 int ret;
629
Stephen Boyddddb5542012-03-28 23:30:43 +0200630 fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
631 false);
632 if (IS_ERR_OR_NULL(fw_priv))
633 return PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200634
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200635 ret = usermodehelper_read_trylock();
636 if (WARN_ON(ret)) {
637 dev_err(device, "firmware: %s will not be loaded\n", name);
638 } else {
Stephen Boyddddb5542012-03-28 23:30:43 +0200639 ret = _request_firmware_load(fw_priv, true,
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200640 firmware_loading_timeout());
641 usermodehelper_read_unlock();
642 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200643 if (ret)
644 _request_firmware_cleanup(firmware_p);
645
646 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700647}
648
649/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800651 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800653void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
655 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800656 if (!fw_is_builtin_firmware(fw))
657 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 kfree(fw);
659 }
660}
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662/* Async support */
663struct firmware_work {
664 struct work_struct work;
665 struct module *module;
666 const char *name;
667 struct device *device;
668 void *context;
669 void (*cont)(const struct firmware *fw, void *context);
Bob Liu072fc8f2011-01-26 18:33:32 +0800670 bool uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671};
672
Stephen Boyda36cf842012-03-28 23:31:00 +0200673static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Stephen Boyda36cf842012-03-28 23:31:00 +0200675 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 const struct firmware *fw;
Stephen Boyddddb5542012-03-28 23:30:43 +0200677 struct firmware_priv *fw_priv;
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200678 long timeout;
matthieu castet113fab12005-11-13 16:07:39 -0800679 int ret;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700680
Stephen Boyda36cf842012-03-28 23:31:00 +0200681 fw_work = container_of(work, struct firmware_work, work);
Stephen Boyddddb5542012-03-28 23:30:43 +0200682 fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
683 fw_work->uevent, true);
684 if (IS_ERR_OR_NULL(fw_priv)) {
685 ret = PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200686 goto out;
Stephen Boyddddb5542012-03-28 23:30:43 +0200687 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200688
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200689 timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
690 if (timeout) {
Stephen Boyddddb5542012-03-28 23:30:43 +0200691 ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200692 usermodehelper_read_unlock();
693 } else {
694 dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
695 fw_work->name);
696 ret = -EAGAIN;
697 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200698 if (ret)
699 _request_firmware_cleanup(&fw);
700
701 out:
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100702 fw_work->cont(fw, fw_work->context);
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 module_put(fw_work->module);
705 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
708/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000709 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800710 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +0100711 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800712 * is non-zero else the firmware copy must be done manually.
713 * @name: name of firmware file
714 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100715 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800716 * @context: will be passed over to @cont, and
717 * @fw may be %NULL if firmware request fails.
718 * @cont: function will be called asynchronously when the firmware
719 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 *
Ming Lei7fcab092009-05-29 11:33:19 +0800721 * Asynchronous variant of request_firmware() for user contexts where
722 * it is not possible to sleep for long time. It can't be called
723 * in atomic contexts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 **/
725int
726request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +0800727 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100728 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 void (*cont)(const struct firmware *fw, void *context))
730{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700731 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700733 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (!fw_work)
735 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700736
737 fw_work->module = module;
738 fw_work->name = name;
739 fw_work->device = device;
740 fw_work->context = context;
741 fw_work->cont = cont;
742 fw_work->uevent = uevent;
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (!try_module_get(module)) {
745 kfree(fw_work);
746 return -EFAULT;
747 }
748
Stephen Boyda36cf842012-03-28 23:31:00 +0200749 INIT_WORK(&fw_work->work, request_firmware_work_func);
750 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return 0;
752}
753
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800754static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800756 return class_register(&firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800758
759static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
761 class_unregister(&firmware_class);
762}
763
Shaohua Lia30a6a22006-09-27 01:50:52 -0700764fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765module_exit(firmware_class_exit);
766
767EXPORT_SYMBOL(release_firmware);
768EXPORT_SYMBOL(request_firmware);
769EXPORT_SYMBOL(request_firmware_nowait);