blob: d351e773f439d159565cf0f467805b6e5a6dadbd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * firmware_class.c - Multi purpose firmware loading support
3 *
Markus Rechberger87d37a42007-06-04 18:45:44 +02004 * Copyright (c) 2003 Manuel Estrada Sainz
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Please see Documentation/firmware_class/ for more information.
7 *
8 */
9
Randy.Dunlapc59ede72006-01-11 12:17:46 -080010#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/device.h>
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/timer.h>
15#include <linux/vmalloc.h>
16#include <linux/interrupt.h>
17#include <linux/bitops.h>
Laura Garciacad1e552006-05-23 23:22:38 +020018#include <linux/mutex.h>
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -070019#include <linux/kthread.h>
David Woodhouse6e03a202009-04-09 22:04:07 -070020#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/firmware.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -070024#define to_dev(obj) container_of(obj, struct device, kobj)
25
Markus Rechberger87d37a42007-06-04 18:45:44 +020026MODULE_AUTHOR("Manuel Estrada Sainz");
Linus Torvalds1da177e2005-04-16 15:20:36 -070027MODULE_DESCRIPTION("Multi purpose firmware loading support");
28MODULE_LICENSE("GPL");
29
30enum {
31 FW_STATUS_LOADING,
32 FW_STATUS_DONE,
33 FW_STATUS_ABORT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070034};
35
Dave Jones2f651682007-01-25 15:56:15 -050036static int loading_timeout = 60; /* In seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/* fw_lock could be moved to 'struct firmware_priv' but since it is just
39 * guarding for corner cases a global lock should be OK */
Laura Garciacad1e552006-05-23 23:22:38 +020040static DEFINE_MUTEX(fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42struct firmware_priv {
Samuel Ortiz976821d2009-05-27 00:49:31 +020043 char *fw_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 struct completion completion;
45 struct bin_attribute attr_data;
46 struct firmware *fw;
47 unsigned long status;
David Woodhouse6e03a202009-04-09 22:04:07 -070048 struct page **pages;
49 int nr_pages;
50 int page_array_size;
51 const char *vdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 struct timer_list timeout;
Johannes Berge9045f92010-03-29 17:57:20 +020053 bool nowait;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054};
55
David Woodhouse5658c762008-05-23 13:52:42 +010056#ifdef CONFIG_FW_LOADER
57extern struct builtin_fw __start_builtin_fw[];
58extern struct builtin_fw __end_builtin_fw[];
59#else /* Module case. Avoid ifdefs later; it'll all optimise out */
60static struct builtin_fw *__start_builtin_fw;
61static struct builtin_fw *__end_builtin_fw;
62#endif
63
Arjan van de Ven858119e2006-01-14 13:20:43 -080064static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070065fw_load_abort(struct firmware_priv *fw_priv)
66{
67 set_bit(FW_STATUS_ABORT, &fw_priv->status);
68 wmb();
69 complete(&fw_priv->completion);
70}
71
72static ssize_t
Andi Kleen28812fe2010-01-05 12:48:07 +010073firmware_timeout_show(struct class *class,
74 struct class_attribute *attr,
75 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 return sprintf(buf, "%d\n", loading_timeout);
78}
79
80/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -080081 * firmware_timeout_store - set number of seconds to wait for firmware
82 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -080083 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -080084 * @buf: buffer to scan for timeout value
85 * @count: number of bytes in @buf
86 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -080088 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * firmware will be provided.
90 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -080091 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 **/
93static ssize_t
Andi Kleen28812fe2010-01-05 12:48:07 +010094firmware_timeout_store(struct class *class,
95 struct class_attribute *attr,
96 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -070099 if (loading_timeout < 0)
100 loading_timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return count;
102}
103
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800104static struct class_attribute firmware_class_attrs[] = {
105 __ATTR(timeout, S_IWUSR | S_IRUGO,
106 firmware_timeout_show, firmware_timeout_store),
107 __ATTR_NULL
108};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800110static void fw_dev_release(struct device *dev)
111{
112 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
113 int i;
114
115 for (i = 0; i < fw_priv->nr_pages; i++)
116 __free_page(fw_priv->pages[i]);
117 kfree(fw_priv->pages);
118 kfree(fw_priv->fw_id);
119 kfree(fw_priv);
120 kfree(dev);
121
122 module_put(THIS_MODULE);
123}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Kay Sievers7eff2e72007-08-14 15:15:12 +0200125static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
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
Kay Sievers7eff2e72007-08-14 15:15:12 +0200129 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200131 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700132 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200133 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
134 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 return 0;
137}
138
Adrian Bunk1b81d662006-05-20 15:00:16 -0700139static struct class firmware_class = {
140 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800141 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700142 .dev_uevent = firmware_uevent,
143 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700144};
145
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700146static ssize_t firmware_loading_show(struct device *dev,
147 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700149 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
151 return sprintf(buf, "%d\n", loading);
152}
153
David Woodhousedd336c52010-05-02 11:21:21 +0300154static void firmware_free_data(const struct firmware *fw)
155{
156 int i;
157 vunmap(fw->data);
158 if (fw->pages) {
159 for (i = 0; i < PFN_UP(fw->size); i++)
160 __free_page(fw->pages[i]);
161 kfree(fw->pages);
162 }
163}
164
David Woodhouse6e03a202009-04-09 22:04:07 -0700165/* Some architectures don't have PAGE_KERNEL_RO */
166#ifndef PAGE_KERNEL_RO
167#define PAGE_KERNEL_RO PAGE_KERNEL
168#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800170 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700171 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800172 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800173 * @buf: buffer to scan for loading control value
174 * @count: number of bytes in @buf
175 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 * The relevant values are:
177 *
178 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800179 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * -1: Conclude the load with an error and discard any written data.
181 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700182static ssize_t firmware_loading_store(struct device *dev,
183 struct device_attribute *attr,
184 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700186 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700188 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 switch (loading) {
191 case 1:
Laura Garciacad1e552006-05-23 23:22:38 +0200192 mutex_lock(&fw_lock);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700193 if (!fw_priv->fw) {
Laura Garciacad1e552006-05-23 23:22:38 +0200194 mutex_unlock(&fw_lock);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700195 break;
196 }
David Woodhousedd336c52010-05-02 11:21:21 +0300197 firmware_free_data(fw_priv->fw);
198 memset(fw_priv->fw, 0, sizeof(struct firmware));
199 /* If the pages are not owned by 'struct firmware' */
David Woodhouse6e03a202009-04-09 22:04:07 -0700200 for (i = 0; i < fw_priv->nr_pages; i++)
201 __free_page(fw_priv->pages[i]);
202 kfree(fw_priv->pages);
203 fw_priv->pages = NULL;
204 fw_priv->page_array_size = 0;
205 fw_priv->nr_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 set_bit(FW_STATUS_LOADING, &fw_priv->status);
Laura Garciacad1e552006-05-23 23:22:38 +0200207 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 break;
209 case 0:
210 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
David Woodhousedd336c52010-05-02 11:21:21 +0300211 vunmap(fw_priv->fw->data);
David Woodhouse6e03a202009-04-09 22:04:07 -0700212 fw_priv->fw->data = vmap(fw_priv->pages,
213 fw_priv->nr_pages,
214 0, PAGE_KERNEL_RO);
215 if (!fw_priv->fw->data) {
216 dev_err(dev, "%s: vmap() failed\n", __func__);
217 goto err;
218 }
David Woodhousedd336c52010-05-02 11:21:21 +0300219 /* Pages are now owned by 'struct firmware' */
220 fw_priv->fw->pages = fw_priv->pages;
221 fw_priv->pages = NULL;
222
David Woodhouse6e03a202009-04-09 22:04:07 -0700223 fw_priv->page_array_size = 0;
224 fw_priv->nr_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 complete(&fw_priv->completion);
226 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
227 break;
228 }
229 /* fallthrough */
230 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700231 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 /* fallthrough */
233 case -1:
David Woodhouse6e03a202009-04-09 22:04:07 -0700234 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 fw_load_abort(fw_priv);
236 break;
237 }
238
239 return count;
240}
241
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700242static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800245firmware_data_read(struct kobject *kobj, struct bin_attribute *bin_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 char *buffer, loff_t offset, size_t count)
247{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700248 struct device *dev = to_dev(kobj);
249 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 struct firmware *fw;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700251 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Laura Garciacad1e552006-05-23 23:22:38 +0200253 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700255 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 ret_count = -ENODEV;
257 goto out;
258 }
Jiri Slaby308975f2009-06-21 23:57:31 +0200259 if (offset > fw->size) {
260 ret_count = 0;
261 goto out;
262 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700263 if (count > fw->size - offset)
264 count = fw->size - offset;
265
266 ret_count = count;
267
268 while (count) {
269 void *page_data;
270 int page_nr = offset >> PAGE_SHIFT;
271 int page_ofs = offset & (PAGE_SIZE-1);
272 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
273
274 page_data = kmap(fw_priv->pages[page_nr]);
275
276 memcpy(buffer, page_data + page_ofs, page_cnt);
277
278 kunmap(fw_priv->pages[page_nr]);
279 buffer += page_cnt;
280 offset += page_cnt;
281 count -= page_cnt;
282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283out:
Laura Garciacad1e552006-05-23 23:22:38 +0200284 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return ret_count;
286}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288static int
289fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
290{
David Woodhouse6e03a202009-04-09 22:04:07 -0700291 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
David Woodhouse6e03a202009-04-09 22:04:07 -0700293 /* If the array of pages is too small, grow it... */
294 if (fw_priv->page_array_size < pages_needed) {
295 int new_array_size = max(pages_needed,
296 fw_priv->page_array_size * 2);
297 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
David Woodhouse6e03a202009-04-09 22:04:07 -0700299 new_pages = kmalloc(new_array_size * sizeof(void *),
300 GFP_KERNEL);
301 if (!new_pages) {
302 fw_load_abort(fw_priv);
303 return -ENOMEM;
304 }
305 memcpy(new_pages, fw_priv->pages,
306 fw_priv->page_array_size * sizeof(void *));
307 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
308 (new_array_size - fw_priv->page_array_size));
309 kfree(fw_priv->pages);
310 fw_priv->pages = new_pages;
311 fw_priv->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700313
314 while (fw_priv->nr_pages < pages_needed) {
315 fw_priv->pages[fw_priv->nr_pages] =
316 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
317
318 if (!fw_priv->pages[fw_priv->nr_pages]) {
319 fw_load_abort(fw_priv);
320 return -ENOMEM;
321 }
322 fw_priv->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return 0;
325}
326
327/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800328 * firmware_data_write - write method for firmware
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700329 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700330 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800331 * @buffer: buffer being written
332 * @offset: buffer offset for write in total data store area
333 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800335 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 * the driver as a firmware image.
337 **/
338static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800339firmware_data_write(struct kobject *kobj, struct bin_attribute *bin_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 char *buffer, loff_t offset, size_t count)
341{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700342 struct device *dev = to_dev(kobj);
343 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 struct firmware *fw;
345 ssize_t retval;
346
347 if (!capable(CAP_SYS_RAWIO))
348 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700349
Laura Garciacad1e552006-05-23 23:22:38 +0200350 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700352 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 retval = -ENODEV;
354 goto out;
355 }
356 retval = fw_realloc_buffer(fw_priv, offset + count);
357 if (retval)
358 goto out;
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700361
362 while (count) {
363 void *page_data;
364 int page_nr = offset >> PAGE_SHIFT;
365 int page_ofs = offset & (PAGE_SIZE - 1);
366 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
367
368 page_data = kmap(fw_priv->pages[page_nr]);
369
370 memcpy(page_data + page_ofs, buffer, page_cnt);
371
372 kunmap(fw_priv->pages[page_nr]);
373 buffer += page_cnt;
374 offset += page_cnt;
375 count -= page_cnt;
376 }
377
378 fw->size = max_t(size_t, offset, fw->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379out:
Laura Garciacad1e552006-05-23 23:22:38 +0200380 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return retval;
382}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384static struct bin_attribute firmware_attr_data_tmpl = {
Tejun Heo7b595752007-06-14 03:45:17 +0900385 .attr = {.name = "data", .mode = 0644},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 .size = 0,
387 .read = firmware_data_read,
388 .write = firmware_data_write,
389};
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391static void
392firmware_class_timeout(u_long data)
393{
394 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
395 fw_load_abort(fw_priv);
396}
397
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700398static int fw_register_device(struct device **dev_p, const char *fw_name,
399 struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 int retval;
Jiri Slaby4aed0642005-09-13 01:25:01 -0700402 struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 GFP_KERNEL);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700404 struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700406 *dev_p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700408 if (!fw_priv || !f_dev) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700409 dev_err(device, "%s: kmalloc failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 retval = -ENOMEM;
411 goto error_kfree;
412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 init_completion(&fw_priv->completion);
415 fw_priv->attr_data = firmware_attr_data_tmpl;
Samuel Ortiz976821d2009-05-27 00:49:31 +0200416 fw_priv->fw_id = kstrdup(fw_name, GFP_KERNEL);
417 if (!fw_priv->fw_id) {
418 dev_err(device, "%s: Firmware name allocation failed\n",
419 __func__);
420 retval = -ENOMEM;
421 goto error_kfree;
422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 fw_priv->timeout.function = firmware_class_timeout;
425 fw_priv->timeout.data = (u_long) fw_priv;
426 init_timer(&fw_priv->timeout);
427
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700428 dev_set_name(f_dev, "%s", dev_name(device));
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700429 f_dev->parent = device;
430 f_dev->class = &firmware_class;
431 dev_set_drvdata(f_dev, fw_priv);
Ming Leif67f1292009-03-01 21:10:49 +0800432 dev_set_uevent_suppress(f_dev, 1);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700433 retval = device_register(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (retval) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700435 dev_err(device, "%s: device_register failed\n", __func__);
Ming Lei6acf70f2009-04-23 22:31:52 +0800436 put_device(f_dev);
Catalin Marinas0f2f2222009-07-08 11:17:40 +0100437 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700439 *dev_p = f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return 0;
441
442error_kfree:
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700443 kfree(f_dev);
Ming Lei6acf70f2009-04-23 22:31:52 +0800444 kfree(fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return retval;
446}
447
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700448static int fw_setup_device(struct firmware *fw, struct device **dev_p,
449 const char *fw_name, struct device *device,
Johannes Berge9045f92010-03-29 17:57:20 +0200450 int uevent, bool nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700452 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 struct firmware_priv *fw_priv;
454 int retval;
455
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700456 *dev_p = NULL;
457 retval = fw_register_device(&f_dev, fw_name, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (retval)
459 goto out;
460
461 /* Need to pin this module until class device is destroyed */
462 __module_get(THIS_MODULE);
463
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700464 fw_priv = dev_get_drvdata(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Johannes Berge9045f92010-03-29 17:57:20 +0200466 fw_priv->nowait = nowait;
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 fw_priv->fw = fw;
Jiri Kosinae1955ca2010-03-09 19:30:28 +0100469 sysfs_bin_attr_init(&fw_priv->attr_data);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700470 retval = sysfs_create_bin_file(&f_dev->kobj, &fw_priv->attr_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (retval) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700472 dev_err(device, "%s: sysfs_create_bin_file failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 goto error_unreg;
474 }
475
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700476 retval = device_create_file(f_dev, &dev_attr_loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (retval) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700478 dev_err(device, "%s: device_create_file failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 goto error_unreg;
480 }
481
Kay Sievers312c0042005-11-16 09:00:00 +0100482 if (uevent)
Ming Leif67f1292009-03-01 21:10:49 +0800483 dev_set_uevent_suppress(f_dev, 0);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700484 *dev_p = f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 goto out;
486
487error_unreg:
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700488 device_unregister(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489out:
490 return retval;
491}
492
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700493static int
494_request_firmware(const struct firmware **firmware_p, const char *name,
Johannes Berge9045f92010-03-29 17:57:20 +0200495 struct device *device, int uevent, bool nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700497 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 struct firmware_priv *fw_priv;
499 struct firmware *firmware;
David Woodhouse5658c762008-05-23 13:52:42 +0100500 struct builtin_fw *builtin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 int retval;
502
503 if (!firmware_p)
504 return -EINVAL;
505
Jiri Slaby4aed0642005-09-13 01:25:01 -0700506 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700508 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
509 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 retval = -ENOMEM;
511 goto out;
512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
David Woodhouse5658c762008-05-23 13:52:42 +0100514 for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
515 builtin++) {
516 if (strcmp(name, builtin->name))
517 continue;
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100518 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
David Woodhouse5658c762008-05-23 13:52:42 +0100519 firmware->size = builtin->size;
520 firmware->data = builtin->data;
521 return 0;
522 }
523
524 if (uevent)
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100525 dev_dbg(device, "firmware: requesting %s\n", name);
David Woodhouse5658c762008-05-23 13:52:42 +0100526
Johannes Berge9045f92010-03-29 17:57:20 +0200527 retval = fw_setup_device(firmware, &f_dev, name, device,
528 uevent, nowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (retval)
530 goto error_kfree_fw;
531
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700532 fw_priv = dev_get_drvdata(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Kay Sievers312c0042005-11-16 09:00:00 +0100534 if (uevent) {
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700535 if (loading_timeout > 0) {
536 fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
537 add_timer(&fw_priv->timeout);
538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700540 kobject_uevent(&f_dev->kobj, KOBJ_ADD);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700541 wait_for_completion(&fw_priv->completion);
542 set_bit(FW_STATUS_DONE, &fw_priv->status);
543 del_timer_sync(&fw_priv->timeout);
544 } else
545 wait_for_completion(&fw_priv->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Laura Garciacad1e552006-05-23 23:22:38 +0200547 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
549 retval = -ENOENT;
550 release_firmware(fw_priv->fw);
551 *firmware_p = NULL;
552 }
553 fw_priv->fw = NULL;
Laura Garciacad1e552006-05-23 23:22:38 +0200554 mutex_unlock(&fw_lock);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700555 device_unregister(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 goto out;
557
558error_kfree_fw:
559 kfree(firmware);
560 *firmware_p = NULL;
561out:
562 return retval;
563}
564
565/**
Kay Sievers312c0042005-11-16 09:00:00 +0100566 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800567 * @firmware_p: pointer to firmware image
568 * @name: name of firmware file
569 * @device: device for which firmware is being loaded
570 *
571 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700572 * of @name for device @device.
573 *
574 * Should be called from user context where sleeping is allowed.
575 *
Kay Sievers312c0042005-11-16 09:00:00 +0100576 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700577 * should be distinctive enough not to be confused with any other
578 * firmware image for this or any other device.
579 **/
580int
581request_firmware(const struct firmware **firmware_p, const char *name,
582 struct device *device)
583{
Kay Sievers312c0042005-11-16 09:00:00 +0100584 int uevent = 1;
Johannes Berge9045f92010-03-29 17:57:20 +0200585 return _request_firmware(firmware_p, name, device, uevent, false);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700586}
587
588/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800590 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 **/
592void
593release_firmware(const struct firmware *fw)
594{
David Woodhouse5658c762008-05-23 13:52:42 +0100595 struct builtin_fw *builtin;
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (fw) {
David Woodhouse5658c762008-05-23 13:52:42 +0100598 for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
599 builtin++) {
600 if (fw->data == builtin->data)
601 goto free_fw;
602 }
David Woodhousedd336c52010-05-02 11:21:21 +0300603 firmware_free_data(fw);
David Woodhouse5658c762008-05-23 13:52:42 +0100604 free_fw:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 kfree(fw);
606 }
607}
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609/* Async support */
610struct firmware_work {
611 struct work_struct work;
612 struct module *module;
613 const char *name;
614 struct device *device;
615 void *context;
616 void (*cont)(const struct firmware *fw, void *context);
Kay Sievers312c0042005-11-16 09:00:00 +0100617 int uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618};
619
620static int
621request_firmware_work_func(void *arg)
622{
623 struct firmware_work *fw_work = arg;
624 const struct firmware *fw;
matthieu castet113fab12005-11-13 16:07:39 -0800625 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (!arg) {
627 WARN_ON(1);
628 return 0;
629 }
matthieu castet113fab12005-11-13 16:07:39 -0800630 ret = _request_firmware(&fw, fw_work->name, fw_work->device,
Johannes Berge9045f92010-03-29 17:57:20 +0200631 fw_work->uevent, true);
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100632
633 fw_work->cont(fw, fw_work->context);
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 module_put(fw_work->module);
636 kfree(fw_work);
matthieu castet113fab12005-11-13 16:07:39 -0800637 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
640/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000641 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800642 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +0100643 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800644 * is non-zero else the firmware copy must be done manually.
645 * @name: name of firmware file
646 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100647 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800648 * @context: will be passed over to @cont, and
649 * @fw may be %NULL if firmware request fails.
650 * @cont: function will be called asynchronously when the firmware
651 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 *
Ming Lei7fcab092009-05-29 11:33:19 +0800653 * Asynchronous variant of request_firmware() for user contexts where
654 * it is not possible to sleep for long time. It can't be called
655 * in atomic contexts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 **/
657int
658request_firmware_nowait(
Kay Sievers312c0042005-11-16 09:00:00 +0100659 struct module *module, int uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100660 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 void (*cont)(const struct firmware *fw, void *context))
662{
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700663 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100665 gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 if (!fw_work)
668 return -ENOMEM;
669 if (!try_module_get(module)) {
670 kfree(fw_work);
671 return -EFAULT;
672 }
673
674 *fw_work = (struct firmware_work) {
675 .module = module,
676 .name = name,
677 .device = device,
678 .context = context,
679 .cont = cont,
Kay Sievers312c0042005-11-16 09:00:00 +0100680 .uevent = uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 };
682
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700683 task = kthread_run(request_firmware_work_func, fw_work,
684 "firmware/%s", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700686 if (IS_ERR(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 fw_work->cont(NULL, fw_work->context);
matthieu castet113fab12005-11-13 16:07:39 -0800688 module_put(fw_work->module);
689 kfree(fw_work);
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700690 return PTR_ERR(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692 return 0;
693}
694
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800695static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800697 return class_register(&firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800699
700static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
702 class_unregister(&firmware_class);
703}
704
Shaohua Lia30a6a22006-09-27 01:50:52 -0700705fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706module_exit(firmware_class_exit);
707
708EXPORT_SYMBOL(release_firmware);
709EXPORT_SYMBOL(request_firmware);
710EXPORT_SYMBOL(request_firmware_nowait);