blob: 985da11174e7a139a9269fce26d29ea2f1a0e32b [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 Woodhouse6e03a202009-04-09 22:04:07 -0700133/* Some architectures don't have PAGE_KERNEL_RO */
134#ifndef PAGE_KERNEL_RO
135#define PAGE_KERNEL_RO PAGE_KERNEL
136#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800138 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700139 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800140 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800141 * @buf: buffer to scan for loading control value
142 * @count: number of bytes in @buf
143 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 * The relevant values are:
145 *
146 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800147 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 * -1: Conclude the load with an error and discard any written data.
149 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700150static ssize_t firmware_loading_store(struct device *dev,
151 struct device_attribute *attr,
152 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700154 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700156 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 switch (loading) {
159 case 1:
Laura Garciacad1e552006-05-23 23:22:38 +0200160 mutex_lock(&fw_lock);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700161 if (!fw_priv->fw) {
Laura Garciacad1e552006-05-23 23:22:38 +0200162 mutex_unlock(&fw_lock);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700163 break;
164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 vfree(fw_priv->fw->data);
166 fw_priv->fw->data = NULL;
David Woodhouse6e03a202009-04-09 22:04:07 -0700167 for (i = 0; i < fw_priv->nr_pages; i++)
168 __free_page(fw_priv->pages[i]);
169 kfree(fw_priv->pages);
170 fw_priv->pages = NULL;
171 fw_priv->page_array_size = 0;
172 fw_priv->nr_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 fw_priv->fw->size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 set_bit(FW_STATUS_LOADING, &fw_priv->status);
Laura Garciacad1e552006-05-23 23:22:38 +0200175 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 break;
177 case 0:
178 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700179 vfree(fw_priv->fw->data);
180 fw_priv->fw->data = vmap(fw_priv->pages,
181 fw_priv->nr_pages,
182 0, PAGE_KERNEL_RO);
183 if (!fw_priv->fw->data) {
184 dev_err(dev, "%s: vmap() failed\n", __func__);
185 goto err;
186 }
187 /* Pages will be freed by vfree() */
David Woodhouse6e03a202009-04-09 22:04:07 -0700188 fw_priv->page_array_size = 0;
189 fw_priv->nr_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 complete(&fw_priv->completion);
191 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
192 break;
193 }
194 /* fallthrough */
195 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700196 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 /* fallthrough */
198 case -1:
David Woodhouse6e03a202009-04-09 22:04:07 -0700199 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 fw_load_abort(fw_priv);
201 break;
202 }
203
204 return count;
205}
206
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700207static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800210firmware_data_read(struct kobject *kobj, struct bin_attribute *bin_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 char *buffer, loff_t offset, size_t count)
212{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700213 struct device *dev = to_dev(kobj);
214 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 struct firmware *fw;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700216 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Laura Garciacad1e552006-05-23 23:22:38 +0200218 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700220 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 ret_count = -ENODEV;
222 goto out;
223 }
Jiri Slaby308975f2009-06-21 23:57:31 +0200224 if (offset > fw->size) {
225 ret_count = 0;
226 goto out;
227 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700228 if (count > fw->size - offset)
229 count = fw->size - offset;
230
231 ret_count = count;
232
233 while (count) {
234 void *page_data;
235 int page_nr = offset >> PAGE_SHIFT;
236 int page_ofs = offset & (PAGE_SIZE-1);
237 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
238
239 page_data = kmap(fw_priv->pages[page_nr]);
240
241 memcpy(buffer, page_data + page_ofs, page_cnt);
242
243 kunmap(fw_priv->pages[page_nr]);
244 buffer += page_cnt;
245 offset += page_cnt;
246 count -= page_cnt;
247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248out:
Laura Garciacad1e552006-05-23 23:22:38 +0200249 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return ret_count;
251}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253static int
254fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
255{
David Woodhouse6e03a202009-04-09 22:04:07 -0700256 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
David Woodhouse6e03a202009-04-09 22:04:07 -0700258 /* If the array of pages is too small, grow it... */
259 if (fw_priv->page_array_size < pages_needed) {
260 int new_array_size = max(pages_needed,
261 fw_priv->page_array_size * 2);
262 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
David Woodhouse6e03a202009-04-09 22:04:07 -0700264 new_pages = kmalloc(new_array_size * sizeof(void *),
265 GFP_KERNEL);
266 if (!new_pages) {
267 fw_load_abort(fw_priv);
268 return -ENOMEM;
269 }
270 memcpy(new_pages, fw_priv->pages,
271 fw_priv->page_array_size * sizeof(void *));
272 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
273 (new_array_size - fw_priv->page_array_size));
274 kfree(fw_priv->pages);
275 fw_priv->pages = new_pages;
276 fw_priv->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700278
279 while (fw_priv->nr_pages < pages_needed) {
280 fw_priv->pages[fw_priv->nr_pages] =
281 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
282
283 if (!fw_priv->pages[fw_priv->nr_pages]) {
284 fw_load_abort(fw_priv);
285 return -ENOMEM;
286 }
287 fw_priv->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return 0;
290}
291
292/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800293 * firmware_data_write - write method for firmware
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700294 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700295 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800296 * @buffer: buffer being written
297 * @offset: buffer offset for write in total data store area
298 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800300 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 * the driver as a firmware image.
302 **/
303static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800304firmware_data_write(struct kobject *kobj, struct bin_attribute *bin_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 char *buffer, loff_t offset, size_t count)
306{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700307 struct device *dev = to_dev(kobj);
308 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 struct firmware *fw;
310 ssize_t retval;
311
312 if (!capable(CAP_SYS_RAWIO))
313 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700314
Laura Garciacad1e552006-05-23 23:22:38 +0200315 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700317 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 retval = -ENODEV;
319 goto out;
320 }
321 retval = fw_realloc_buffer(fw_priv, offset + count);
322 if (retval)
323 goto out;
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700326
327 while (count) {
328 void *page_data;
329 int page_nr = offset >> PAGE_SHIFT;
330 int page_ofs = offset & (PAGE_SIZE - 1);
331 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
332
333 page_data = kmap(fw_priv->pages[page_nr]);
334
335 memcpy(page_data + page_ofs, buffer, page_cnt);
336
337 kunmap(fw_priv->pages[page_nr]);
338 buffer += page_cnt;
339 offset += page_cnt;
340 count -= page_cnt;
341 }
342
343 fw->size = max_t(size_t, offset, fw->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344out:
Laura Garciacad1e552006-05-23 23:22:38 +0200345 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return retval;
347}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349static struct bin_attribute firmware_attr_data_tmpl = {
Tejun Heo7b595752007-06-14 03:45:17 +0900350 .attr = {.name = "data", .mode = 0644},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 .size = 0,
352 .read = firmware_data_read,
353 .write = firmware_data_write,
354};
355
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700356static void fw_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700358 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
David Woodhouse6e03a202009-04-09 22:04:07 -0700359 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
David Woodhouse6e03a202009-04-09 22:04:07 -0700361 for (i = 0; i < fw_priv->nr_pages; i++)
362 __free_page(fw_priv->pages[i]);
363 kfree(fw_priv->pages);
Samuel Ortiz976821d2009-05-27 00:49:31 +0200364 kfree(fw_priv->fw_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 kfree(fw_priv);
Catalin Marinas0f2f2222009-07-08 11:17:40 +0100366 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 module_put(THIS_MODULE);
369}
370
371static void
372firmware_class_timeout(u_long data)
373{
374 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
375 fw_load_abort(fw_priv);
376}
377
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700378static int fw_register_device(struct device **dev_p, const char *fw_name,
379 struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 int retval;
Jiri Slaby4aed0642005-09-13 01:25:01 -0700382 struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 GFP_KERNEL);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700384 struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700386 *dev_p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700388 if (!fw_priv || !f_dev) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700389 dev_err(device, "%s: kmalloc failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 retval = -ENOMEM;
391 goto error_kfree;
392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 init_completion(&fw_priv->completion);
395 fw_priv->attr_data = firmware_attr_data_tmpl;
Samuel Ortiz976821d2009-05-27 00:49:31 +0200396 fw_priv->fw_id = kstrdup(fw_name, GFP_KERNEL);
397 if (!fw_priv->fw_id) {
398 dev_err(device, "%s: Firmware name allocation failed\n",
399 __func__);
400 retval = -ENOMEM;
401 goto error_kfree;
402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 fw_priv->timeout.function = firmware_class_timeout;
405 fw_priv->timeout.data = (u_long) fw_priv;
406 init_timer(&fw_priv->timeout);
407
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700408 dev_set_name(f_dev, "%s", dev_name(device));
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700409 f_dev->parent = device;
410 f_dev->class = &firmware_class;
411 dev_set_drvdata(f_dev, fw_priv);
Ming Leif67f1292009-03-01 21:10:49 +0800412 dev_set_uevent_suppress(f_dev, 1);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700413 retval = device_register(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (retval) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700415 dev_err(device, "%s: device_register failed\n", __func__);
Ming Lei6acf70f2009-04-23 22:31:52 +0800416 put_device(f_dev);
Catalin Marinas0f2f2222009-07-08 11:17:40 +0100417 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700419 *dev_p = f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return 0;
421
422error_kfree:
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700423 kfree(f_dev);
Ming Lei6acf70f2009-04-23 22:31:52 +0800424 kfree(fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return retval;
426}
427
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700428static int fw_setup_device(struct firmware *fw, struct device **dev_p,
429 const char *fw_name, struct device *device,
430 int uevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700432 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 struct firmware_priv *fw_priv;
434 int retval;
435
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700436 *dev_p = NULL;
437 retval = fw_register_device(&f_dev, fw_name, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (retval)
439 goto out;
440
441 /* Need to pin this module until class device is destroyed */
442 __module_get(THIS_MODULE);
443
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700444 fw_priv = dev_get_drvdata(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 fw_priv->fw = fw;
Jiri Kosinae1955ca2010-03-09 19:30:28 +0100447 sysfs_bin_attr_init(&fw_priv->attr_data);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700448 retval = sysfs_create_bin_file(&f_dev->kobj, &fw_priv->attr_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (retval) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700450 dev_err(device, "%s: sysfs_create_bin_file failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 goto error_unreg;
452 }
453
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700454 retval = device_create_file(f_dev, &dev_attr_loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (retval) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700456 dev_err(device, "%s: device_create_file failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 goto error_unreg;
458 }
459
Kay Sievers312c0042005-11-16 09:00:00 +0100460 if (uevent)
Ming Leif67f1292009-03-01 21:10:49 +0800461 dev_set_uevent_suppress(f_dev, 0);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700462 *dev_p = f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 goto out;
464
465error_unreg:
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700466 device_unregister(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467out:
468 return retval;
469}
470
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700471static int
472_request_firmware(const struct firmware **firmware_p, const char *name,
Kay Sievers312c0042005-11-16 09:00:00 +0100473 struct device *device, int uevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700475 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 struct firmware_priv *fw_priv;
477 struct firmware *firmware;
David Woodhouse5658c762008-05-23 13:52:42 +0100478 struct builtin_fw *builtin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 int retval;
480
481 if (!firmware_p)
482 return -EINVAL;
483
Jiri Slaby4aed0642005-09-13 01:25:01 -0700484 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700486 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
487 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 retval = -ENOMEM;
489 goto out;
490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
David Woodhouse5658c762008-05-23 13:52:42 +0100492 for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
493 builtin++) {
494 if (strcmp(name, builtin->name))
495 continue;
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700496 dev_info(device, "firmware: using built-in firmware %s\n",
497 name);
David Woodhouse5658c762008-05-23 13:52:42 +0100498 firmware->size = builtin->size;
499 firmware->data = builtin->data;
500 return 0;
501 }
502
503 if (uevent)
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700504 dev_info(device, "firmware: requesting %s\n", name);
David Woodhouse5658c762008-05-23 13:52:42 +0100505
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700506 retval = fw_setup_device(firmware, &f_dev, name, device, uevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (retval)
508 goto error_kfree_fw;
509
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700510 fw_priv = dev_get_drvdata(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Kay Sievers312c0042005-11-16 09:00:00 +0100512 if (uevent) {
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700513 if (loading_timeout > 0) {
514 fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
515 add_timer(&fw_priv->timeout);
516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700518 kobject_uevent(&f_dev->kobj, KOBJ_ADD);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700519 wait_for_completion(&fw_priv->completion);
520 set_bit(FW_STATUS_DONE, &fw_priv->status);
521 del_timer_sync(&fw_priv->timeout);
522 } else
523 wait_for_completion(&fw_priv->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Laura Garciacad1e552006-05-23 23:22:38 +0200525 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
527 retval = -ENOENT;
528 release_firmware(fw_priv->fw);
529 *firmware_p = NULL;
530 }
531 fw_priv->fw = NULL;
Laura Garciacad1e552006-05-23 23:22:38 +0200532 mutex_unlock(&fw_lock);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700533 device_unregister(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 goto out;
535
536error_kfree_fw:
537 kfree(firmware);
538 *firmware_p = NULL;
539out:
540 return retval;
541}
542
543/**
Kay Sievers312c0042005-11-16 09:00:00 +0100544 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800545 * @firmware_p: pointer to firmware image
546 * @name: name of firmware file
547 * @device: device for which firmware is being loaded
548 *
549 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700550 * of @name for device @device.
551 *
552 * Should be called from user context where sleeping is allowed.
553 *
Kay Sievers312c0042005-11-16 09:00:00 +0100554 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700555 * should be distinctive enough not to be confused with any other
556 * firmware image for this or any other device.
557 **/
558int
559request_firmware(const struct firmware **firmware_p, const char *name,
560 struct device *device)
561{
Kay Sievers312c0042005-11-16 09:00:00 +0100562 int uevent = 1;
563 return _request_firmware(firmware_p, name, device, uevent);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700564}
565
566/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800568 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 **/
570void
571release_firmware(const struct firmware *fw)
572{
David Woodhouse5658c762008-05-23 13:52:42 +0100573 struct builtin_fw *builtin;
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (fw) {
David Woodhouse5658c762008-05-23 13:52:42 +0100576 for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
577 builtin++) {
578 if (fw->data == builtin->data)
579 goto free_fw;
580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 vfree(fw->data);
David Woodhouse5658c762008-05-23 13:52:42 +0100582 free_fw:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 kfree(fw);
584 }
585}
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587/* Async support */
588struct firmware_work {
589 struct work_struct work;
590 struct module *module;
591 const char *name;
592 struct device *device;
593 void *context;
594 void (*cont)(const struct firmware *fw, void *context);
Kay Sievers312c0042005-11-16 09:00:00 +0100595 int uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596};
597
598static int
599request_firmware_work_func(void *arg)
600{
601 struct firmware_work *fw_work = arg;
602 const struct firmware *fw;
matthieu castet113fab12005-11-13 16:07:39 -0800603 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (!arg) {
605 WARN_ON(1);
606 return 0;
607 }
matthieu castet113fab12005-11-13 16:07:39 -0800608 ret = _request_firmware(&fw, fw_work->name, fw_work->device,
Kay Sievers312c0042005-11-16 09:00:00 +0100609 fw_work->uevent);
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100610
611 fw_work->cont(fw, fw_work->context);
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 module_put(fw_work->module);
614 kfree(fw_work);
matthieu castet113fab12005-11-13 16:07:39 -0800615 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
618/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000619 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800620 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +0100621 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800622 * is non-zero else the firmware copy must be done manually.
623 * @name: name of firmware file
624 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100625 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800626 * @context: will be passed over to @cont, and
627 * @fw may be %NULL if firmware request fails.
628 * @cont: function will be called asynchronously when the firmware
629 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 *
Ming Lei7fcab092009-05-29 11:33:19 +0800631 * Asynchronous variant of request_firmware() for user contexts where
632 * it is not possible to sleep for long time. It can't be called
633 * in atomic contexts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 **/
635int
636request_firmware_nowait(
Kay Sievers312c0042005-11-16 09:00:00 +0100637 struct module *module, int uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100638 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 void (*cont)(const struct firmware *fw, void *context))
640{
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700641 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100643 gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 if (!fw_work)
646 return -ENOMEM;
647 if (!try_module_get(module)) {
648 kfree(fw_work);
649 return -EFAULT;
650 }
651
652 *fw_work = (struct firmware_work) {
653 .module = module,
654 .name = name,
655 .device = device,
656 .context = context,
657 .cont = cont,
Kay Sievers312c0042005-11-16 09:00:00 +0100658 .uevent = uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 };
660
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700661 task = kthread_run(request_firmware_work_func, fw_work,
662 "firmware/%s", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700664 if (IS_ERR(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 fw_work->cont(NULL, fw_work->context);
matthieu castet113fab12005-11-13 16:07:39 -0800666 module_put(fw_work->module);
667 kfree(fw_work);
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700668 return PTR_ERR(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
670 return 0;
671}
672
673static int __init
674firmware_class_init(void)
675{
676 int error;
677 error = class_register(&firmware_class);
678 if (error) {
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800679 printk(KERN_ERR "%s: class_register failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return error;
681 }
682 error = class_create_file(&firmware_class, &class_attr_timeout);
683 if (error) {
684 printk(KERN_ERR "%s: class_create_file failed\n",
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800685 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 class_unregister(&firmware_class);
687 }
688 return error;
689
690}
691static void __exit
692firmware_class_exit(void)
693{
694 class_unregister(&firmware_class);
695}
696
Shaohua Lia30a6a22006-09-27 01:50:52 -0700697fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698module_exit(firmware_class_exit);
699
700EXPORT_SYMBOL(release_firmware);
701EXPORT_SYMBOL(request_firmware);
702EXPORT_SYMBOL(request_firmware_nowait);