blob: fb29c64ebecc1548ccd4b3c3dae0c0771205cbcc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * firmware_class.c - Multi purpose firmware loading support
3 *
Markus Rechberger87d37a42007-06-04 18:45:44 +02004 * Copyright (c) 2003 Manuel Estrada Sainz
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Please see Documentation/firmware_class/ for more information.
7 *
8 */
9
Randy.Dunlapc59ede72006-01-11 12:17:46 -080010#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/device.h>
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/timer.h>
15#include <linux/vmalloc.h>
16#include <linux/interrupt.h>
17#include <linux/bitops.h>
Laura Garciacad1e552006-05-23 23:22:38 +020018#include <linux/mutex.h>
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -070019#include <linux/kthread.h>
David Woodhouse6e03a202009-04-09 22:04:07 -070020#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/firmware.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -070024#define to_dev(obj) container_of(obj, struct device, kobj)
25
Markus Rechberger87d37a42007-06-04 18:45:44 +020026MODULE_AUTHOR("Manuel Estrada Sainz");
Linus Torvalds1da177e2005-04-16 15:20:36 -070027MODULE_DESCRIPTION("Multi purpose firmware loading support");
28MODULE_LICENSE("GPL");
29
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080030/* Builtin firmware support */
31
32#ifdef CONFIG_FW_LOADER
33
34extern struct builtin_fw __start_builtin_fw[];
35extern struct builtin_fw __end_builtin_fw[];
36
37static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
38{
39 struct builtin_fw *b_fw;
40
41 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
42 if (strcmp(name, b_fw->name) == 0) {
43 fw->size = b_fw->size;
44 fw->data = b_fw->data;
45 return true;
46 }
47 }
48
49 return false;
50}
51
52static bool fw_is_builtin_firmware(const struct firmware *fw)
53{
54 struct builtin_fw *b_fw;
55
56 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
57 if (fw->data == b_fw->data)
58 return true;
59
60 return false;
61}
62
63#else /* Module case - no builtin firmware support */
64
65static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
66{
67 return false;
68}
69
70static inline bool fw_is_builtin_firmware(const struct firmware *fw)
71{
72 return false;
73}
74#endif
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076enum {
77 FW_STATUS_LOADING,
78 FW_STATUS_DONE,
79 FW_STATUS_ABORT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080};
81
Dave Jones2f651682007-01-25 15:56:15 -050082static int loading_timeout = 60; /* In seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84/* fw_lock could be moved to 'struct firmware_priv' but since it is just
85 * guarding for corner cases a global lock should be OK */
Laura Garciacad1e552006-05-23 23:22:38 +020086static DEFINE_MUTEX(fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88struct firmware_priv {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 struct completion completion;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 struct firmware *fw;
91 unsigned long status;
David Woodhouse6e03a202009-04-09 22:04:07 -070092 struct page **pages;
93 int nr_pages;
94 int page_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 struct timer_list timeout;
Johannes Berge9045f92010-03-29 17:57:20 +020096 bool nowait;
Dmitry Torokhove1771232010-03-13 23:49:23 -080097 char fw_id[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070098};
99
Arjan van de Ven858119e2006-01-14 13:20:43 -0800100static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101fw_load_abort(struct firmware_priv *fw_priv)
102{
103 set_bit(FW_STATUS_ABORT, &fw_priv->status);
104 wmb();
105 complete(&fw_priv->completion);
106}
107
108static ssize_t
Andi Kleen28812fe2010-01-05 12:48:07 +0100109firmware_timeout_show(struct class *class,
110 struct class_attribute *attr,
111 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 return sprintf(buf, "%d\n", loading_timeout);
114}
115
116/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800117 * firmware_timeout_store - set number of seconds to wait for firmware
118 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800119 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800120 * @buf: buffer to scan for timeout value
121 * @count: number of bytes in @buf
122 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800124 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 * firmware will be provided.
126 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800127 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 **/
129static ssize_t
Andi Kleen28812fe2010-01-05 12:48:07 +0100130firmware_timeout_store(struct class *class,
131 struct class_attribute *attr,
132 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700135 if (loading_timeout < 0)
136 loading_timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return count;
138}
139
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800140static struct class_attribute firmware_class_attrs[] = {
141 __ATTR(timeout, S_IWUSR | S_IRUGO,
142 firmware_timeout_show, firmware_timeout_store),
143 __ATTR_NULL
144};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800146static void fw_dev_release(struct device *dev)
147{
148 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
149 int i;
150
151 for (i = 0; i < fw_priv->nr_pages; i++)
152 __free_page(fw_priv->pages[i]);
153 kfree(fw_priv->pages);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800154 kfree(fw_priv);
155 kfree(dev);
156
157 module_put(THIS_MODULE);
158}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Kay Sievers7eff2e72007-08-14 15:15:12 +0200160static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700162 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Kay Sievers7eff2e72007-08-14 15:15:12 +0200164 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200166 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700167 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200168 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
169 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 return 0;
172}
173
Adrian Bunk1b81d662006-05-20 15:00:16 -0700174static struct class firmware_class = {
175 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800176 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700177 .dev_uevent = firmware_uevent,
178 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700179};
180
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700181static ssize_t firmware_loading_show(struct device *dev,
182 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700184 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
186 return sprintf(buf, "%d\n", loading);
187}
188
David Woodhousedd336c52010-05-02 11:21:21 +0300189static void firmware_free_data(const struct firmware *fw)
190{
191 int i;
192 vunmap(fw->data);
193 if (fw->pages) {
194 for (i = 0; i < PFN_UP(fw->size); i++)
195 __free_page(fw->pages[i]);
196 kfree(fw->pages);
197 }
198}
199
David Woodhouse6e03a202009-04-09 22:04:07 -0700200/* Some architectures don't have PAGE_KERNEL_RO */
201#ifndef PAGE_KERNEL_RO
202#define PAGE_KERNEL_RO PAGE_KERNEL
203#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800205 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700206 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800207 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800208 * @buf: buffer to scan for loading control value
209 * @count: number of bytes in @buf
210 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * The relevant values are:
212 *
213 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800214 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * -1: Conclude the load with an error and discard any written data.
216 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700217static ssize_t firmware_loading_store(struct device *dev,
218 struct device_attribute *attr,
219 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700221 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700223 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 switch (loading) {
226 case 1:
Laura Garciacad1e552006-05-23 23:22:38 +0200227 mutex_lock(&fw_lock);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700228 if (!fw_priv->fw) {
Laura Garciacad1e552006-05-23 23:22:38 +0200229 mutex_unlock(&fw_lock);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700230 break;
231 }
David Woodhousedd336c52010-05-02 11:21:21 +0300232 firmware_free_data(fw_priv->fw);
233 memset(fw_priv->fw, 0, sizeof(struct firmware));
234 /* If the pages are not owned by 'struct firmware' */
David Woodhouse6e03a202009-04-09 22:04:07 -0700235 for (i = 0; i < fw_priv->nr_pages; i++)
236 __free_page(fw_priv->pages[i]);
237 kfree(fw_priv->pages);
238 fw_priv->pages = NULL;
239 fw_priv->page_array_size = 0;
240 fw_priv->nr_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 set_bit(FW_STATUS_LOADING, &fw_priv->status);
Laura Garciacad1e552006-05-23 23:22:38 +0200242 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 break;
244 case 0:
245 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
David Woodhousedd336c52010-05-02 11:21:21 +0300246 vunmap(fw_priv->fw->data);
David Woodhouse6e03a202009-04-09 22:04:07 -0700247 fw_priv->fw->data = vmap(fw_priv->pages,
248 fw_priv->nr_pages,
249 0, PAGE_KERNEL_RO);
250 if (!fw_priv->fw->data) {
251 dev_err(dev, "%s: vmap() failed\n", __func__);
252 goto err;
253 }
David Woodhousedd336c52010-05-02 11:21:21 +0300254 /* Pages are now owned by 'struct firmware' */
255 fw_priv->fw->pages = fw_priv->pages;
256 fw_priv->pages = NULL;
257
David Woodhouse6e03a202009-04-09 22:04:07 -0700258 fw_priv->page_array_size = 0;
259 fw_priv->nr_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 complete(&fw_priv->completion);
261 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
262 break;
263 }
264 /* fallthrough */
265 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700266 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 /* fallthrough */
268 case -1:
David Woodhouse6e03a202009-04-09 22:04:07 -0700269 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 fw_load_abort(fw_priv);
271 break;
272 }
273
274 return count;
275}
276
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700277static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700280firmware_data_read(struct file *filp, struct kobject *kobj,
281 struct bin_attribute *bin_attr, char *buffer, loff_t offset,
282 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700284 struct device *dev = to_dev(kobj);
285 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 struct firmware *fw;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700287 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Laura Garciacad1e552006-05-23 23:22:38 +0200289 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700291 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 ret_count = -ENODEV;
293 goto out;
294 }
Jiri Slaby308975f2009-06-21 23:57:31 +0200295 if (offset > fw->size) {
296 ret_count = 0;
297 goto out;
298 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700299 if (count > fw->size - offset)
300 count = fw->size - offset;
301
302 ret_count = count;
303
304 while (count) {
305 void *page_data;
306 int page_nr = offset >> PAGE_SHIFT;
307 int page_ofs = offset & (PAGE_SIZE-1);
308 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
309
310 page_data = kmap(fw_priv->pages[page_nr]);
311
312 memcpy(buffer, page_data + page_ofs, page_cnt);
313
314 kunmap(fw_priv->pages[page_nr]);
315 buffer += page_cnt;
316 offset += page_cnt;
317 count -= page_cnt;
318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319out:
Laura Garciacad1e552006-05-23 23:22:38 +0200320 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return ret_count;
322}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324static int
325fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
326{
David Woodhouse6e03a202009-04-09 22:04:07 -0700327 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
David Woodhouse6e03a202009-04-09 22:04:07 -0700329 /* If the array of pages is too small, grow it... */
330 if (fw_priv->page_array_size < pages_needed) {
331 int new_array_size = max(pages_needed,
332 fw_priv->page_array_size * 2);
333 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
David Woodhouse6e03a202009-04-09 22:04:07 -0700335 new_pages = kmalloc(new_array_size * sizeof(void *),
336 GFP_KERNEL);
337 if (!new_pages) {
338 fw_load_abort(fw_priv);
339 return -ENOMEM;
340 }
341 memcpy(new_pages, fw_priv->pages,
342 fw_priv->page_array_size * sizeof(void *));
343 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
344 (new_array_size - fw_priv->page_array_size));
345 kfree(fw_priv->pages);
346 fw_priv->pages = new_pages;
347 fw_priv->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700349
350 while (fw_priv->nr_pages < pages_needed) {
351 fw_priv->pages[fw_priv->nr_pages] =
352 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
353
354 if (!fw_priv->pages[fw_priv->nr_pages]) {
355 fw_load_abort(fw_priv);
356 return -ENOMEM;
357 }
358 fw_priv->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return 0;
361}
362
363/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800364 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700365 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700366 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700367 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800368 * @buffer: buffer being written
369 * @offset: buffer offset for write in total data store area
370 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800372 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 * the driver as a firmware image.
374 **/
375static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700376firmware_data_write(struct file* filp, struct kobject *kobj,
377 struct bin_attribute *bin_attr, char *buffer,
378 loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700380 struct device *dev = to_dev(kobj);
381 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 struct firmware *fw;
383 ssize_t retval;
384
385 if (!capable(CAP_SYS_RAWIO))
386 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700387
Laura Garciacad1e552006-05-23 23:22:38 +0200388 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700390 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 retval = -ENODEV;
392 goto out;
393 }
394 retval = fw_realloc_buffer(fw_priv, offset + count);
395 if (retval)
396 goto out;
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700399
400 while (count) {
401 void *page_data;
402 int page_nr = offset >> PAGE_SHIFT;
403 int page_ofs = offset & (PAGE_SIZE - 1);
404 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
405
406 page_data = kmap(fw_priv->pages[page_nr]);
407
408 memcpy(page_data + page_ofs, buffer, page_cnt);
409
410 kunmap(fw_priv->pages[page_nr]);
411 buffer += page_cnt;
412 offset += page_cnt;
413 count -= page_cnt;
414 }
415
416 fw->size = max_t(size_t, offset, fw->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417out:
Laura Garciacad1e552006-05-23 23:22:38 +0200418 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return retval;
420}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800421
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700422static struct bin_attribute firmware_attr_data = {
423 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 .size = 0,
425 .read = firmware_data_read,
426 .write = firmware_data_write,
427};
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429static void
430firmware_class_timeout(u_long data)
431{
432 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
433 fw_load_abort(fw_priv);
434}
435
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700436static int fw_register_device(struct device **dev_p, const char *fw_name,
437 struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 int retval;
Dmitry Torokhove1771232010-03-13 23:49:23 -0800440 struct firmware_priv *fw_priv =
441 kzalloc(sizeof(*fw_priv) + strlen(fw_name) + 1 , GFP_KERNEL);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700442 struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700444 *dev_p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700446 if (!fw_priv || !f_dev) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700447 dev_err(device, "%s: kmalloc failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 retval = -ENOMEM;
449 goto error_kfree;
450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Dmitry Torokhove1771232010-03-13 23:49:23 -0800452 strcpy(fw_priv->fw_id, fw_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 init_completion(&fw_priv->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 fw_priv->timeout.function = firmware_class_timeout;
455 fw_priv->timeout.data = (u_long) fw_priv;
456 init_timer(&fw_priv->timeout);
457
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700458 dev_set_name(f_dev, "%s", dev_name(device));
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700459 f_dev->parent = device;
460 f_dev->class = &firmware_class;
461 dev_set_drvdata(f_dev, fw_priv);
Ming Leif67f1292009-03-01 21:10:49 +0800462 dev_set_uevent_suppress(f_dev, 1);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700463 retval = device_register(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (retval) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700465 dev_err(device, "%s: device_register failed\n", __func__);
Ming Lei6acf70f2009-04-23 22:31:52 +0800466 put_device(f_dev);
Catalin Marinas0f2f2222009-07-08 11:17:40 +0100467 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700469 *dev_p = f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return 0;
471
472error_kfree:
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700473 kfree(f_dev);
Ming Lei6acf70f2009-04-23 22:31:52 +0800474 kfree(fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return retval;
476}
477
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700478static int fw_setup_device(struct firmware *fw, struct device **dev_p,
479 const char *fw_name, struct device *device,
Johannes Berge9045f92010-03-29 17:57:20 +0200480 int uevent, bool nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700482 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 struct firmware_priv *fw_priv;
484 int retval;
485
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700486 *dev_p = NULL;
487 retval = fw_register_device(&f_dev, fw_name, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (retval)
489 goto out;
490
491 /* Need to pin this module until class device is destroyed */
492 __module_get(THIS_MODULE);
493
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700494 fw_priv = dev_get_drvdata(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Johannes Berge9045f92010-03-29 17:57:20 +0200496 fw_priv->nowait = nowait;
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 fw_priv->fw = fw;
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700499 retval = sysfs_create_bin_file(&f_dev->kobj, &firmware_attr_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (retval) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700501 dev_err(device, "%s: sysfs_create_bin_file failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 goto error_unreg;
503 }
504
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700505 retval = device_create_file(f_dev, &dev_attr_loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (retval) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700507 dev_err(device, "%s: device_create_file failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 goto error_unreg;
509 }
510
Kay Sievers312c0042005-11-16 09:00:00 +0100511 if (uevent)
Ming Leif67f1292009-03-01 21:10:49 +0800512 dev_set_uevent_suppress(f_dev, 0);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700513 *dev_p = f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 goto out;
515
516error_unreg:
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700517 device_unregister(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518out:
519 return retval;
520}
521
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700522static int
523_request_firmware(const struct firmware **firmware_p, const char *name,
Johannes Berge9045f92010-03-29 17:57:20 +0200524 struct device *device, int uevent, bool nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700526 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 struct firmware_priv *fw_priv;
528 struct firmware *firmware;
529 int retval;
530
531 if (!firmware_p)
532 return -EINVAL;
533
Jiri Slaby4aed0642005-09-13 01:25:01 -0700534 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700536 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
537 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 retval = -ENOMEM;
539 goto out;
540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800542 if (fw_get_builtin_firmware(firmware, name)) {
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100543 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
David Woodhouse5658c762008-05-23 13:52:42 +0100544 return 0;
545 }
546
547 if (uevent)
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100548 dev_dbg(device, "firmware: requesting %s\n", name);
David Woodhouse5658c762008-05-23 13:52:42 +0100549
Johannes Berge9045f92010-03-29 17:57:20 +0200550 retval = fw_setup_device(firmware, &f_dev, name, device,
551 uevent, nowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (retval)
553 goto error_kfree_fw;
554
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700555 fw_priv = dev_get_drvdata(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Kay Sievers312c0042005-11-16 09:00:00 +0100557 if (uevent) {
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700558 if (loading_timeout > 0) {
559 fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
560 add_timer(&fw_priv->timeout);
561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700563 kobject_uevent(&f_dev->kobj, KOBJ_ADD);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700564 wait_for_completion(&fw_priv->completion);
565 set_bit(FW_STATUS_DONE, &fw_priv->status);
566 del_timer_sync(&fw_priv->timeout);
567 } else
568 wait_for_completion(&fw_priv->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Laura Garciacad1e552006-05-23 23:22:38 +0200570 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
572 retval = -ENOENT;
573 release_firmware(fw_priv->fw);
574 *firmware_p = NULL;
575 }
576 fw_priv->fw = NULL;
Laura Garciacad1e552006-05-23 23:22:38 +0200577 mutex_unlock(&fw_lock);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700578 device_unregister(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 goto out;
580
581error_kfree_fw:
582 kfree(firmware);
583 *firmware_p = NULL;
584out:
585 return retval;
586}
587
588/**
Kay Sievers312c0042005-11-16 09:00:00 +0100589 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800590 * @firmware_p: pointer to firmware image
591 * @name: name of firmware file
592 * @device: device for which firmware is being loaded
593 *
594 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700595 * of @name for device @device.
596 *
597 * Should be called from user context where sleeping is allowed.
598 *
Kay Sievers312c0042005-11-16 09:00:00 +0100599 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700600 * should be distinctive enough not to be confused with any other
601 * firmware image for this or any other device.
602 **/
603int
604request_firmware(const struct firmware **firmware_p, const char *name,
605 struct device *device)
606{
Kay Sievers312c0042005-11-16 09:00:00 +0100607 int uevent = 1;
Johannes Berge9045f92010-03-29 17:57:20 +0200608 return _request_firmware(firmware_p, name, device, uevent, false);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700609}
610
611/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800613 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800615void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800618 if (!fw_is_builtin_firmware(fw))
619 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 kfree(fw);
621 }
622}
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624/* Async support */
625struct firmware_work {
626 struct work_struct work;
627 struct module *module;
628 const char *name;
629 struct device *device;
630 void *context;
631 void (*cont)(const struct firmware *fw, void *context);
Kay Sievers312c0042005-11-16 09:00:00 +0100632 int uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633};
634
635static int
636request_firmware_work_func(void *arg)
637{
638 struct firmware_work *fw_work = arg;
639 const struct firmware *fw;
matthieu castet113fab12005-11-13 16:07:39 -0800640 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (!arg) {
642 WARN_ON(1);
643 return 0;
644 }
matthieu castet113fab12005-11-13 16:07:39 -0800645 ret = _request_firmware(&fw, fw_work->name, fw_work->device,
Johannes Berge9045f92010-03-29 17:57:20 +0200646 fw_work->uevent, true);
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100647
648 fw_work->cont(fw, fw_work->context);
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 module_put(fw_work->module);
651 kfree(fw_work);
matthieu castet113fab12005-11-13 16:07:39 -0800652 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
655/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000656 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800657 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +0100658 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800659 * is non-zero else the firmware copy must be done manually.
660 * @name: name of firmware file
661 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100662 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800663 * @context: will be passed over to @cont, and
664 * @fw may be %NULL if firmware request fails.
665 * @cont: function will be called asynchronously when the firmware
666 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 *
Ming Lei7fcab092009-05-29 11:33:19 +0800668 * Asynchronous variant of request_firmware() for user contexts where
669 * it is not possible to sleep for long time. It can't be called
670 * in atomic contexts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 **/
672int
673request_firmware_nowait(
Kay Sievers312c0042005-11-16 09:00:00 +0100674 struct module *module, int uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100675 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 void (*cont)(const struct firmware *fw, void *context))
677{
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700678 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100680 gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 if (!fw_work)
683 return -ENOMEM;
684 if (!try_module_get(module)) {
685 kfree(fw_work);
686 return -EFAULT;
687 }
688
689 *fw_work = (struct firmware_work) {
690 .module = module,
691 .name = name,
692 .device = device,
693 .context = context,
694 .cont = cont,
Kay Sievers312c0042005-11-16 09:00:00 +0100695 .uevent = uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 };
697
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700698 task = kthread_run(request_firmware_work_func, fw_work,
699 "firmware/%s", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700701 if (IS_ERR(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 fw_work->cont(NULL, fw_work->context);
matthieu castet113fab12005-11-13 16:07:39 -0800703 module_put(fw_work->module);
704 kfree(fw_work);
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700705 return PTR_ERR(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707 return 0;
708}
709
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800710static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800712 return class_register(&firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800714
715static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
717 class_unregister(&firmware_class);
718}
719
Shaohua Lia30a6a22006-09-27 01:50:52 -0700720fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721module_exit(firmware_class_exit);
722
723EXPORT_SYMBOL(release_firmware);
724EXPORT_SYMBOL(request_firmware);
725EXPORT_SYMBOL(request_firmware_nowait);