blob: d0dc26ad53871df760302961376ed779c58a113c [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -070023#define to_dev(obj) container_of(obj, struct device, kobj)
24
Markus Rechberger87d37a42007-06-04 18:45:44 +020025MODULE_AUTHOR("Manuel Estrada Sainz");
Linus Torvalds1da177e2005-04-16 15:20:36 -070026MODULE_DESCRIPTION("Multi purpose firmware loading support");
27MODULE_LICENSE("GPL");
28
29enum {
30 FW_STATUS_LOADING,
31 FW_STATUS_DONE,
32 FW_STATUS_ABORT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070033};
34
Dave Jones2f651682007-01-25 15:56:15 -050035static int loading_timeout = 60; /* In seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/* fw_lock could be moved to 'struct firmware_priv' but since it is just
38 * guarding for corner cases a global lock should be OK */
Laura Garciacad1e552006-05-23 23:22:38 +020039static DEFINE_MUTEX(fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41struct firmware_priv {
Samuel Ortiz976821d2009-05-27 00:49:31 +020042 char *fw_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 struct completion completion;
44 struct bin_attribute attr_data;
45 struct firmware *fw;
46 unsigned long status;
David Woodhouse6e03a202009-04-09 22:04:07 -070047 struct page **pages;
48 int nr_pages;
49 int page_array_size;
50 const char *vdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 struct timer_list timeout;
52};
53
David Woodhouse5658c762008-05-23 13:52:42 +010054#ifdef CONFIG_FW_LOADER
55extern struct builtin_fw __start_builtin_fw[];
56extern struct builtin_fw __end_builtin_fw[];
57#else /* Module case. Avoid ifdefs later; it'll all optimise out */
58static struct builtin_fw *__start_builtin_fw;
59static struct builtin_fw *__end_builtin_fw;
60#endif
61
Arjan van de Ven858119e2006-01-14 13:20:43 -080062static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070063fw_load_abort(struct firmware_priv *fw_priv)
64{
65 set_bit(FW_STATUS_ABORT, &fw_priv->status);
66 wmb();
67 complete(&fw_priv->completion);
68}
69
70static ssize_t
Andi Kleen28812fe2010-01-05 12:48:07 +010071firmware_timeout_show(struct class *class,
72 struct class_attribute *attr,
73 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 return sprintf(buf, "%d\n", loading_timeout);
76}
77
78/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -080079 * firmware_timeout_store - set number of seconds to wait for firmware
80 * @class: device class pointer
81 * @buf: buffer to scan for timeout value
82 * @count: number of bytes in @buf
83 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -080085 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * firmware will be provided.
87 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -080088 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 **/
90static ssize_t
Andi Kleen28812fe2010-01-05 12:48:07 +010091firmware_timeout_store(struct class *class,
92 struct class_attribute *attr,
93 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -070096 if (loading_timeout < 0)
97 loading_timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return count;
99}
100
101static CLASS_ATTR(timeout, 0644, firmware_timeout_show, firmware_timeout_store);
102
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700103static void fw_dev_release(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Kay Sievers7eff2e72007-08-14 15:15:12 +0200105static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700107 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Kay Sievers7eff2e72007-08-14 15:15:12 +0200109 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200111 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700112 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 return 0;
115}
116
Adrian Bunk1b81d662006-05-20 15:00:16 -0700117static struct class firmware_class = {
118 .name = "firmware",
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700119 .dev_uevent = firmware_uevent,
120 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700121};
122
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700123static ssize_t firmware_loading_show(struct device *dev,
124 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700126 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
128 return sprintf(buf, "%d\n", loading);
129}
130
David Woodhouse6e03a202009-04-09 22:04:07 -0700131/* Some architectures don't have PAGE_KERNEL_RO */
132#ifndef PAGE_KERNEL_RO
133#define PAGE_KERNEL_RO PAGE_KERNEL
134#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800136 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700137 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800138 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800139 * @buf: buffer to scan for loading control value
140 * @count: number of bytes in @buf
141 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 * The relevant values are:
143 *
144 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800145 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 * -1: Conclude the load with an error and discard any written data.
147 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700148static ssize_t firmware_loading_store(struct device *dev,
149 struct device_attribute *attr,
150 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700152 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700154 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 switch (loading) {
157 case 1:
Laura Garciacad1e552006-05-23 23:22:38 +0200158 mutex_lock(&fw_lock);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700159 if (!fw_priv->fw) {
Laura Garciacad1e552006-05-23 23:22:38 +0200160 mutex_unlock(&fw_lock);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700161 break;
162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 vfree(fw_priv->fw->data);
164 fw_priv->fw->data = NULL;
David Woodhouse6e03a202009-04-09 22:04:07 -0700165 for (i = 0; i < fw_priv->nr_pages; i++)
166 __free_page(fw_priv->pages[i]);
167 kfree(fw_priv->pages);
168 fw_priv->pages = NULL;
169 fw_priv->page_array_size = 0;
170 fw_priv->nr_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 fw_priv->fw->size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 set_bit(FW_STATUS_LOADING, &fw_priv->status);
Laura Garciacad1e552006-05-23 23:22:38 +0200173 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 break;
175 case 0:
176 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700177 vfree(fw_priv->fw->data);
178 fw_priv->fw->data = vmap(fw_priv->pages,
179 fw_priv->nr_pages,
180 0, PAGE_KERNEL_RO);
181 if (!fw_priv->fw->data) {
182 dev_err(dev, "%s: vmap() failed\n", __func__);
183 goto err;
184 }
185 /* Pages will be freed by vfree() */
David Woodhouse6e03a202009-04-09 22:04:07 -0700186 fw_priv->page_array_size = 0;
187 fw_priv->nr_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 complete(&fw_priv->completion);
189 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
190 break;
191 }
192 /* fallthrough */
193 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700194 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 /* fallthrough */
196 case -1:
David Woodhouse6e03a202009-04-09 22:04:07 -0700197 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 fw_load_abort(fw_priv);
199 break;
200 }
201
202 return count;
203}
204
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700205static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800208firmware_data_read(struct kobject *kobj, struct bin_attribute *bin_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 char *buffer, loff_t offset, size_t count)
210{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700211 struct device *dev = to_dev(kobj);
212 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 struct firmware *fw;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700214 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Laura Garciacad1e552006-05-23 23:22:38 +0200216 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700218 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 ret_count = -ENODEV;
220 goto out;
221 }
Jiri Slaby308975f2009-06-21 23:57:31 +0200222 if (offset > fw->size) {
223 ret_count = 0;
224 goto out;
225 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700226 if (count > fw->size - offset)
227 count = fw->size - offset;
228
229 ret_count = count;
230
231 while (count) {
232 void *page_data;
233 int page_nr = offset >> PAGE_SHIFT;
234 int page_ofs = offset & (PAGE_SIZE-1);
235 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
236
237 page_data = kmap(fw_priv->pages[page_nr]);
238
239 memcpy(buffer, page_data + page_ofs, page_cnt);
240
241 kunmap(fw_priv->pages[page_nr]);
242 buffer += page_cnt;
243 offset += page_cnt;
244 count -= page_cnt;
245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246out:
Laura Garciacad1e552006-05-23 23:22:38 +0200247 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return ret_count;
249}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251static int
252fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
253{
David Woodhouse6e03a202009-04-09 22:04:07 -0700254 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
David Woodhouse6e03a202009-04-09 22:04:07 -0700256 /* If the array of pages is too small, grow it... */
257 if (fw_priv->page_array_size < pages_needed) {
258 int new_array_size = max(pages_needed,
259 fw_priv->page_array_size * 2);
260 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
David Woodhouse6e03a202009-04-09 22:04:07 -0700262 new_pages = kmalloc(new_array_size * sizeof(void *),
263 GFP_KERNEL);
264 if (!new_pages) {
265 fw_load_abort(fw_priv);
266 return -ENOMEM;
267 }
268 memcpy(new_pages, fw_priv->pages,
269 fw_priv->page_array_size * sizeof(void *));
270 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
271 (new_array_size - fw_priv->page_array_size));
272 kfree(fw_priv->pages);
273 fw_priv->pages = new_pages;
274 fw_priv->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700276
277 while (fw_priv->nr_pages < pages_needed) {
278 fw_priv->pages[fw_priv->nr_pages] =
279 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
280
281 if (!fw_priv->pages[fw_priv->nr_pages]) {
282 fw_load_abort(fw_priv);
283 return -ENOMEM;
284 }
285 fw_priv->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return 0;
288}
289
290/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800291 * firmware_data_write - write method for firmware
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700292 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700293 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800294 * @buffer: buffer being written
295 * @offset: buffer offset for write in total data store area
296 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800298 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 * the driver as a firmware image.
300 **/
301static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800302firmware_data_write(struct kobject *kobj, struct bin_attribute *bin_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 char *buffer, loff_t offset, size_t count)
304{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700305 struct device *dev = to_dev(kobj);
306 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 struct firmware *fw;
308 ssize_t retval;
309
310 if (!capable(CAP_SYS_RAWIO))
311 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700312
Laura Garciacad1e552006-05-23 23:22:38 +0200313 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700315 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 retval = -ENODEV;
317 goto out;
318 }
319 retval = fw_realloc_buffer(fw_priv, offset + count);
320 if (retval)
321 goto out;
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700324
325 while (count) {
326 void *page_data;
327 int page_nr = offset >> PAGE_SHIFT;
328 int page_ofs = offset & (PAGE_SIZE - 1);
329 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
330
331 page_data = kmap(fw_priv->pages[page_nr]);
332
333 memcpy(page_data + page_ofs, buffer, page_cnt);
334
335 kunmap(fw_priv->pages[page_nr]);
336 buffer += page_cnt;
337 offset += page_cnt;
338 count -= page_cnt;
339 }
340
341 fw->size = max_t(size_t, offset, fw->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342out:
Laura Garciacad1e552006-05-23 23:22:38 +0200343 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return retval;
345}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347static struct bin_attribute firmware_attr_data_tmpl = {
Tejun Heo7b595752007-06-14 03:45:17 +0900348 .attr = {.name = "data", .mode = 0644},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 .size = 0,
350 .read = firmware_data_read,
351 .write = firmware_data_write,
352};
353
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700354static void fw_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700356 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
David Woodhouse6e03a202009-04-09 22:04:07 -0700357 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
David Woodhouse6e03a202009-04-09 22:04:07 -0700359 for (i = 0; i < fw_priv->nr_pages; i++)
360 __free_page(fw_priv->pages[i]);
361 kfree(fw_priv->pages);
Samuel Ortiz976821d2009-05-27 00:49:31 +0200362 kfree(fw_priv->fw_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 kfree(fw_priv);
Catalin Marinas0f2f2222009-07-08 11:17:40 +0100364 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 module_put(THIS_MODULE);
367}
368
369static void
370firmware_class_timeout(u_long data)
371{
372 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
373 fw_load_abort(fw_priv);
374}
375
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700376static int fw_register_device(struct device **dev_p, const char *fw_name,
377 struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
379 int retval;
Jiri Slaby4aed0642005-09-13 01:25:01 -0700380 struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 GFP_KERNEL);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700382 struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700384 *dev_p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700386 if (!fw_priv || !f_dev) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700387 dev_err(device, "%s: kmalloc failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 retval = -ENOMEM;
389 goto error_kfree;
390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 init_completion(&fw_priv->completion);
393 fw_priv->attr_data = firmware_attr_data_tmpl;
Samuel Ortiz976821d2009-05-27 00:49:31 +0200394 fw_priv->fw_id = kstrdup(fw_name, GFP_KERNEL);
395 if (!fw_priv->fw_id) {
396 dev_err(device, "%s: Firmware name allocation failed\n",
397 __func__);
398 retval = -ENOMEM;
399 goto error_kfree;
400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 fw_priv->timeout.function = firmware_class_timeout;
403 fw_priv->timeout.data = (u_long) fw_priv;
404 init_timer(&fw_priv->timeout);
405
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700406 dev_set_name(f_dev, "%s", dev_name(device));
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700407 f_dev->parent = device;
408 f_dev->class = &firmware_class;
409 dev_set_drvdata(f_dev, fw_priv);
Ming Leif67f1292009-03-01 21:10:49 +0800410 dev_set_uevent_suppress(f_dev, 1);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700411 retval = device_register(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (retval) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700413 dev_err(device, "%s: device_register failed\n", __func__);
Ming Lei6acf70f2009-04-23 22:31:52 +0800414 put_device(f_dev);
Catalin Marinas0f2f2222009-07-08 11:17:40 +0100415 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700417 *dev_p = f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return 0;
419
420error_kfree:
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700421 kfree(f_dev);
Ming Lei6acf70f2009-04-23 22:31:52 +0800422 kfree(fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return retval;
424}
425
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700426static int fw_setup_device(struct firmware *fw, struct device **dev_p,
427 const char *fw_name, struct device *device,
428 int uevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700430 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 struct firmware_priv *fw_priv;
432 int retval;
433
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700434 *dev_p = NULL;
435 retval = fw_register_device(&f_dev, fw_name, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (retval)
437 goto out;
438
439 /* Need to pin this module until class device is destroyed */
440 __module_get(THIS_MODULE);
441
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700442 fw_priv = dev_get_drvdata(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 fw_priv->fw = fw;
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700445 retval = sysfs_create_bin_file(&f_dev->kobj, &fw_priv->attr_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (retval) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700447 dev_err(device, "%s: sysfs_create_bin_file failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 goto error_unreg;
449 }
450
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700451 retval = device_create_file(f_dev, &dev_attr_loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (retval) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700453 dev_err(device, "%s: device_create_file failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 goto error_unreg;
455 }
456
Kay Sievers312c0042005-11-16 09:00:00 +0100457 if (uevent)
Ming Leif67f1292009-03-01 21:10:49 +0800458 dev_set_uevent_suppress(f_dev, 0);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700459 *dev_p = f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 goto out;
461
462error_unreg:
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700463 device_unregister(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464out:
465 return retval;
466}
467
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700468static int
469_request_firmware(const struct firmware **firmware_p, const char *name,
Kay Sievers312c0042005-11-16 09:00:00 +0100470 struct device *device, int uevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700472 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 struct firmware_priv *fw_priv;
474 struct firmware *firmware;
David Woodhouse5658c762008-05-23 13:52:42 +0100475 struct builtin_fw *builtin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 int retval;
477
478 if (!firmware_p)
479 return -EINVAL;
480
Jiri Slaby4aed0642005-09-13 01:25:01 -0700481 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700483 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
484 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 retval = -ENOMEM;
486 goto out;
487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
David Woodhouse5658c762008-05-23 13:52:42 +0100489 for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
490 builtin++) {
491 if (strcmp(name, builtin->name))
492 continue;
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700493 dev_info(device, "firmware: using built-in firmware %s\n",
494 name);
David Woodhouse5658c762008-05-23 13:52:42 +0100495 firmware->size = builtin->size;
496 firmware->data = builtin->data;
497 return 0;
498 }
499
500 if (uevent)
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700501 dev_info(device, "firmware: requesting %s\n", name);
David Woodhouse5658c762008-05-23 13:52:42 +0100502
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700503 retval = fw_setup_device(firmware, &f_dev, name, device, uevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (retval)
505 goto error_kfree_fw;
506
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700507 fw_priv = dev_get_drvdata(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Kay Sievers312c0042005-11-16 09:00:00 +0100509 if (uevent) {
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700510 if (loading_timeout > 0) {
511 fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
512 add_timer(&fw_priv->timeout);
513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700515 kobject_uevent(&f_dev->kobj, KOBJ_ADD);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700516 wait_for_completion(&fw_priv->completion);
517 set_bit(FW_STATUS_DONE, &fw_priv->status);
518 del_timer_sync(&fw_priv->timeout);
519 } else
520 wait_for_completion(&fw_priv->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Laura Garciacad1e552006-05-23 23:22:38 +0200522 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
524 retval = -ENOENT;
525 release_firmware(fw_priv->fw);
526 *firmware_p = NULL;
527 }
528 fw_priv->fw = NULL;
Laura Garciacad1e552006-05-23 23:22:38 +0200529 mutex_unlock(&fw_lock);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700530 device_unregister(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 goto out;
532
533error_kfree_fw:
534 kfree(firmware);
535 *firmware_p = NULL;
536out:
537 return retval;
538}
539
540/**
Kay Sievers312c0042005-11-16 09:00:00 +0100541 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800542 * @firmware_p: pointer to firmware image
543 * @name: name of firmware file
544 * @device: device for which firmware is being loaded
545 *
546 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700547 * of @name for device @device.
548 *
549 * Should be called from user context where sleeping is allowed.
550 *
Kay Sievers312c0042005-11-16 09:00:00 +0100551 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700552 * should be distinctive enough not to be confused with any other
553 * firmware image for this or any other device.
554 **/
555int
556request_firmware(const struct firmware **firmware_p, const char *name,
557 struct device *device)
558{
Kay Sievers312c0042005-11-16 09:00:00 +0100559 int uevent = 1;
560 return _request_firmware(firmware_p, name, device, uevent);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700561}
562
563/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800565 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 **/
567void
568release_firmware(const struct firmware *fw)
569{
David Woodhouse5658c762008-05-23 13:52:42 +0100570 struct builtin_fw *builtin;
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (fw) {
David Woodhouse5658c762008-05-23 13:52:42 +0100573 for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
574 builtin++) {
575 if (fw->data == builtin->data)
576 goto free_fw;
577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 vfree(fw->data);
David Woodhouse5658c762008-05-23 13:52:42 +0100579 free_fw:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 kfree(fw);
581 }
582}
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584/* Async support */
585struct firmware_work {
586 struct work_struct work;
587 struct module *module;
588 const char *name;
589 struct device *device;
590 void *context;
591 void (*cont)(const struct firmware *fw, void *context);
Kay Sievers312c0042005-11-16 09:00:00 +0100592 int uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593};
594
595static int
596request_firmware_work_func(void *arg)
597{
598 struct firmware_work *fw_work = arg;
599 const struct firmware *fw;
matthieu castet113fab12005-11-13 16:07:39 -0800600 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (!arg) {
602 WARN_ON(1);
603 return 0;
604 }
matthieu castet113fab12005-11-13 16:07:39 -0800605 ret = _request_firmware(&fw, fw_work->name, fw_work->device,
Kay Sievers312c0042005-11-16 09:00:00 +0100606 fw_work->uevent);
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100607
608 fw_work->cont(fw, fw_work->context);
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 module_put(fw_work->module);
611 kfree(fw_work);
matthieu castet113fab12005-11-13 16:07:39 -0800612 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
615/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000616 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800617 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +0100618 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800619 * is non-zero else the firmware copy must be done manually.
620 * @name: name of firmware file
621 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100622 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800623 * @context: will be passed over to @cont, and
624 * @fw may be %NULL if firmware request fails.
625 * @cont: function will be called asynchronously when the firmware
626 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 *
Ming Lei7fcab092009-05-29 11:33:19 +0800628 * Asynchronous variant of request_firmware() for user contexts where
629 * it is not possible to sleep for long time. It can't be called
630 * in atomic contexts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 **/
632int
633request_firmware_nowait(
Kay Sievers312c0042005-11-16 09:00:00 +0100634 struct module *module, int uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100635 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 void (*cont)(const struct firmware *fw, void *context))
637{
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700638 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100640 gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 if (!fw_work)
643 return -ENOMEM;
644 if (!try_module_get(module)) {
645 kfree(fw_work);
646 return -EFAULT;
647 }
648
649 *fw_work = (struct firmware_work) {
650 .module = module,
651 .name = name,
652 .device = device,
653 .context = context,
654 .cont = cont,
Kay Sievers312c0042005-11-16 09:00:00 +0100655 .uevent = uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 };
657
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700658 task = kthread_run(request_firmware_work_func, fw_work,
659 "firmware/%s", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700661 if (IS_ERR(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 fw_work->cont(NULL, fw_work->context);
matthieu castet113fab12005-11-13 16:07:39 -0800663 module_put(fw_work->module);
664 kfree(fw_work);
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700665 return PTR_ERR(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
667 return 0;
668}
669
670static int __init
671firmware_class_init(void)
672{
673 int error;
674 error = class_register(&firmware_class);
675 if (error) {
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800676 printk(KERN_ERR "%s: class_register failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return error;
678 }
679 error = class_create_file(&firmware_class, &class_attr_timeout);
680 if (error) {
681 printk(KERN_ERR "%s: class_create_file failed\n",
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800682 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 class_unregister(&firmware_class);
684 }
685 return error;
686
687}
688static void __exit
689firmware_class_exit(void)
690{
691 class_unregister(&firmware_class);
692}
693
Shaohua Lia30a6a22006-09-27 01:50:52 -0700694fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695module_exit(firmware_class_exit);
696
697EXPORT_SYMBOL(release_firmware);
698EXPORT_SYMBOL(request_firmware);
699EXPORT_SYMBOL(request_firmware_nowait);