blob: 8e6c62b4f512539e96f8ba0a069e53561f2dc4d8 [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
30enum {
31 FW_STATUS_LOADING,
32 FW_STATUS_DONE,
33 FW_STATUS_ABORT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070034};
35
Dave Jones2f651682007-01-25 15:56:15 -050036static int loading_timeout = 60; /* In seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/* fw_lock could be moved to 'struct firmware_priv' but since it is just
39 * guarding for corner cases a global lock should be OK */
Laura Garciacad1e552006-05-23 23:22:38 +020040static DEFINE_MUTEX(fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42struct firmware_priv {
Samuel Ortiz976821d2009-05-27 00:49:31 +020043 char *fw_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 struct completion completion;
45 struct bin_attribute attr_data;
46 struct firmware *fw;
47 unsigned long status;
David Woodhouse6e03a202009-04-09 22:04:07 -070048 struct page **pages;
49 int nr_pages;
50 int page_array_size;
51 const char *vdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 struct timer_list timeout;
53};
54
David Woodhouse5658c762008-05-23 13:52:42 +010055#ifdef CONFIG_FW_LOADER
56extern struct builtin_fw __start_builtin_fw[];
57extern struct builtin_fw __end_builtin_fw[];
58#else /* Module case. Avoid ifdefs later; it'll all optimise out */
59static struct builtin_fw *__start_builtin_fw;
60static struct builtin_fw *__end_builtin_fw;
61#endif
62
Arjan van de Ven858119e2006-01-14 13:20:43 -080063static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070064fw_load_abort(struct firmware_priv *fw_priv)
65{
66 set_bit(FW_STATUS_ABORT, &fw_priv->status);
67 wmb();
68 complete(&fw_priv->completion);
69}
70
71static ssize_t
Andi Kleen28812fe2010-01-05 12:48:07 +010072firmware_timeout_show(struct class *class,
73 struct class_attribute *attr,
74 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 return sprintf(buf, "%d\n", loading_timeout);
77}
78
79/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -080080 * firmware_timeout_store - set number of seconds to wait for firmware
81 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -080082 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -080083 * @buf: buffer to scan for timeout value
84 * @count: number of bytes in @buf
85 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -080087 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * firmware will be provided.
89 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -080090 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 **/
92static ssize_t
Andi Kleen28812fe2010-01-05 12:48:07 +010093firmware_timeout_store(struct class *class,
94 struct class_attribute *attr,
95 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -070098 if (loading_timeout < 0)
99 loading_timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return count;
101}
102
103static CLASS_ATTR(timeout, 0644, firmware_timeout_show, firmware_timeout_store);
104
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700105static void fw_dev_release(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Kay Sievers7eff2e72007-08-14 15:15:12 +0200107static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700109 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Kay Sievers7eff2e72007-08-14 15:15:12 +0200111 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200113 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700114 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 return 0;
117}
118
Adrian Bunk1b81d662006-05-20 15:00:16 -0700119static struct class firmware_class = {
120 .name = "firmware",
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700121 .dev_uevent = firmware_uevent,
122 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700123};
124
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700125static ssize_t firmware_loading_show(struct device *dev,
126 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700128 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
130 return sprintf(buf, "%d\n", loading);
131}
132
David Woodhousedd336c52010-05-02 11:21:21 +0300133static void firmware_free_data(const struct firmware *fw)
134{
135 int i;
136 vunmap(fw->data);
137 if (fw->pages) {
138 for (i = 0; i < PFN_UP(fw->size); i++)
139 __free_page(fw->pages[i]);
140 kfree(fw->pages);
141 }
142}
143
David Woodhouse6e03a202009-04-09 22:04:07 -0700144/* Some architectures don't have PAGE_KERNEL_RO */
145#ifndef PAGE_KERNEL_RO
146#define PAGE_KERNEL_RO PAGE_KERNEL
147#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800149 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700150 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800151 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800152 * @buf: buffer to scan for loading control value
153 * @count: number of bytes in @buf
154 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 * The relevant values are:
156 *
157 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800158 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 * -1: Conclude the load with an error and discard any written data.
160 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700161static ssize_t firmware_loading_store(struct device *dev,
162 struct device_attribute *attr,
163 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700165 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700167 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 switch (loading) {
170 case 1:
Laura Garciacad1e552006-05-23 23:22:38 +0200171 mutex_lock(&fw_lock);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700172 if (!fw_priv->fw) {
Laura Garciacad1e552006-05-23 23:22:38 +0200173 mutex_unlock(&fw_lock);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700174 break;
175 }
David Woodhousedd336c52010-05-02 11:21:21 +0300176 firmware_free_data(fw_priv->fw);
177 memset(fw_priv->fw, 0, sizeof(struct firmware));
178 /* If the pages are not owned by 'struct firmware' */
David Woodhouse6e03a202009-04-09 22:04:07 -0700179 for (i = 0; i < fw_priv->nr_pages; i++)
180 __free_page(fw_priv->pages[i]);
181 kfree(fw_priv->pages);
182 fw_priv->pages = NULL;
183 fw_priv->page_array_size = 0;
184 fw_priv->nr_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 set_bit(FW_STATUS_LOADING, &fw_priv->status);
Laura Garciacad1e552006-05-23 23:22:38 +0200186 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 break;
188 case 0:
189 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
David Woodhousedd336c52010-05-02 11:21:21 +0300190 vunmap(fw_priv->fw->data);
David Woodhouse6e03a202009-04-09 22:04:07 -0700191 fw_priv->fw->data = vmap(fw_priv->pages,
192 fw_priv->nr_pages,
193 0, PAGE_KERNEL_RO);
194 if (!fw_priv->fw->data) {
195 dev_err(dev, "%s: vmap() failed\n", __func__);
196 goto err;
197 }
David Woodhousedd336c52010-05-02 11:21:21 +0300198 /* Pages are now owned by 'struct firmware' */
199 fw_priv->fw->pages = fw_priv->pages;
200 fw_priv->pages = NULL;
201
David Woodhouse6e03a202009-04-09 22:04:07 -0700202 fw_priv->page_array_size = 0;
203 fw_priv->nr_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 complete(&fw_priv->completion);
205 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
206 break;
207 }
208 /* fallthrough */
209 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700210 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /* fallthrough */
212 case -1:
David Woodhouse6e03a202009-04-09 22:04:07 -0700213 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 fw_load_abort(fw_priv);
215 break;
216 }
217
218 return count;
219}
220
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700221static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800224firmware_data_read(struct kobject *kobj, struct bin_attribute *bin_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 char *buffer, loff_t offset, size_t count)
226{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700227 struct device *dev = to_dev(kobj);
228 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 struct firmware *fw;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700230 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Laura Garciacad1e552006-05-23 23:22:38 +0200232 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700234 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 ret_count = -ENODEV;
236 goto out;
237 }
Jiri Slaby308975f2009-06-21 23:57:31 +0200238 if (offset > fw->size) {
239 ret_count = 0;
240 goto out;
241 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700242 if (count > fw->size - offset)
243 count = fw->size - offset;
244
245 ret_count = count;
246
247 while (count) {
248 void *page_data;
249 int page_nr = offset >> PAGE_SHIFT;
250 int page_ofs = offset & (PAGE_SIZE-1);
251 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
252
253 page_data = kmap(fw_priv->pages[page_nr]);
254
255 memcpy(buffer, page_data + page_ofs, page_cnt);
256
257 kunmap(fw_priv->pages[page_nr]);
258 buffer += page_cnt;
259 offset += page_cnt;
260 count -= page_cnt;
261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262out:
Laura Garciacad1e552006-05-23 23:22:38 +0200263 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return ret_count;
265}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267static int
268fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
269{
David Woodhouse6e03a202009-04-09 22:04:07 -0700270 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
David Woodhouse6e03a202009-04-09 22:04:07 -0700272 /* If the array of pages is too small, grow it... */
273 if (fw_priv->page_array_size < pages_needed) {
274 int new_array_size = max(pages_needed,
275 fw_priv->page_array_size * 2);
276 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
David Woodhouse6e03a202009-04-09 22:04:07 -0700278 new_pages = kmalloc(new_array_size * sizeof(void *),
279 GFP_KERNEL);
280 if (!new_pages) {
281 fw_load_abort(fw_priv);
282 return -ENOMEM;
283 }
284 memcpy(new_pages, fw_priv->pages,
285 fw_priv->page_array_size * sizeof(void *));
286 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
287 (new_array_size - fw_priv->page_array_size));
288 kfree(fw_priv->pages);
289 fw_priv->pages = new_pages;
290 fw_priv->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700292
293 while (fw_priv->nr_pages < pages_needed) {
294 fw_priv->pages[fw_priv->nr_pages] =
295 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
296
297 if (!fw_priv->pages[fw_priv->nr_pages]) {
298 fw_load_abort(fw_priv);
299 return -ENOMEM;
300 }
301 fw_priv->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return 0;
304}
305
306/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800307 * firmware_data_write - write method for firmware
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700308 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700309 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800310 * @buffer: buffer being written
311 * @offset: buffer offset for write in total data store area
312 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800314 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 * the driver as a firmware image.
316 **/
317static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800318firmware_data_write(struct kobject *kobj, struct bin_attribute *bin_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 char *buffer, loff_t offset, size_t count)
320{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700321 struct device *dev = to_dev(kobj);
322 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 struct firmware *fw;
324 ssize_t retval;
325
326 if (!capable(CAP_SYS_RAWIO))
327 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700328
Laura Garciacad1e552006-05-23 23:22:38 +0200329 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700331 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 retval = -ENODEV;
333 goto out;
334 }
335 retval = fw_realloc_buffer(fw_priv, offset + count);
336 if (retval)
337 goto out;
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700340
341 while (count) {
342 void *page_data;
343 int page_nr = offset >> PAGE_SHIFT;
344 int page_ofs = offset & (PAGE_SIZE - 1);
345 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
346
347 page_data = kmap(fw_priv->pages[page_nr]);
348
349 memcpy(page_data + page_ofs, buffer, page_cnt);
350
351 kunmap(fw_priv->pages[page_nr]);
352 buffer += page_cnt;
353 offset += page_cnt;
354 count -= page_cnt;
355 }
356
357 fw->size = max_t(size_t, offset, fw->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358out:
Laura Garciacad1e552006-05-23 23:22:38 +0200359 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return retval;
361}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363static struct bin_attribute firmware_attr_data_tmpl = {
Tejun Heo7b595752007-06-14 03:45:17 +0900364 .attr = {.name = "data", .mode = 0644},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 .size = 0,
366 .read = firmware_data_read,
367 .write = firmware_data_write,
368};
369
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700370static void fw_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700372 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
David Woodhouse6e03a202009-04-09 22:04:07 -0700373 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
David Woodhouse6e03a202009-04-09 22:04:07 -0700375 for (i = 0; i < fw_priv->nr_pages; i++)
376 __free_page(fw_priv->pages[i]);
377 kfree(fw_priv->pages);
Samuel Ortiz976821d2009-05-27 00:49:31 +0200378 kfree(fw_priv->fw_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 kfree(fw_priv);
Catalin Marinas0f2f2222009-07-08 11:17:40 +0100380 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 module_put(THIS_MODULE);
383}
384
385static void
386firmware_class_timeout(u_long data)
387{
388 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
389 fw_load_abort(fw_priv);
390}
391
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700392static int fw_register_device(struct device **dev_p, const char *fw_name,
393 struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
395 int retval;
Jiri Slaby4aed0642005-09-13 01:25:01 -0700396 struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 GFP_KERNEL);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700398 struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700400 *dev_p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700402 if (!fw_priv || !f_dev) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700403 dev_err(device, "%s: kmalloc failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 retval = -ENOMEM;
405 goto error_kfree;
406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 init_completion(&fw_priv->completion);
409 fw_priv->attr_data = firmware_attr_data_tmpl;
Samuel Ortiz976821d2009-05-27 00:49:31 +0200410 fw_priv->fw_id = kstrdup(fw_name, GFP_KERNEL);
411 if (!fw_priv->fw_id) {
412 dev_err(device, "%s: Firmware name allocation failed\n",
413 __func__);
414 retval = -ENOMEM;
415 goto error_kfree;
416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 fw_priv->timeout.function = firmware_class_timeout;
419 fw_priv->timeout.data = (u_long) fw_priv;
420 init_timer(&fw_priv->timeout);
421
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700422 dev_set_name(f_dev, "%s", dev_name(device));
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700423 f_dev->parent = device;
424 f_dev->class = &firmware_class;
425 dev_set_drvdata(f_dev, fw_priv);
Ming Leif67f1292009-03-01 21:10:49 +0800426 dev_set_uevent_suppress(f_dev, 1);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700427 retval = device_register(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (retval) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700429 dev_err(device, "%s: device_register failed\n", __func__);
Ming Lei6acf70f2009-04-23 22:31:52 +0800430 put_device(f_dev);
Catalin Marinas0f2f2222009-07-08 11:17:40 +0100431 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700433 *dev_p = f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return 0;
435
436error_kfree:
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700437 kfree(f_dev);
Ming Lei6acf70f2009-04-23 22:31:52 +0800438 kfree(fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return retval;
440}
441
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700442static int fw_setup_device(struct firmware *fw, struct device **dev_p,
443 const char *fw_name, struct device *device,
444 int uevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700446 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 struct firmware_priv *fw_priv;
448 int retval;
449
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700450 *dev_p = NULL;
451 retval = fw_register_device(&f_dev, fw_name, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (retval)
453 goto out;
454
455 /* Need to pin this module until class device is destroyed */
456 __module_get(THIS_MODULE);
457
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700458 fw_priv = dev_get_drvdata(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 fw_priv->fw = fw;
Jiri Kosinae1955ca2010-03-09 19:30:28 +0100461 sysfs_bin_attr_init(&fw_priv->attr_data);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700462 retval = sysfs_create_bin_file(&f_dev->kobj, &fw_priv->attr_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (retval) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700464 dev_err(device, "%s: sysfs_create_bin_file failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 goto error_unreg;
466 }
467
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700468 retval = device_create_file(f_dev, &dev_attr_loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (retval) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700470 dev_err(device, "%s: device_create_file failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 goto error_unreg;
472 }
473
Kay Sievers312c0042005-11-16 09:00:00 +0100474 if (uevent)
Ming Leif67f1292009-03-01 21:10:49 +0800475 dev_set_uevent_suppress(f_dev, 0);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700476 *dev_p = f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 goto out;
478
479error_unreg:
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700480 device_unregister(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481out:
482 return retval;
483}
484
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700485static int
486_request_firmware(const struct firmware **firmware_p, const char *name,
Kay Sievers312c0042005-11-16 09:00:00 +0100487 struct device *device, int uevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700489 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 struct firmware_priv *fw_priv;
491 struct firmware *firmware;
David Woodhouse5658c762008-05-23 13:52:42 +0100492 struct builtin_fw *builtin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 int retval;
494
495 if (!firmware_p)
496 return -EINVAL;
497
Jiri Slaby4aed0642005-09-13 01:25:01 -0700498 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700500 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
501 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 retval = -ENOMEM;
503 goto out;
504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
David Woodhouse5658c762008-05-23 13:52:42 +0100506 for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
507 builtin++) {
508 if (strcmp(name, builtin->name))
509 continue;
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100510 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
David Woodhouse5658c762008-05-23 13:52:42 +0100511 firmware->size = builtin->size;
512 firmware->data = builtin->data;
513 return 0;
514 }
515
516 if (uevent)
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100517 dev_dbg(device, "firmware: requesting %s\n", name);
David Woodhouse5658c762008-05-23 13:52:42 +0100518
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700519 retval = fw_setup_device(firmware, &f_dev, name, device, uevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (retval)
521 goto error_kfree_fw;
522
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700523 fw_priv = dev_get_drvdata(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Kay Sievers312c0042005-11-16 09:00:00 +0100525 if (uevent) {
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700526 if (loading_timeout > 0) {
527 fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
528 add_timer(&fw_priv->timeout);
529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700531 kobject_uevent(&f_dev->kobj, KOBJ_ADD);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700532 wait_for_completion(&fw_priv->completion);
533 set_bit(FW_STATUS_DONE, &fw_priv->status);
534 del_timer_sync(&fw_priv->timeout);
535 } else
536 wait_for_completion(&fw_priv->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Laura Garciacad1e552006-05-23 23:22:38 +0200538 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
540 retval = -ENOENT;
541 release_firmware(fw_priv->fw);
542 *firmware_p = NULL;
543 }
544 fw_priv->fw = NULL;
Laura Garciacad1e552006-05-23 23:22:38 +0200545 mutex_unlock(&fw_lock);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700546 device_unregister(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 goto out;
548
549error_kfree_fw:
550 kfree(firmware);
551 *firmware_p = NULL;
552out:
553 return retval;
554}
555
556/**
Kay Sievers312c0042005-11-16 09:00:00 +0100557 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800558 * @firmware_p: pointer to firmware image
559 * @name: name of firmware file
560 * @device: device for which firmware is being loaded
561 *
562 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700563 * of @name for device @device.
564 *
565 * Should be called from user context where sleeping is allowed.
566 *
Kay Sievers312c0042005-11-16 09:00:00 +0100567 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700568 * should be distinctive enough not to be confused with any other
569 * firmware image for this or any other device.
570 **/
571int
572request_firmware(const struct firmware **firmware_p, const char *name,
573 struct device *device)
574{
Kay Sievers312c0042005-11-16 09:00:00 +0100575 int uevent = 1;
576 return _request_firmware(firmware_p, name, device, uevent);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700577}
578
579/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800581 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 **/
583void
584release_firmware(const struct firmware *fw)
585{
David Woodhouse5658c762008-05-23 13:52:42 +0100586 struct builtin_fw *builtin;
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (fw) {
David Woodhouse5658c762008-05-23 13:52:42 +0100589 for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
590 builtin++) {
591 if (fw->data == builtin->data)
592 goto free_fw;
593 }
David Woodhousedd336c52010-05-02 11:21:21 +0300594 firmware_free_data(fw);
David Woodhouse5658c762008-05-23 13:52:42 +0100595 free_fw:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 kfree(fw);
597 }
598}
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600/* Async support */
601struct firmware_work {
602 struct work_struct work;
603 struct module *module;
604 const char *name;
605 struct device *device;
606 void *context;
607 void (*cont)(const struct firmware *fw, void *context);
Kay Sievers312c0042005-11-16 09:00:00 +0100608 int uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609};
610
611static int
612request_firmware_work_func(void *arg)
613{
614 struct firmware_work *fw_work = arg;
615 const struct firmware *fw;
matthieu castet113fab12005-11-13 16:07:39 -0800616 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (!arg) {
618 WARN_ON(1);
619 return 0;
620 }
matthieu castet113fab12005-11-13 16:07:39 -0800621 ret = _request_firmware(&fw, fw_work->name, fw_work->device,
Kay Sievers312c0042005-11-16 09:00:00 +0100622 fw_work->uevent);
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100623
624 fw_work->cont(fw, fw_work->context);
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 module_put(fw_work->module);
627 kfree(fw_work);
matthieu castet113fab12005-11-13 16:07:39 -0800628 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
631/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000632 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800633 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +0100634 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800635 * is non-zero else the firmware copy must be done manually.
636 * @name: name of firmware file
637 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100638 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800639 * @context: will be passed over to @cont, and
640 * @fw may be %NULL if firmware request fails.
641 * @cont: function will be called asynchronously when the firmware
642 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 *
Ming Lei7fcab092009-05-29 11:33:19 +0800644 * Asynchronous variant of request_firmware() for user contexts where
645 * it is not possible to sleep for long time. It can't be called
646 * in atomic contexts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 **/
648int
649request_firmware_nowait(
Kay Sievers312c0042005-11-16 09:00:00 +0100650 struct module *module, int uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100651 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 void (*cont)(const struct firmware *fw, void *context))
653{
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700654 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100656 gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 if (!fw_work)
659 return -ENOMEM;
660 if (!try_module_get(module)) {
661 kfree(fw_work);
662 return -EFAULT;
663 }
664
665 *fw_work = (struct firmware_work) {
666 .module = module,
667 .name = name,
668 .device = device,
669 .context = context,
670 .cont = cont,
Kay Sievers312c0042005-11-16 09:00:00 +0100671 .uevent = uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 };
673
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700674 task = kthread_run(request_firmware_work_func, fw_work,
675 "firmware/%s", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700677 if (IS_ERR(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 fw_work->cont(NULL, fw_work->context);
matthieu castet113fab12005-11-13 16:07:39 -0800679 module_put(fw_work->module);
680 kfree(fw_work);
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700681 return PTR_ERR(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
683 return 0;
684}
685
686static int __init
687firmware_class_init(void)
688{
689 int error;
690 error = class_register(&firmware_class);
691 if (error) {
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800692 printk(KERN_ERR "%s: class_register failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return error;
694 }
695 error = class_create_file(&firmware_class, &class_attr_timeout);
696 if (error) {
697 printk(KERN_ERR "%s: class_create_file failed\n",
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800698 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 class_unregister(&firmware_class);
700 }
701 return error;
702
703}
704static void __exit
705firmware_class_exit(void)
706{
707 class_unregister(&firmware_class);
708}
709
Shaohua Lia30a6a22006-09-27 01:50:52 -0700710fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711module_exit(firmware_class_exit);
712
713EXPORT_SYMBOL(release_firmware);
714EXPORT_SYMBOL(request_firmware);
715EXPORT_SYMBOL(request_firmware_nowait);