blob: 6604fb33d07291f0ab87222c4bddf2096458f516 [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>
22#include "base.h"
23
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
82 * @buf: buffer to scan for timeout value
83 * @count: number of bytes in @buf
84 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -080086 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * firmware will be provided.
88 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -080089 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 **/
91static ssize_t
Andi Kleen28812fe2010-01-05 12:48:07 +010092firmware_timeout_store(struct class *class,
93 struct class_attribute *attr,
94 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -070097 if (loading_timeout < 0)
98 loading_timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return count;
100}
101
102static CLASS_ATTR(timeout, 0644, firmware_timeout_show, firmware_timeout_store);
103
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700104static void fw_dev_release(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Kay Sievers7eff2e72007-08-14 15:15:12 +0200106static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700108 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Kay Sievers7eff2e72007-08-14 15:15:12 +0200110 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200112 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700113 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 return 0;
116}
117
Adrian Bunk1b81d662006-05-20 15:00:16 -0700118static struct class firmware_class = {
119 .name = "firmware",
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700120 .dev_uevent = firmware_uevent,
121 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700122};
123
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700124static ssize_t firmware_loading_show(struct device *dev,
125 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700127 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
129 return sprintf(buf, "%d\n", loading);
130}
131
David Woodhouse6e03a202009-04-09 22:04:07 -0700132/* Some architectures don't have PAGE_KERNEL_RO */
133#ifndef PAGE_KERNEL_RO
134#define PAGE_KERNEL_RO PAGE_KERNEL
135#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800137 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700138 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800139 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800140 * @buf: buffer to scan for loading control value
141 * @count: number of bytes in @buf
142 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 * The relevant values are:
144 *
145 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800146 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 * -1: Conclude the load with an error and discard any written data.
148 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700149static ssize_t firmware_loading_store(struct device *dev,
150 struct device_attribute *attr,
151 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700153 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700155 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 switch (loading) {
158 case 1:
Laura Garciacad1e552006-05-23 23:22:38 +0200159 mutex_lock(&fw_lock);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700160 if (!fw_priv->fw) {
Laura Garciacad1e552006-05-23 23:22:38 +0200161 mutex_unlock(&fw_lock);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700162 break;
163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 vfree(fw_priv->fw->data);
165 fw_priv->fw->data = NULL;
David Woodhouse6e03a202009-04-09 22:04:07 -0700166 for (i = 0; i < fw_priv->nr_pages; i++)
167 __free_page(fw_priv->pages[i]);
168 kfree(fw_priv->pages);
169 fw_priv->pages = NULL;
170 fw_priv->page_array_size = 0;
171 fw_priv->nr_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 fw_priv->fw->size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 set_bit(FW_STATUS_LOADING, &fw_priv->status);
Laura Garciacad1e552006-05-23 23:22:38 +0200174 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 break;
176 case 0:
177 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700178 vfree(fw_priv->fw->data);
179 fw_priv->fw->data = vmap(fw_priv->pages,
180 fw_priv->nr_pages,
181 0, PAGE_KERNEL_RO);
182 if (!fw_priv->fw->data) {
183 dev_err(dev, "%s: vmap() failed\n", __func__);
184 goto err;
185 }
186 /* Pages will be freed by vfree() */
David Woodhouse6e03a202009-04-09 22:04:07 -0700187 fw_priv->page_array_size = 0;
188 fw_priv->nr_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 complete(&fw_priv->completion);
190 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
191 break;
192 }
193 /* fallthrough */
194 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700195 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /* fallthrough */
197 case -1:
David Woodhouse6e03a202009-04-09 22:04:07 -0700198 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 fw_load_abort(fw_priv);
200 break;
201 }
202
203 return count;
204}
205
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700206static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800209firmware_data_read(struct kobject *kobj, struct bin_attribute *bin_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 char *buffer, loff_t offset, size_t count)
211{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700212 struct device *dev = to_dev(kobj);
213 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 struct firmware *fw;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700215 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Laura Garciacad1e552006-05-23 23:22:38 +0200217 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700219 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 ret_count = -ENODEV;
221 goto out;
222 }
Jiri Slaby308975f2009-06-21 23:57:31 +0200223 if (offset > fw->size) {
224 ret_count = 0;
225 goto out;
226 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700227 if (count > fw->size - offset)
228 count = fw->size - offset;
229
230 ret_count = count;
231
232 while (count) {
233 void *page_data;
234 int page_nr = offset >> PAGE_SHIFT;
235 int page_ofs = offset & (PAGE_SIZE-1);
236 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
237
238 page_data = kmap(fw_priv->pages[page_nr]);
239
240 memcpy(buffer, page_data + page_ofs, page_cnt);
241
242 kunmap(fw_priv->pages[page_nr]);
243 buffer += page_cnt;
244 offset += page_cnt;
245 count -= page_cnt;
246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247out:
Laura Garciacad1e552006-05-23 23:22:38 +0200248 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return ret_count;
250}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252static int
253fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
254{
David Woodhouse6e03a202009-04-09 22:04:07 -0700255 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
David Woodhouse6e03a202009-04-09 22:04:07 -0700257 /* If the array of pages is too small, grow it... */
258 if (fw_priv->page_array_size < pages_needed) {
259 int new_array_size = max(pages_needed,
260 fw_priv->page_array_size * 2);
261 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
David Woodhouse6e03a202009-04-09 22:04:07 -0700263 new_pages = kmalloc(new_array_size * sizeof(void *),
264 GFP_KERNEL);
265 if (!new_pages) {
266 fw_load_abort(fw_priv);
267 return -ENOMEM;
268 }
269 memcpy(new_pages, fw_priv->pages,
270 fw_priv->page_array_size * sizeof(void *));
271 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
272 (new_array_size - fw_priv->page_array_size));
273 kfree(fw_priv->pages);
274 fw_priv->pages = new_pages;
275 fw_priv->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700277
278 while (fw_priv->nr_pages < pages_needed) {
279 fw_priv->pages[fw_priv->nr_pages] =
280 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
281
282 if (!fw_priv->pages[fw_priv->nr_pages]) {
283 fw_load_abort(fw_priv);
284 return -ENOMEM;
285 }
286 fw_priv->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return 0;
289}
290
291/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800292 * firmware_data_write - write method for firmware
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700293 * @kobj: kobject for the device
Randy Dunlap42e61f4a2007-07-23 21:42:11 -0700294 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800295 * @buffer: buffer being written
296 * @offset: buffer offset for write in total data store area
297 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800299 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 * the driver as a firmware image.
301 **/
302static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800303firmware_data_write(struct kobject *kobj, struct bin_attribute *bin_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 char *buffer, loff_t offset, size_t count)
305{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700306 struct device *dev = to_dev(kobj);
307 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 struct firmware *fw;
309 ssize_t retval;
310
311 if (!capable(CAP_SYS_RAWIO))
312 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700313
Laura Garciacad1e552006-05-23 23:22:38 +0200314 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700316 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 retval = -ENODEV;
318 goto out;
319 }
320 retval = fw_realloc_buffer(fw_priv, offset + count);
321 if (retval)
322 goto out;
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700325
326 while (count) {
327 void *page_data;
328 int page_nr = offset >> PAGE_SHIFT;
329 int page_ofs = offset & (PAGE_SIZE - 1);
330 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
331
332 page_data = kmap(fw_priv->pages[page_nr]);
333
334 memcpy(page_data + page_ofs, buffer, page_cnt);
335
336 kunmap(fw_priv->pages[page_nr]);
337 buffer += page_cnt;
338 offset += page_cnt;
339 count -= page_cnt;
340 }
341
342 fw->size = max_t(size_t, offset, fw->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343out:
Laura Garciacad1e552006-05-23 23:22:38 +0200344 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return retval;
346}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348static struct bin_attribute firmware_attr_data_tmpl = {
Tejun Heo7b595752007-06-14 03:45:17 +0900349 .attr = {.name = "data", .mode = 0644},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 .size = 0,
351 .read = firmware_data_read,
352 .write = firmware_data_write,
353};
354
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700355static void fw_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700357 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
David Woodhouse6e03a202009-04-09 22:04:07 -0700358 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
David Woodhouse6e03a202009-04-09 22:04:07 -0700360 for (i = 0; i < fw_priv->nr_pages; i++)
361 __free_page(fw_priv->pages[i]);
362 kfree(fw_priv->pages);
Samuel Ortiz976821d2009-05-27 00:49:31 +0200363 kfree(fw_priv->fw_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 kfree(fw_priv);
Catalin Marinas0f2f2222009-07-08 11:17:40 +0100365 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 module_put(THIS_MODULE);
368}
369
370static void
371firmware_class_timeout(u_long data)
372{
373 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
374 fw_load_abort(fw_priv);
375}
376
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700377static int fw_register_device(struct device **dev_p, const char *fw_name,
378 struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 int retval;
Jiri Slaby4aed0642005-09-13 01:25:01 -0700381 struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 GFP_KERNEL);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700383 struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700385 *dev_p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700387 if (!fw_priv || !f_dev) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700388 dev_err(device, "%s: kmalloc failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 retval = -ENOMEM;
390 goto error_kfree;
391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 init_completion(&fw_priv->completion);
394 fw_priv->attr_data = firmware_attr_data_tmpl;
Samuel Ortiz976821d2009-05-27 00:49:31 +0200395 fw_priv->fw_id = kstrdup(fw_name, GFP_KERNEL);
396 if (!fw_priv->fw_id) {
397 dev_err(device, "%s: Firmware name allocation failed\n",
398 __func__);
399 retval = -ENOMEM;
400 goto error_kfree;
401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 fw_priv->timeout.function = firmware_class_timeout;
404 fw_priv->timeout.data = (u_long) fw_priv;
405 init_timer(&fw_priv->timeout);
406
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700407 dev_set_name(f_dev, "%s", dev_name(device));
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700408 f_dev->parent = device;
409 f_dev->class = &firmware_class;
410 dev_set_drvdata(f_dev, fw_priv);
Ming Leif67f1292009-03-01 21:10:49 +0800411 dev_set_uevent_suppress(f_dev, 1);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700412 retval = device_register(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (retval) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700414 dev_err(device, "%s: device_register failed\n", __func__);
Ming Lei6acf70f2009-04-23 22:31:52 +0800415 put_device(f_dev);
Catalin Marinas0f2f2222009-07-08 11:17:40 +0100416 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700418 *dev_p = f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return 0;
420
421error_kfree:
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700422 kfree(f_dev);
Ming Lei6acf70f2009-04-23 22:31:52 +0800423 kfree(fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return retval;
425}
426
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700427static int fw_setup_device(struct firmware *fw, struct device **dev_p,
428 const char *fw_name, struct device *device,
429 int uevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700431 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 struct firmware_priv *fw_priv;
433 int retval;
434
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700435 *dev_p = NULL;
436 retval = fw_register_device(&f_dev, fw_name, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (retval)
438 goto out;
439
440 /* Need to pin this module until class device is destroyed */
441 __module_get(THIS_MODULE);
442
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700443 fw_priv = dev_get_drvdata(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 fw_priv->fw = fw;
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700446 retval = sysfs_create_bin_file(&f_dev->kobj, &fw_priv->attr_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (retval) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700448 dev_err(device, "%s: sysfs_create_bin_file failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 goto error_unreg;
450 }
451
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700452 retval = device_create_file(f_dev, &dev_attr_loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (retval) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700454 dev_err(device, "%s: device_create_file failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 goto error_unreg;
456 }
457
Kay Sievers312c0042005-11-16 09:00:00 +0100458 if (uevent)
Ming Leif67f1292009-03-01 21:10:49 +0800459 dev_set_uevent_suppress(f_dev, 0);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700460 *dev_p = f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 goto out;
462
463error_unreg:
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700464 device_unregister(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465out:
466 return retval;
467}
468
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700469static int
470_request_firmware(const struct firmware **firmware_p, const char *name,
Kay Sievers312c0042005-11-16 09:00:00 +0100471 struct device *device, int uevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700473 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 struct firmware_priv *fw_priv;
475 struct firmware *firmware;
David Woodhouse5658c762008-05-23 13:52:42 +0100476 struct builtin_fw *builtin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 int retval;
478
479 if (!firmware_p)
480 return -EINVAL;
481
Jiri Slaby4aed0642005-09-13 01:25:01 -0700482 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700484 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
485 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 retval = -ENOMEM;
487 goto out;
488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
David Woodhouse5658c762008-05-23 13:52:42 +0100490 for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
491 builtin++) {
492 if (strcmp(name, builtin->name))
493 continue;
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700494 dev_info(device, "firmware: using built-in firmware %s\n",
495 name);
David Woodhouse5658c762008-05-23 13:52:42 +0100496 firmware->size = builtin->size;
497 firmware->data = builtin->data;
498 return 0;
499 }
500
501 if (uevent)
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700502 dev_info(device, "firmware: requesting %s\n", name);
David Woodhouse5658c762008-05-23 13:52:42 +0100503
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700504 retval = fw_setup_device(firmware, &f_dev, name, device, uevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (retval)
506 goto error_kfree_fw;
507
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700508 fw_priv = dev_get_drvdata(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Kay Sievers312c0042005-11-16 09:00:00 +0100510 if (uevent) {
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700511 if (loading_timeout > 0) {
512 fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
513 add_timer(&fw_priv->timeout);
514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700516 kobject_uevent(&f_dev->kobj, KOBJ_ADD);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700517 wait_for_completion(&fw_priv->completion);
518 set_bit(FW_STATUS_DONE, &fw_priv->status);
519 del_timer_sync(&fw_priv->timeout);
520 } else
521 wait_for_completion(&fw_priv->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Laura Garciacad1e552006-05-23 23:22:38 +0200523 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
525 retval = -ENOENT;
526 release_firmware(fw_priv->fw);
527 *firmware_p = NULL;
528 }
529 fw_priv->fw = NULL;
Laura Garciacad1e552006-05-23 23:22:38 +0200530 mutex_unlock(&fw_lock);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700531 device_unregister(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 goto out;
533
534error_kfree_fw:
535 kfree(firmware);
536 *firmware_p = NULL;
537out:
538 return retval;
539}
540
541/**
Kay Sievers312c0042005-11-16 09:00:00 +0100542 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800543 * @firmware_p: pointer to firmware image
544 * @name: name of firmware file
545 * @device: device for which firmware is being loaded
546 *
547 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700548 * of @name for device @device.
549 *
550 * Should be called from user context where sleeping is allowed.
551 *
Kay Sievers312c0042005-11-16 09:00:00 +0100552 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700553 * should be distinctive enough not to be confused with any other
554 * firmware image for this or any other device.
555 **/
556int
557request_firmware(const struct firmware **firmware_p, const char *name,
558 struct device *device)
559{
Kay Sievers312c0042005-11-16 09:00:00 +0100560 int uevent = 1;
561 return _request_firmware(firmware_p, name, device, uevent);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700562}
563
564/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800566 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 **/
568void
569release_firmware(const struct firmware *fw)
570{
David Woodhouse5658c762008-05-23 13:52:42 +0100571 struct builtin_fw *builtin;
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (fw) {
David Woodhouse5658c762008-05-23 13:52:42 +0100574 for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
575 builtin++) {
576 if (fw->data == builtin->data)
577 goto free_fw;
578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 vfree(fw->data);
David Woodhouse5658c762008-05-23 13:52:42 +0100580 free_fw:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 kfree(fw);
582 }
583}
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585/* Async support */
586struct firmware_work {
587 struct work_struct work;
588 struct module *module;
589 const char *name;
590 struct device *device;
591 void *context;
592 void (*cont)(const struct firmware *fw, void *context);
Kay Sievers312c0042005-11-16 09:00:00 +0100593 int uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594};
595
596static int
597request_firmware_work_func(void *arg)
598{
599 struct firmware_work *fw_work = arg;
600 const struct firmware *fw;
matthieu castet113fab12005-11-13 16:07:39 -0800601 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (!arg) {
603 WARN_ON(1);
604 return 0;
605 }
matthieu castet113fab12005-11-13 16:07:39 -0800606 ret = _request_firmware(&fw, fw_work->name, fw_work->device,
Kay Sievers312c0042005-11-16 09:00:00 +0100607 fw_work->uevent);
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100608
609 fw_work->cont(fw, fw_work->context);
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 module_put(fw_work->module);
612 kfree(fw_work);
matthieu castet113fab12005-11-13 16:07:39 -0800613 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
616/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800617 * request_firmware_nowait: asynchronous version of request_firmware
618 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +0100619 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800620 * is non-zero else the firmware copy must be done manually.
621 * @name: name of firmware file
622 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100623 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800624 * @context: will be passed over to @cont, and
625 * @fw may be %NULL if firmware request fails.
626 * @cont: function will be called asynchronously when the firmware
627 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 *
Ming Lei7fcab092009-05-29 11:33:19 +0800629 * Asynchronous variant of request_firmware() for user contexts where
630 * it is not possible to sleep for long time. It can't be called
631 * in atomic contexts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 **/
633int
634request_firmware_nowait(
Kay Sievers312c0042005-11-16 09:00:00 +0100635 struct module *module, int uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100636 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 void (*cont)(const struct firmware *fw, void *context))
638{
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700639 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100641 gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 if (!fw_work)
644 return -ENOMEM;
645 if (!try_module_get(module)) {
646 kfree(fw_work);
647 return -EFAULT;
648 }
649
650 *fw_work = (struct firmware_work) {
651 .module = module,
652 .name = name,
653 .device = device,
654 .context = context,
655 .cont = cont,
Kay Sievers312c0042005-11-16 09:00:00 +0100656 .uevent = uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 };
658
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700659 task = kthread_run(request_firmware_work_func, fw_work,
660 "firmware/%s", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700662 if (IS_ERR(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 fw_work->cont(NULL, fw_work->context);
matthieu castet113fab12005-11-13 16:07:39 -0800664 module_put(fw_work->module);
665 kfree(fw_work);
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700666 return PTR_ERR(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668 return 0;
669}
670
671static int __init
672firmware_class_init(void)
673{
674 int error;
675 error = class_register(&firmware_class);
676 if (error) {
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800677 printk(KERN_ERR "%s: class_register failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return error;
679 }
680 error = class_create_file(&firmware_class, &class_attr_timeout);
681 if (error) {
682 printk(KERN_ERR "%s: class_create_file failed\n",
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800683 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 class_unregister(&firmware_class);
685 }
686 return error;
687
688}
689static void __exit
690firmware_class_exit(void)
691{
692 class_unregister(&firmware_class);
693}
694
Shaohua Lia30a6a22006-09-27 01:50:52 -0700695fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696module_exit(firmware_class_exit);
697
698EXPORT_SYMBOL(release_firmware);
699EXPORT_SYMBOL(request_firmware);
700EXPORT_SYMBOL(request_firmware_nowait);