blob: 60290671f04a92a8cc05085832694b8d0a47274a [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
84/* fw_lock could be moved to 'struct firmware_priv' but since it is just
85 * guarding for corner cases a global lock should be OK */
Laura Garciacad1e552006-05-23 23:22:38 +020086static DEFINE_MUTEX(fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88struct firmware_priv {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 struct completion completion;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 struct firmware *fw;
91 unsigned long status;
David Woodhouse6e03a202009-04-09 22:04:07 -070092 struct page **pages;
93 int nr_pages;
94 int page_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 struct timer_list timeout;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -070096 struct device dev;
Johannes Berge9045f92010-03-29 17:57:20 +020097 bool nowait;
Dmitry Torokhove1771232010-03-13 23:49:23 -080098 char fw_id[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700101static struct firmware_priv *to_firmware_priv(struct device *dev)
102{
103 return container_of(dev, struct firmware_priv, dev);
104}
105
106static void fw_load_abort(struct firmware_priv *fw_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 set_bit(FW_STATUS_ABORT, &fw_priv->status);
109 wmb();
110 complete(&fw_priv->completion);
111}
112
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700113static ssize_t firmware_timeout_show(struct class *class,
114 struct class_attribute *attr,
115 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 return sprintf(buf, "%d\n", loading_timeout);
118}
119
120/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800121 * firmware_timeout_store - set number of seconds to wait for firmware
122 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800123 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800124 * @buf: buffer to scan for timeout value
125 * @count: number of bytes in @buf
126 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800128 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 * firmware will be provided.
130 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800131 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700133static ssize_t firmware_timeout_store(struct class *class,
134 struct class_attribute *attr,
135 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700138 if (loading_timeout < 0)
139 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return count;
142}
143
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800144static struct class_attribute firmware_class_attrs[] = {
145 __ATTR(timeout, S_IWUSR | S_IRUGO,
146 firmware_timeout_show, firmware_timeout_store),
147 __ATTR_NULL
148};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800150static void fw_dev_release(struct device *dev)
151{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700152 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800153 int i;
154
155 for (i = 0; i < fw_priv->nr_pages; i++)
156 __free_page(fw_priv->pages[i]);
157 kfree(fw_priv->pages);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800158 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800159
160 module_put(THIS_MODULE);
161}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Kay Sievers7eff2e72007-08-14 15:15:12 +0200163static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700165 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Kay Sievers7eff2e72007-08-14 15:15:12 +0200167 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200169 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700170 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200171 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
172 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 return 0;
175}
176
Adrian Bunk1b81d662006-05-20 15:00:16 -0700177static struct class firmware_class = {
178 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800179 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700180 .dev_uevent = firmware_uevent,
181 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700182};
183
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700184static ssize_t firmware_loading_show(struct device *dev,
185 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700187 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return sprintf(buf, "%d\n", loading);
191}
192
David Woodhousedd336c52010-05-02 11:21:21 +0300193static void firmware_free_data(const struct firmware *fw)
194{
195 int i;
196 vunmap(fw->data);
197 if (fw->pages) {
198 for (i = 0; i < PFN_UP(fw->size); i++)
199 __free_page(fw->pages[i]);
200 kfree(fw->pages);
201 }
202}
203
David Woodhouse6e03a202009-04-09 22:04:07 -0700204/* Some architectures don't have PAGE_KERNEL_RO */
205#ifndef PAGE_KERNEL_RO
206#define PAGE_KERNEL_RO PAGE_KERNEL
207#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800209 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700210 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800211 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800212 * @buf: buffer to scan for loading control value
213 * @count: number of bytes in @buf
214 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * The relevant values are:
216 *
217 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800218 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * -1: Conclude the load with an error and discard any written data.
220 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700221static ssize_t firmware_loading_store(struct device *dev,
222 struct device_attribute *attr,
223 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700225 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700227 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Neil Hormaneea915b2012-01-02 15:31:23 -0500229 mutex_lock(&fw_lock);
230
231 if (!fw_priv->fw)
232 goto out;
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 switch (loading) {
235 case 1:
David Woodhousedd336c52010-05-02 11:21:21 +0300236 firmware_free_data(fw_priv->fw);
237 memset(fw_priv->fw, 0, sizeof(struct firmware));
238 /* If the pages are not owned by 'struct firmware' */
David Woodhouse6e03a202009-04-09 22:04:07 -0700239 for (i = 0; i < fw_priv->nr_pages; i++)
240 __free_page(fw_priv->pages[i]);
241 kfree(fw_priv->pages);
242 fw_priv->pages = NULL;
243 fw_priv->page_array_size = 0;
244 fw_priv->nr_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 set_bit(FW_STATUS_LOADING, &fw_priv->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 break;
247 case 0:
248 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
David Woodhousedd336c52010-05-02 11:21:21 +0300249 vunmap(fw_priv->fw->data);
David Woodhouse6e03a202009-04-09 22:04:07 -0700250 fw_priv->fw->data = vmap(fw_priv->pages,
251 fw_priv->nr_pages,
252 0, PAGE_KERNEL_RO);
253 if (!fw_priv->fw->data) {
254 dev_err(dev, "%s: vmap() failed\n", __func__);
255 goto err;
256 }
David Woodhousedd336c52010-05-02 11:21:21 +0300257 /* Pages are now owned by 'struct firmware' */
258 fw_priv->fw->pages = fw_priv->pages;
259 fw_priv->pages = NULL;
260
David Woodhouse6e03a202009-04-09 22:04:07 -0700261 fw_priv->page_array_size = 0;
262 fw_priv->nr_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 complete(&fw_priv->completion);
264 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
265 break;
266 }
267 /* fallthrough */
268 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700269 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 /* fallthrough */
271 case -1:
David Woodhouse6e03a202009-04-09 22:04:07 -0700272 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 fw_load_abort(fw_priv);
274 break;
275 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500276out:
277 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return count;
279}
280
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700281static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700283static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
284 struct bin_attribute *bin_attr,
285 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700287 struct device *dev = to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700288 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 struct firmware *fw;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700290 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Laura Garciacad1e552006-05-23 23:22:38 +0200292 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700294 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 ret_count = -ENODEV;
296 goto out;
297 }
Jiri Slaby308975f2009-06-21 23:57:31 +0200298 if (offset > fw->size) {
299 ret_count = 0;
300 goto out;
301 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700302 if (count > fw->size - offset)
303 count = fw->size - offset;
304
305 ret_count = count;
306
307 while (count) {
308 void *page_data;
309 int page_nr = offset >> PAGE_SHIFT;
310 int page_ofs = offset & (PAGE_SIZE-1);
311 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
312
313 page_data = kmap(fw_priv->pages[page_nr]);
314
315 memcpy(buffer, page_data + page_ofs, page_cnt);
316
317 kunmap(fw_priv->pages[page_nr]);
318 buffer += page_cnt;
319 offset += page_cnt;
320 count -= page_cnt;
321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322out:
Laura Garciacad1e552006-05-23 23:22:38 +0200323 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return ret_count;
325}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800326
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700327static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
David Woodhouse6e03a202009-04-09 22:04:07 -0700329 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
David Woodhouse6e03a202009-04-09 22:04:07 -0700331 /* If the array of pages is too small, grow it... */
332 if (fw_priv->page_array_size < pages_needed) {
333 int new_array_size = max(pages_needed,
334 fw_priv->page_array_size * 2);
335 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
David Woodhouse6e03a202009-04-09 22:04:07 -0700337 new_pages = kmalloc(new_array_size * sizeof(void *),
338 GFP_KERNEL);
339 if (!new_pages) {
340 fw_load_abort(fw_priv);
341 return -ENOMEM;
342 }
343 memcpy(new_pages, fw_priv->pages,
344 fw_priv->page_array_size * sizeof(void *));
345 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
346 (new_array_size - fw_priv->page_array_size));
347 kfree(fw_priv->pages);
348 fw_priv->pages = new_pages;
349 fw_priv->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700351
352 while (fw_priv->nr_pages < pages_needed) {
353 fw_priv->pages[fw_priv->nr_pages] =
354 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
355
356 if (!fw_priv->pages[fw_priv->nr_pages]) {
357 fw_load_abort(fw_priv);
358 return -ENOMEM;
359 }
360 fw_priv->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return 0;
363}
364
365/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800366 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700367 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700368 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700369 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800370 * @buffer: buffer being written
371 * @offset: buffer offset for write in total data store area
372 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800374 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 * the driver as a firmware image.
376 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700377static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
378 struct bin_attribute *bin_attr,
379 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700381 struct device *dev = to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700382 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 struct firmware *fw;
384 ssize_t retval;
385
386 if (!capable(CAP_SYS_RAWIO))
387 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700388
Laura Garciacad1e552006-05-23 23:22:38 +0200389 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700391 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 retval = -ENODEV;
393 goto out;
394 }
395 retval = fw_realloc_buffer(fw_priv, offset + count);
396 if (retval)
397 goto out;
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700400
401 while (count) {
402 void *page_data;
403 int page_nr = offset >> PAGE_SHIFT;
404 int page_ofs = offset & (PAGE_SIZE - 1);
405 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
406
407 page_data = kmap(fw_priv->pages[page_nr]);
408
409 memcpy(page_data + page_ofs, buffer, page_cnt);
410
411 kunmap(fw_priv->pages[page_nr]);
412 buffer += page_cnt;
413 offset += page_cnt;
414 count -= page_cnt;
415 }
416
417 fw->size = max_t(size_t, offset, fw->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418out:
Laura Garciacad1e552006-05-23 23:22:38 +0200419 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return retval;
421}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800422
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700423static struct bin_attribute firmware_attr_data = {
424 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 .size = 0,
426 .read = firmware_data_read,
427 .write = firmware_data_write,
428};
429
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700430static void firmware_class_timeout(u_long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 fw_load_abort(fw_priv);
435}
436
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700437static struct firmware_priv *
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200438fw_create_instance(const struct firmware *firmware, const char *fw_name,
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700439 struct device *device, bool uevent, bool nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700441 struct firmware_priv *fw_priv;
442 struct device *f_dev;
443 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700445 fw_priv = kzalloc(sizeof(*fw_priv) + strlen(fw_name) + 1 , GFP_KERNEL);
446 if (!fw_priv) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700447 dev_err(device, "%s: kmalloc failed\n", __func__);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700448 error = -ENOMEM;
449 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200452 fw_priv->fw = (struct firmware *)firmware;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700453 fw_priv->nowait = nowait;
Dmitry Torokhove1771232010-03-13 23:49:23 -0800454 strcpy(fw_priv->fw_id, fw_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 init_completion(&fw_priv->completion);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700456 setup_timer(&fw_priv->timeout,
457 firmware_class_timeout, (u_long) fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700459 f_dev = &fw_priv->dev;
460
461 device_initialize(f_dev);
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700462 dev_set_name(f_dev, "%s", dev_name(device));
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700463 f_dev->parent = device;
464 f_dev->class = &firmware_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700466 dev_set_uevent_suppress(f_dev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 /* Need to pin this module until class device is destroyed */
469 __module_get(THIS_MODULE);
470
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700471 error = device_add(f_dev);
472 if (error) {
473 dev_err(device, "%s: device_register failed\n", __func__);
474 goto err_put_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700477 error = device_create_bin_file(f_dev, &firmware_attr_data);
478 if (error) {
479 dev_err(device, "%s: sysfs_create_bin_file failed\n", __func__);
480 goto err_del_dev;
481 }
482
483 error = device_create_file(f_dev, &dev_attr_loading);
484 if (error) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700485 dev_err(device, "%s: device_create_file failed\n", __func__);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700486 goto err_del_bin_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488
Kay Sievers312c0042005-11-16 09:00:00 +0100489 if (uevent)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700490 dev_set_uevent_suppress(f_dev, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700492 return fw_priv;
493
494err_del_bin_attr:
495 device_remove_bin_file(f_dev, &firmware_attr_data);
496err_del_dev:
497 device_del(f_dev);
498err_put_dev:
499 put_device(f_dev);
500err_out:
501 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700504static void fw_destroy_instance(struct firmware_priv *fw_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700506 struct device *f_dev = &fw_priv->dev;
507
508 device_remove_file(f_dev, &dev_attr_loading);
509 device_remove_bin_file(f_dev, &firmware_attr_data);
510 device_unregister(f_dev);
511}
512
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200513static int _request_firmware_prepare(const struct firmware **firmware_p,
514 const char *name, struct device *device)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700515{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 struct firmware *firmware;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 if (!firmware_p)
519 return -EINVAL;
520
Jiri Slaby4aed0642005-09-13 01:25:01 -0700521 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700523 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
524 __func__);
Tetsuo Handae4c89a52012-01-23 21:59:08 +0100525 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800528 if (fw_get_builtin_firmware(firmware, name)) {
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100529 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
David Woodhouse5658c762008-05-23 13:52:42 +0100530 return 0;
531 }
532
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200533 return 1;
534}
535
536static void _request_firmware_cleanup(const struct firmware **firmware_p)
537{
538 release_firmware(*firmware_p);
539 *firmware_p = NULL;
540}
541
542static int _request_firmware(const struct firmware *firmware,
543 const char *name, struct device *device,
544 bool uevent, bool nowait)
545{
546 struct firmware_priv *fw_priv;
547 int retval;
548
Rafael J. Wysockife2e39d2012-03-28 23:29:45 +0200549 retval = usermodehelper_read_trylock();
550 if (WARN_ON(retval)) {
Linus Torvaldscaca9512011-08-24 15:55:30 -0700551 dev_err(device, "firmware: %s will not be loaded\n", name);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200552 return retval;
Linus Torvaldscaca9512011-08-24 15:55:30 -0700553 }
554
David Woodhouse5658c762008-05-23 13:52:42 +0100555 if (uevent)
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100556 dev_dbg(device, "firmware: requesting %s\n", name);
David Woodhouse5658c762008-05-23 13:52:42 +0100557
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700558 fw_priv = fw_create_instance(firmware, name, device, uevent, nowait);
559 if (IS_ERR(fw_priv)) {
560 retval = PTR_ERR(fw_priv);
561 goto out;
562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Kay Sievers312c0042005-11-16 09:00:00 +0100564 if (uevent) {
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700565 if (loading_timeout > 0)
566 mod_timer(&fw_priv->timeout,
567 round_jiffies_up(jiffies +
568 loading_timeout * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700570 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
571 }
572
573 wait_for_completion(&fw_priv->completion);
574
575 set_bit(FW_STATUS_DONE, &fw_priv->status);
576 del_timer_sync(&fw_priv->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Laura Garciacad1e552006-05-23 23:22:38 +0200578 mutex_lock(&fw_lock);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700579 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 retval = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 fw_priv->fw = NULL;
Laura Garciacad1e552006-05-23 23:22:38 +0200582 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700584 fw_destroy_instance(fw_priv);
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586out:
Rafael J. Wysockife2e39d2012-03-28 23:29:45 +0200587 usermodehelper_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return retval;
589}
590
591/**
Kay Sievers312c0042005-11-16 09:00:00 +0100592 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800593 * @firmware_p: pointer to firmware image
594 * @name: name of firmware file
595 * @device: device for which firmware is being loaded
596 *
597 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700598 * of @name for device @device.
599 *
600 * Should be called from user context where sleeping is allowed.
601 *
Kay Sievers312c0042005-11-16 09:00:00 +0100602 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700603 * should be distinctive enough not to be confused with any other
604 * firmware image for this or any other device.
605 **/
606int
607request_firmware(const struct firmware **firmware_p, const char *name,
608 struct device *device)
609{
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200610 int ret;
611
612 ret = _request_firmware_prepare(firmware_p, name, device);
613 if (ret <= 0)
614 return ret;
615
616 ret = _request_firmware(*firmware_p, name, device, true, false);
617 if (ret)
618 _request_firmware_cleanup(firmware_p);
619
620 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700621}
622
623/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800625 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800627void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800630 if (!fw_is_builtin_firmware(fw))
631 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 kfree(fw);
633 }
634}
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636/* Async support */
637struct firmware_work {
638 struct work_struct work;
639 struct module *module;
640 const char *name;
641 struct device *device;
642 void *context;
643 void (*cont)(const struct firmware *fw, void *context);
Bob Liu072fc8f2011-01-26 18:33:32 +0800644 bool uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645};
646
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700647static int request_firmware_work_func(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 struct firmware_work *fw_work = arg;
650 const struct firmware *fw;
matthieu castet113fab12005-11-13 16:07:39 -0800651 int ret;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (!arg) {
654 WARN_ON(1);
655 return 0;
656 }
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100657
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200658 ret = _request_firmware_prepare(&fw, fw_work->name, fw_work->device);
659 if (ret <= 0)
660 goto out;
661
662 ret = _request_firmware(fw, fw_work->name, fw_work->device,
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700663 fw_work->uevent, true);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200664 if (ret)
665 _request_firmware_cleanup(&fw);
666
667 out:
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100668 fw_work->cont(fw, fw_work->context);
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 module_put(fw_work->module);
671 kfree(fw_work);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700672
matthieu castet113fab12005-11-13 16:07:39 -0800673 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
676/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000677 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800678 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +0100679 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800680 * is non-zero else the firmware copy must be done manually.
681 * @name: name of firmware file
682 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100683 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800684 * @context: will be passed over to @cont, and
685 * @fw may be %NULL if firmware request fails.
686 * @cont: function will be called asynchronously when the firmware
687 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 *
Ming Lei7fcab092009-05-29 11:33:19 +0800689 * Asynchronous variant of request_firmware() for user contexts where
690 * it is not possible to sleep for long time. It can't be called
691 * in atomic contexts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 **/
693int
694request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +0800695 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100696 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 void (*cont)(const struct firmware *fw, void *context))
698{
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700699 struct task_struct *task;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700700 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700702 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (!fw_work)
704 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700705
706 fw_work->module = module;
707 fw_work->name = name;
708 fw_work->device = device;
709 fw_work->context = context;
710 fw_work->cont = cont;
711 fw_work->uevent = uevent;
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (!try_module_get(module)) {
714 kfree(fw_work);
715 return -EFAULT;
716 }
717
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700718 task = kthread_run(request_firmware_work_func, fw_work,
719 "firmware/%s", name);
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700720 if (IS_ERR(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 fw_work->cont(NULL, fw_work->context);
matthieu castet113fab12005-11-13 16:07:39 -0800722 module_put(fw_work->module);
723 kfree(fw_work);
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700724 return PTR_ERR(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700726
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);