blob: ae00a2fd280fbddbd060c5aa9c90e0ac3bf70951 [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>
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -070019#include <linux/kthread.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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -070024#define to_dev(obj) container_of(obj, struct device, kobj)
25
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/* fw_lock could be moved to 'struct firmware_priv' but since it is just
90 * guarding for corner cases a global lock should be OK */
Laura Garciacad1e552006-05-23 23:22:38 +020091static DEFINE_MUTEX(fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93struct firmware_priv {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 struct completion completion;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 struct firmware *fw;
96 unsigned long status;
David Woodhouse6e03a202009-04-09 22:04:07 -070097 struct page **pages;
98 int nr_pages;
99 int page_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 struct timer_list timeout;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700101 struct device dev;
Johannes Berge9045f92010-03-29 17:57:20 +0200102 bool nowait;
Dmitry Torokhove1771232010-03-13 23:49:23 -0800103 char fw_id[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
105
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700106static struct firmware_priv *to_firmware_priv(struct device *dev)
107{
108 return container_of(dev, struct firmware_priv, dev);
109}
110
111static void fw_load_abort(struct firmware_priv *fw_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 set_bit(FW_STATUS_ABORT, &fw_priv->status);
114 wmb();
115 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
160 for (i = 0; i < fw_priv->nr_pages; i++)
161 __free_page(fw_priv->pages[i]);
162 kfree(fw_priv->pages);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800163 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800164
165 module_put(THIS_MODULE);
166}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Kay Sievers7eff2e72007-08-14 15:15:12 +0200168static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700170 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Kay Sievers7eff2e72007-08-14 15:15:12 +0200172 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200174 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700175 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200176 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
177 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 return 0;
180}
181
Adrian Bunk1b81d662006-05-20 15:00:16 -0700182static struct class firmware_class = {
183 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800184 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700185 .dev_uevent = firmware_uevent,
186 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700187};
188
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700189static ssize_t firmware_loading_show(struct device *dev,
190 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700192 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return sprintf(buf, "%d\n", loading);
196}
197
David Woodhousedd336c52010-05-02 11:21:21 +0300198static void firmware_free_data(const struct firmware *fw)
199{
200 int i;
201 vunmap(fw->data);
202 if (fw->pages) {
203 for (i = 0; i < PFN_UP(fw->size); i++)
204 __free_page(fw->pages[i]);
205 kfree(fw->pages);
206 }
207}
208
David Woodhouse6e03a202009-04-09 22:04:07 -0700209/* Some architectures don't have PAGE_KERNEL_RO */
210#ifndef PAGE_KERNEL_RO
211#define PAGE_KERNEL_RO PAGE_KERNEL
212#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800214 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700215 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800216 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800217 * @buf: buffer to scan for loading control value
218 * @count: number of bytes in @buf
219 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 * The relevant values are:
221 *
222 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800223 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 * -1: Conclude the load with an error and discard any written data.
225 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700226static ssize_t firmware_loading_store(struct device *dev,
227 struct device_attribute *attr,
228 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700230 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700232 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Neil Hormaneea915b2012-01-02 15:31:23 -0500234 mutex_lock(&fw_lock);
235
236 if (!fw_priv->fw)
237 goto out;
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 switch (loading) {
240 case 1:
David Woodhousedd336c52010-05-02 11:21:21 +0300241 firmware_free_data(fw_priv->fw);
242 memset(fw_priv->fw, 0, sizeof(struct firmware));
243 /* If the pages are not owned by 'struct firmware' */
David Woodhouse6e03a202009-04-09 22:04:07 -0700244 for (i = 0; i < fw_priv->nr_pages; i++)
245 __free_page(fw_priv->pages[i]);
246 kfree(fw_priv->pages);
247 fw_priv->pages = NULL;
248 fw_priv->page_array_size = 0;
249 fw_priv->nr_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 set_bit(FW_STATUS_LOADING, &fw_priv->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 break;
252 case 0:
253 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
David Woodhousedd336c52010-05-02 11:21:21 +0300254 vunmap(fw_priv->fw->data);
David Woodhouse6e03a202009-04-09 22:04:07 -0700255 fw_priv->fw->data = vmap(fw_priv->pages,
256 fw_priv->nr_pages,
257 0, PAGE_KERNEL_RO);
258 if (!fw_priv->fw->data) {
259 dev_err(dev, "%s: vmap() failed\n", __func__);
260 goto err;
261 }
David Woodhousedd336c52010-05-02 11:21:21 +0300262 /* Pages are now owned by 'struct firmware' */
263 fw_priv->fw->pages = fw_priv->pages;
264 fw_priv->pages = NULL;
265
David Woodhouse6e03a202009-04-09 22:04:07 -0700266 fw_priv->page_array_size = 0;
267 fw_priv->nr_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 complete(&fw_priv->completion);
269 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
270 break;
271 }
272 /* fallthrough */
273 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700274 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 /* fallthrough */
276 case -1:
David Woodhouse6e03a202009-04-09 22:04:07 -0700277 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 fw_load_abort(fw_priv);
279 break;
280 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500281out:
282 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return count;
284}
285
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700286static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700288static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
289 struct bin_attribute *bin_attr,
290 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700292 struct device *dev = to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700293 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 struct firmware *fw;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700295 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Laura Garciacad1e552006-05-23 23:22:38 +0200297 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700299 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 ret_count = -ENODEV;
301 goto out;
302 }
Jiri Slaby308975f2009-06-21 23:57:31 +0200303 if (offset > fw->size) {
304 ret_count = 0;
305 goto out;
306 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700307 if (count > fw->size - offset)
308 count = fw->size - offset;
309
310 ret_count = count;
311
312 while (count) {
313 void *page_data;
314 int page_nr = offset >> PAGE_SHIFT;
315 int page_ofs = offset & (PAGE_SIZE-1);
316 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
317
318 page_data = kmap(fw_priv->pages[page_nr]);
319
320 memcpy(buffer, page_data + page_ofs, page_cnt);
321
322 kunmap(fw_priv->pages[page_nr]);
323 buffer += page_cnt;
324 offset += page_cnt;
325 count -= page_cnt;
326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327out:
Laura Garciacad1e552006-05-23 23:22:38 +0200328 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return ret_count;
330}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800331
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700332static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
David Woodhouse6e03a202009-04-09 22:04:07 -0700334 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
David Woodhouse6e03a202009-04-09 22:04:07 -0700336 /* If the array of pages is too small, grow it... */
337 if (fw_priv->page_array_size < pages_needed) {
338 int new_array_size = max(pages_needed,
339 fw_priv->page_array_size * 2);
340 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
David Woodhouse6e03a202009-04-09 22:04:07 -0700342 new_pages = kmalloc(new_array_size * sizeof(void *),
343 GFP_KERNEL);
344 if (!new_pages) {
345 fw_load_abort(fw_priv);
346 return -ENOMEM;
347 }
348 memcpy(new_pages, fw_priv->pages,
349 fw_priv->page_array_size * sizeof(void *));
350 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
351 (new_array_size - fw_priv->page_array_size));
352 kfree(fw_priv->pages);
353 fw_priv->pages = new_pages;
354 fw_priv->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700356
357 while (fw_priv->nr_pages < pages_needed) {
358 fw_priv->pages[fw_priv->nr_pages] =
359 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
360
361 if (!fw_priv->pages[fw_priv->nr_pages]) {
362 fw_load_abort(fw_priv);
363 return -ENOMEM;
364 }
365 fw_priv->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return 0;
368}
369
370/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800371 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700372 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700373 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700374 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800375 * @buffer: buffer being written
376 * @offset: buffer offset for write in total data store area
377 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800379 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 * the driver as a firmware image.
381 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700382static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
383 struct bin_attribute *bin_attr,
384 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700386 struct device *dev = to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700387 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 struct firmware *fw;
389 ssize_t retval;
390
391 if (!capable(CAP_SYS_RAWIO))
392 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700393
Laura Garciacad1e552006-05-23 23:22:38 +0200394 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700396 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 retval = -ENODEV;
398 goto out;
399 }
400 retval = fw_realloc_buffer(fw_priv, offset + count);
401 if (retval)
402 goto out;
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700405
406 while (count) {
407 void *page_data;
408 int page_nr = offset >> PAGE_SHIFT;
409 int page_ofs = offset & (PAGE_SIZE - 1);
410 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
411
412 page_data = kmap(fw_priv->pages[page_nr]);
413
414 memcpy(page_data + page_ofs, buffer, page_cnt);
415
416 kunmap(fw_priv->pages[page_nr]);
417 buffer += page_cnt;
418 offset += page_cnt;
419 count -= page_cnt;
420 }
421
422 fw->size = max_t(size_t, offset, fw->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423out:
Laura Garciacad1e552006-05-23 23:22:38 +0200424 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return retval;
426}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800427
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700428static struct bin_attribute firmware_attr_data = {
429 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 .size = 0,
431 .read = firmware_data_read,
432 .write = firmware_data_write,
433};
434
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700435static void firmware_class_timeout(u_long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 fw_load_abort(fw_priv);
440}
441
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700442static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200443fw_create_instance(struct firmware *firmware, const char *fw_name,
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700444 struct device *device, bool uevent, bool nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700446 struct firmware_priv *fw_priv;
447 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700449 fw_priv = kzalloc(sizeof(*fw_priv) + strlen(fw_name) + 1 , GFP_KERNEL);
450 if (!fw_priv) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700451 dev_err(device, "%s: kmalloc failed\n", __func__);
Stephen Boyddddb5542012-03-28 23:30:43 +0200452 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Stephen Boyddddb5542012-03-28 23:30:43 +0200455 fw_priv->fw = firmware;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700456 fw_priv->nowait = nowait;
Dmitry Torokhove1771232010-03-13 23:49:23 -0800457 strcpy(fw_priv->fw_id, fw_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 init_completion(&fw_priv->completion);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700459 setup_timer(&fw_priv->timeout,
460 firmware_class_timeout, (u_long) fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700462 f_dev = &fw_priv->dev;
463
464 device_initialize(f_dev);
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700465 dev_set_name(f_dev, "%s", dev_name(device));
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700466 f_dev->parent = device;
467 f_dev->class = &firmware_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700469 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
Stephen Boyddddb5542012-03-28 23:30:43 +0200472static struct firmware_priv *
473_request_firmware_prepare(const struct firmware **firmware_p, const char *name,
474 struct device *device, bool uevent, bool nowait)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700475{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 struct firmware *firmware;
Stephen Boyddddb5542012-03-28 23:30:43 +0200477 struct firmware_priv *fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 if (!firmware_p)
Stephen Boyddddb5542012-03-28 23:30:43 +0200480 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Jiri Slaby4aed0642005-09-13 01:25:01 -0700482 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700484 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
485 __func__);
Stephen Boyddddb5542012-03-28 23:30:43 +0200486 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800489 if (fw_get_builtin_firmware(firmware, name)) {
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100490 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
Stephen Boyddddb5542012-03-28 23:30:43 +0200491 return NULL;
David Woodhouse5658c762008-05-23 13:52:42 +0100492 }
493
Stephen Boyddddb5542012-03-28 23:30:43 +0200494 fw_priv = fw_create_instance(firmware, name, device, uevent, nowait);
495 if (IS_ERR(fw_priv)) {
496 release_firmware(firmware);
497 *firmware_p = NULL;
498 }
499 return fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200500}
501
502static void _request_firmware_cleanup(const struct firmware **firmware_p)
503{
504 release_firmware(*firmware_p);
505 *firmware_p = NULL;
506}
507
Stephen Boyddddb5542012-03-28 23:30:43 +0200508static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
509 long timeout)
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200510{
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200511 int retval = 0;
Stephen Boyddddb5542012-03-28 23:30:43 +0200512 struct device *f_dev = &fw_priv->dev;
Linus Torvaldscaca9512011-08-24 15:55:30 -0700513
Stephen Boyddddb5542012-03-28 23:30:43 +0200514 dev_set_uevent_suppress(f_dev, true);
David Woodhouse5658c762008-05-23 13:52:42 +0100515
Stephen Boyddddb5542012-03-28 23:30:43 +0200516 /* Need to pin this module until class device is destroyed */
517 __module_get(THIS_MODULE);
518
519 retval = device_add(f_dev);
520 if (retval) {
521 dev_err(f_dev, "%s: device_register failed\n", __func__);
522 goto err_put_dev;
523 }
524
525 retval = device_create_bin_file(f_dev, &firmware_attr_data);
526 if (retval) {
527 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
528 goto err_del_dev;
529 }
530
531 retval = device_create_file(f_dev, &dev_attr_loading);
532 if (retval) {
533 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
534 goto err_del_bin_attr;
535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Kay Sievers312c0042005-11-16 09:00:00 +0100537 if (uevent) {
Stephen Boyddddb5542012-03-28 23:30:43 +0200538 dev_set_uevent_suppress(f_dev, false);
539 dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_id);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200540 if (timeout != MAX_SCHEDULE_TIMEOUT)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700541 mod_timer(&fw_priv->timeout,
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200542 round_jiffies_up(jiffies + timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700544 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
545 }
546
547 wait_for_completion(&fw_priv->completion);
548
549 set_bit(FW_STATUS_DONE, &fw_priv->status);
550 del_timer_sync(&fw_priv->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Laura Garciacad1e552006-05-23 23:22:38 +0200552 mutex_lock(&fw_lock);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700553 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 retval = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 fw_priv->fw = NULL;
Laura Garciacad1e552006-05-23 23:22:38 +0200556 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Stephen Boyddddb5542012-03-28 23:30:43 +0200558 device_remove_file(f_dev, &dev_attr_loading);
559err_del_bin_attr:
560 device_remove_bin_file(f_dev, &firmware_attr_data);
561err_del_dev:
562 device_del(f_dev);
563err_put_dev:
564 put_device(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return retval;
566}
567
568/**
Kay Sievers312c0042005-11-16 09:00:00 +0100569 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800570 * @firmware_p: pointer to firmware image
571 * @name: name of firmware file
572 * @device: device for which firmware is being loaded
573 *
574 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700575 * of @name for device @device.
576 *
577 * Should be called from user context where sleeping is allowed.
578 *
Kay Sievers312c0042005-11-16 09:00:00 +0100579 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700580 * should be distinctive enough not to be confused with any other
581 * firmware image for this or any other device.
582 **/
583int
584request_firmware(const struct firmware **firmware_p, const char *name,
585 struct device *device)
586{
Stephen Boyddddb5542012-03-28 23:30:43 +0200587 struct firmware_priv *fw_priv;
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200588 int ret;
589
Stephen Boyddddb5542012-03-28 23:30:43 +0200590 fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
591 false);
592 if (IS_ERR_OR_NULL(fw_priv))
593 return PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200594
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200595 ret = usermodehelper_read_trylock();
596 if (WARN_ON(ret)) {
597 dev_err(device, "firmware: %s will not be loaded\n", name);
598 } else {
Stephen Boyddddb5542012-03-28 23:30:43 +0200599 ret = _request_firmware_load(fw_priv, true,
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200600 firmware_loading_timeout());
601 usermodehelper_read_unlock();
602 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200603 if (ret)
604 _request_firmware_cleanup(firmware_p);
605
606 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700607}
608
609/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800611 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800613void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800616 if (!fw_is_builtin_firmware(fw))
617 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 kfree(fw);
619 }
620}
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622/* Async support */
623struct firmware_work {
624 struct work_struct work;
625 struct module *module;
626 const char *name;
627 struct device *device;
628 void *context;
629 void (*cont)(const struct firmware *fw, void *context);
Bob Liu072fc8f2011-01-26 18:33:32 +0800630 bool uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631};
632
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700633static int request_firmware_work_func(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
635 struct firmware_work *fw_work = arg;
636 const struct firmware *fw;
Stephen Boyddddb5542012-03-28 23:30:43 +0200637 struct firmware_priv *fw_priv;
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200638 long timeout;
matthieu castet113fab12005-11-13 16:07:39 -0800639 int ret;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (!arg) {
642 WARN_ON(1);
643 return 0;
644 }
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100645
Stephen Boyddddb5542012-03-28 23:30:43 +0200646 fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
647 fw_work->uevent, true);
648 if (IS_ERR_OR_NULL(fw_priv)) {
649 ret = PTR_RET(fw_priv);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200650 goto out;
Stephen Boyddddb5542012-03-28 23:30:43 +0200651 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200652
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200653 timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
654 if (timeout) {
Stephen Boyddddb5542012-03-28 23:30:43 +0200655 ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200656 usermodehelper_read_unlock();
657 } else {
658 dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
659 fw_work->name);
660 ret = -EAGAIN;
661 }
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200662 if (ret)
663 _request_firmware_cleanup(&fw);
664
665 out:
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100666 fw_work->cont(fw, fw_work->context);
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 module_put(fw_work->module);
669 kfree(fw_work);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700670
matthieu castet113fab12005-11-13 16:07:39 -0800671 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
674/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000675 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800676 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +0100677 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800678 * is non-zero else the firmware copy must be done manually.
679 * @name: name of firmware file
680 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100681 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800682 * @context: will be passed over to @cont, and
683 * @fw may be %NULL if firmware request fails.
684 * @cont: function will be called asynchronously when the firmware
685 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 *
Ming Lei7fcab092009-05-29 11:33:19 +0800687 * Asynchronous variant of request_firmware() for user contexts where
688 * it is not possible to sleep for long time. It can't be called
689 * in atomic contexts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 **/
691int
692request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +0800693 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100694 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 void (*cont)(const struct firmware *fw, void *context))
696{
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700697 struct task_struct *task;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700698 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700700 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (!fw_work)
702 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700703
704 fw_work->module = module;
705 fw_work->name = name;
706 fw_work->device = device;
707 fw_work->context = context;
708 fw_work->cont = cont;
709 fw_work->uevent = uevent;
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (!try_module_get(module)) {
712 kfree(fw_work);
713 return -EFAULT;
714 }
715
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700716 task = kthread_run(request_firmware_work_func, fw_work,
717 "firmware/%s", name);
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700718 if (IS_ERR(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 fw_work->cont(NULL, fw_work->context);
matthieu castet113fab12005-11-13 16:07:39 -0800720 module_put(fw_work->module);
721 kfree(fw_work);
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700722 return PTR_ERR(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return 0;
726}
727
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800728static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800730 return class_register(&firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800732
733static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
735 class_unregister(&firmware_class);
736}
737
Shaohua Lia30a6a22006-09-27 01:50:52 -0700738fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739module_exit(firmware_class_exit);
740
741EXPORT_SYMBOL(release_firmware);
742EXPORT_SYMBOL(request_firmware);
743EXPORT_SYMBOL(request_firmware_nowait);