blob: 8a267c4276291bb775e2590a6935825ae35e9305 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * firmware_class.c - Multi purpose firmware loading support
3 *
Markus Rechberger87d37a42007-06-04 18:45:44 +02004 * Copyright (c) 2003 Manuel Estrada Sainz
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Please see Documentation/firmware_class/ for more information.
7 *
8 */
9
Randy.Dunlapc59ede72006-01-11 12:17:46 -080010#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/device.h>
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/timer.h>
15#include <linux/vmalloc.h>
16#include <linux/interrupt.h>
17#include <linux/bitops.h>
Laura Garciacad1e552006-05-23 23:22:38 +020018#include <linux/mutex.h>
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -070019#include <linux/kthread.h>
David Woodhouse6e03a202009-04-09 22:04:07 -070020#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/firmware.h>
22#include "base.h"
23
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -070024#define to_dev(obj) container_of(obj, struct device, kobj)
25
Markus Rechberger87d37a42007-06-04 18:45:44 +020026MODULE_AUTHOR("Manuel Estrada Sainz");
Linus Torvalds1da177e2005-04-16 15:20:36 -070027MODULE_DESCRIPTION("Multi purpose firmware loading support");
28MODULE_LICENSE("GPL");
29
30enum {
31 FW_STATUS_LOADING,
32 FW_STATUS_DONE,
33 FW_STATUS_ABORT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070034};
35
Dave Jones2f651682007-01-25 15:56:15 -050036static int loading_timeout = 60; /* In seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/* fw_lock could be moved to 'struct firmware_priv' but since it is just
39 * guarding for corner cases a global lock should be OK */
Laura Garciacad1e552006-05-23 23:22:38 +020040static DEFINE_MUTEX(fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42struct firmware_priv {
43 char fw_id[FIRMWARE_NAME_MAX];
44 struct completion completion;
45 struct bin_attribute attr_data;
46 struct firmware *fw;
47 unsigned long status;
David Woodhouse6e03a202009-04-09 22:04:07 -070048 struct page **pages;
49 int nr_pages;
50 int page_array_size;
51 const char *vdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 struct timer_list timeout;
53};
54
David Woodhouse5658c762008-05-23 13:52:42 +010055#ifdef CONFIG_FW_LOADER
56extern struct builtin_fw __start_builtin_fw[];
57extern struct builtin_fw __end_builtin_fw[];
58#else /* Module case. Avoid ifdefs later; it'll all optimise out */
59static struct builtin_fw *__start_builtin_fw;
60static struct builtin_fw *__end_builtin_fw;
61#endif
62
Arjan van de Ven858119e2006-01-14 13:20:43 -080063static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070064fw_load_abort(struct firmware_priv *fw_priv)
65{
66 set_bit(FW_STATUS_ABORT, &fw_priv->status);
67 wmb();
68 complete(&fw_priv->completion);
69}
70
71static ssize_t
72firmware_timeout_show(struct class *class, char *buf)
73{
74 return sprintf(buf, "%d\n", loading_timeout);
75}
76
77/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -080078 * firmware_timeout_store - set number of seconds to wait for firmware
79 * @class: device class pointer
80 * @buf: buffer to scan for timeout value
81 * @count: number of bytes in @buf
82 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -080084 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * firmware will be provided.
86 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -080087 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 **/
89static ssize_t
90firmware_timeout_store(struct class *class, const char *buf, size_t count)
91{
92 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -070093 if (loading_timeout < 0)
94 loading_timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return count;
96}
97
98static CLASS_ATTR(timeout, 0644, firmware_timeout_show, firmware_timeout_store);
99
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700100static void fw_dev_release(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Kay Sievers7eff2e72007-08-14 15:15:12 +0200102static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700104 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Kay Sievers7eff2e72007-08-14 15:15:12 +0200106 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200108 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700109 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 return 0;
112}
113
Adrian Bunk1b81d662006-05-20 15:00:16 -0700114static struct class firmware_class = {
115 .name = "firmware",
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700116 .dev_uevent = firmware_uevent,
117 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700118};
119
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700120static ssize_t firmware_loading_show(struct device *dev,
121 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700123 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
125 return sprintf(buf, "%d\n", loading);
126}
127
David Woodhouse6e03a202009-04-09 22:04:07 -0700128/* Some architectures don't have PAGE_KERNEL_RO */
129#ifndef PAGE_KERNEL_RO
130#define PAGE_KERNEL_RO PAGE_KERNEL
131#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800133 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700134 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800135 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800136 * @buf: buffer to scan for loading control value
137 * @count: number of bytes in @buf
138 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 * The relevant values are:
140 *
141 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800142 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 * -1: Conclude the load with an error and discard any written data.
144 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700145static ssize_t firmware_loading_store(struct device *dev,
146 struct device_attribute *attr,
147 const char *buf, size_t count)
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 = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700151 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 switch (loading) {
154 case 1:
Laura Garciacad1e552006-05-23 23:22:38 +0200155 mutex_lock(&fw_lock);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700156 if (!fw_priv->fw) {
Laura Garciacad1e552006-05-23 23:22:38 +0200157 mutex_unlock(&fw_lock);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700158 break;
159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 vfree(fw_priv->fw->data);
161 fw_priv->fw->data = NULL;
David Woodhouse6e03a202009-04-09 22:04:07 -0700162 for (i = 0; i < fw_priv->nr_pages; i++)
163 __free_page(fw_priv->pages[i]);
164 kfree(fw_priv->pages);
165 fw_priv->pages = NULL;
166 fw_priv->page_array_size = 0;
167 fw_priv->nr_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 fw_priv->fw->size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 set_bit(FW_STATUS_LOADING, &fw_priv->status);
Laura Garciacad1e552006-05-23 23:22:38 +0200170 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 break;
172 case 0:
173 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700174 vfree(fw_priv->fw->data);
175 fw_priv->fw->data = vmap(fw_priv->pages,
176 fw_priv->nr_pages,
177 0, PAGE_KERNEL_RO);
178 if (!fw_priv->fw->data) {
179 dev_err(dev, "%s: vmap() failed\n", __func__);
180 goto err;
181 }
182 /* Pages will be freed by vfree() */
183 fw_priv->pages = NULL;
184 fw_priv->page_array_size = 0;
185 fw_priv->nr_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 complete(&fw_priv->completion);
187 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
188 break;
189 }
190 /* fallthrough */
191 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700192 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 /* fallthrough */
194 case -1:
David Woodhouse6e03a202009-04-09 22:04:07 -0700195 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 fw_load_abort(fw_priv);
197 break;
198 }
199
200 return count;
201}
202
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700203static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800206firmware_data_read(struct kobject *kobj, struct bin_attribute *bin_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 char *buffer, loff_t offset, size_t count)
208{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700209 struct device *dev = to_dev(kobj);
210 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 struct firmware *fw;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700212 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Laura Garciacad1e552006-05-23 23:22:38 +0200214 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700216 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 ret_count = -ENODEV;
218 goto out;
219 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700220 if (offset > fw->size)
221 return 0;
222 if (count > fw->size - offset)
223 count = fw->size - offset;
224
225 ret_count = count;
226
227 while (count) {
228 void *page_data;
229 int page_nr = offset >> PAGE_SHIFT;
230 int page_ofs = offset & (PAGE_SIZE-1);
231 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
232
233 page_data = kmap(fw_priv->pages[page_nr]);
234
235 memcpy(buffer, page_data + page_ofs, page_cnt);
236
237 kunmap(fw_priv->pages[page_nr]);
238 buffer += page_cnt;
239 offset += page_cnt;
240 count -= page_cnt;
241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242out:
Laura Garciacad1e552006-05-23 23:22:38 +0200243 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return ret_count;
245}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247static int
248fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
249{
David Woodhouse6e03a202009-04-09 22:04:07 -0700250 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
David Woodhouse6e03a202009-04-09 22:04:07 -0700252 /* If the array of pages is too small, grow it... */
253 if (fw_priv->page_array_size < pages_needed) {
254 int new_array_size = max(pages_needed,
255 fw_priv->page_array_size * 2);
256 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
David Woodhouse6e03a202009-04-09 22:04:07 -0700258 new_pages = kmalloc(new_array_size * sizeof(void *),
259 GFP_KERNEL);
260 if (!new_pages) {
261 fw_load_abort(fw_priv);
262 return -ENOMEM;
263 }
264 memcpy(new_pages, fw_priv->pages,
265 fw_priv->page_array_size * sizeof(void *));
266 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
267 (new_array_size - fw_priv->page_array_size));
268 kfree(fw_priv->pages);
269 fw_priv->pages = new_pages;
270 fw_priv->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700272
273 while (fw_priv->nr_pages < pages_needed) {
274 fw_priv->pages[fw_priv->nr_pages] =
275 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
276
277 if (!fw_priv->pages[fw_priv->nr_pages]) {
278 fw_load_abort(fw_priv);
279 return -ENOMEM;
280 }
281 fw_priv->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return 0;
284}
285
286/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800287 * firmware_data_write - write method for firmware
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700288 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700289 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800290 * @buffer: buffer being written
291 * @offset: buffer offset for write in total data store area
292 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800294 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 * the driver as a firmware image.
296 **/
297static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800298firmware_data_write(struct kobject *kobj, struct bin_attribute *bin_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 char *buffer, loff_t offset, size_t count)
300{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700301 struct device *dev = to_dev(kobj);
302 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 struct firmware *fw;
304 ssize_t retval;
305
306 if (!capable(CAP_SYS_RAWIO))
307 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700308
Laura Garciacad1e552006-05-23 23:22:38 +0200309 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700311 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 retval = -ENODEV;
313 goto out;
314 }
315 retval = fw_realloc_buffer(fw_priv, offset + count);
316 if (retval)
317 goto out;
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700320
321 while (count) {
322 void *page_data;
323 int page_nr = offset >> PAGE_SHIFT;
324 int page_ofs = offset & (PAGE_SIZE - 1);
325 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
326
327 page_data = kmap(fw_priv->pages[page_nr]);
328
329 memcpy(page_data + page_ofs, buffer, page_cnt);
330
331 kunmap(fw_priv->pages[page_nr]);
332 buffer += page_cnt;
333 offset += page_cnt;
334 count -= page_cnt;
335 }
336
337 fw->size = max_t(size_t, offset, fw->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338out:
Laura Garciacad1e552006-05-23 23:22:38 +0200339 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return retval;
341}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343static struct bin_attribute firmware_attr_data_tmpl = {
Tejun Heo7b595752007-06-14 03:45:17 +0900344 .attr = {.name = "data", .mode = 0644},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 .size = 0,
346 .read = firmware_data_read,
347 .write = firmware_data_write,
348};
349
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700350static void fw_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700352 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
David Woodhouse6e03a202009-04-09 22:04:07 -0700353 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
David Woodhouse6e03a202009-04-09 22:04:07 -0700355 for (i = 0; i < fw_priv->nr_pages; i++)
356 __free_page(fw_priv->pages[i]);
357 kfree(fw_priv->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 kfree(fw_priv);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700359 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 module_put(THIS_MODULE);
362}
363
364static void
365firmware_class_timeout(u_long data)
366{
367 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
368 fw_load_abort(fw_priv);
369}
370
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700371static int fw_register_device(struct device **dev_p, const char *fw_name,
372 struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 int retval;
Jiri Slaby4aed0642005-09-13 01:25:01 -0700375 struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 GFP_KERNEL);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700377 struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700379 *dev_p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700381 if (!fw_priv || !f_dev) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700382 dev_err(device, "%s: kmalloc failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 retval = -ENOMEM;
384 goto error_kfree;
385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 init_completion(&fw_priv->completion);
388 fw_priv->attr_data = firmware_attr_data_tmpl;
389 strlcpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX);
390
391 fw_priv->timeout.function = firmware_class_timeout;
392 fw_priv->timeout.data = (u_long) fw_priv;
393 init_timer(&fw_priv->timeout);
394
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100395 dev_set_name(f_dev, dev_name(device));
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700396 f_dev->parent = device;
397 f_dev->class = &firmware_class;
398 dev_set_drvdata(f_dev, fw_priv);
Ming Leif67f1292009-03-01 21:10:49 +0800399 dev_set_uevent_suppress(f_dev, 1);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700400 retval = device_register(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (retval) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700402 dev_err(device, "%s: device_register failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 goto error_kfree;
404 }
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700405 *dev_p = f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return 0;
407
408error_kfree:
409 kfree(fw_priv);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700410 kfree(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return retval;
412}
413
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700414static int fw_setup_device(struct firmware *fw, struct device **dev_p,
415 const char *fw_name, struct device *device,
416 int uevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700418 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 struct firmware_priv *fw_priv;
420 int retval;
421
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700422 *dev_p = NULL;
423 retval = fw_register_device(&f_dev, fw_name, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (retval)
425 goto out;
426
427 /* Need to pin this module until class device is destroyed */
428 __module_get(THIS_MODULE);
429
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700430 fw_priv = dev_get_drvdata(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 fw_priv->fw = fw;
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700433 retval = sysfs_create_bin_file(&f_dev->kobj, &fw_priv->attr_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (retval) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700435 dev_err(device, "%s: sysfs_create_bin_file failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 goto error_unreg;
437 }
438
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700439 retval = device_create_file(f_dev, &dev_attr_loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (retval) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700441 dev_err(device, "%s: device_create_file failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 goto error_unreg;
443 }
444
Kay Sievers312c0042005-11-16 09:00:00 +0100445 if (uevent)
Ming Leif67f1292009-03-01 21:10:49 +0800446 dev_set_uevent_suppress(f_dev, 0);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700447 *dev_p = f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 goto out;
449
450error_unreg:
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700451 device_unregister(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452out:
453 return retval;
454}
455
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700456static int
457_request_firmware(const struct firmware **firmware_p, const char *name,
Kay Sievers312c0042005-11-16 09:00:00 +0100458 struct device *device, int uevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700460 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 struct firmware_priv *fw_priv;
462 struct firmware *firmware;
David Woodhouse5658c762008-05-23 13:52:42 +0100463 struct builtin_fw *builtin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 int retval;
465
466 if (!firmware_p)
467 return -EINVAL;
468
Jiri Slaby4aed0642005-09-13 01:25:01 -0700469 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700471 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
472 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 retval = -ENOMEM;
474 goto out;
475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
David Woodhouse5658c762008-05-23 13:52:42 +0100477 for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
478 builtin++) {
479 if (strcmp(name, builtin->name))
480 continue;
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700481 dev_info(device, "firmware: using built-in firmware %s\n",
482 name);
David Woodhouse5658c762008-05-23 13:52:42 +0100483 firmware->size = builtin->size;
484 firmware->data = builtin->data;
485 return 0;
486 }
487
488 if (uevent)
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700489 dev_info(device, "firmware: requesting %s\n", name);
David Woodhouse5658c762008-05-23 13:52:42 +0100490
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700491 retval = fw_setup_device(firmware, &f_dev, name, device, uevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (retval)
493 goto error_kfree_fw;
494
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700495 fw_priv = dev_get_drvdata(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Kay Sievers312c0042005-11-16 09:00:00 +0100497 if (uevent) {
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700498 if (loading_timeout > 0) {
499 fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
500 add_timer(&fw_priv->timeout);
501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700503 kobject_uevent(&f_dev->kobj, KOBJ_ADD);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700504 wait_for_completion(&fw_priv->completion);
505 set_bit(FW_STATUS_DONE, &fw_priv->status);
506 del_timer_sync(&fw_priv->timeout);
507 } else
508 wait_for_completion(&fw_priv->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Laura Garciacad1e552006-05-23 23:22:38 +0200510 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
512 retval = -ENOENT;
513 release_firmware(fw_priv->fw);
514 *firmware_p = NULL;
515 }
516 fw_priv->fw = NULL;
Laura Garciacad1e552006-05-23 23:22:38 +0200517 mutex_unlock(&fw_lock);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700518 device_unregister(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 goto out;
520
521error_kfree_fw:
522 kfree(firmware);
523 *firmware_p = NULL;
524out:
525 return retval;
526}
527
528/**
Kay Sievers312c0042005-11-16 09:00:00 +0100529 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800530 * @firmware_p: pointer to firmware image
531 * @name: name of firmware file
532 * @device: device for which firmware is being loaded
533 *
534 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700535 * of @name for device @device.
536 *
537 * Should be called from user context where sleeping is allowed.
538 *
Kay Sievers312c0042005-11-16 09:00:00 +0100539 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700540 * should be distinctive enough not to be confused with any other
541 * firmware image for this or any other device.
542 **/
543int
544request_firmware(const struct firmware **firmware_p, const char *name,
545 struct device *device)
546{
Kay Sievers312c0042005-11-16 09:00:00 +0100547 int uevent = 1;
548 return _request_firmware(firmware_p, name, device, uevent);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700549}
550
551/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800553 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 **/
555void
556release_firmware(const struct firmware *fw)
557{
David Woodhouse5658c762008-05-23 13:52:42 +0100558 struct builtin_fw *builtin;
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (fw) {
David Woodhouse5658c762008-05-23 13:52:42 +0100561 for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
562 builtin++) {
563 if (fw->data == builtin->data)
564 goto free_fw;
565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 vfree(fw->data);
David Woodhouse5658c762008-05-23 13:52:42 +0100567 free_fw:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 kfree(fw);
569 }
570}
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572/* Async support */
573struct firmware_work {
574 struct work_struct work;
575 struct module *module;
576 const char *name;
577 struct device *device;
578 void *context;
579 void (*cont)(const struct firmware *fw, void *context);
Kay Sievers312c0042005-11-16 09:00:00 +0100580 int uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581};
582
583static int
584request_firmware_work_func(void *arg)
585{
586 struct firmware_work *fw_work = arg;
587 const struct firmware *fw;
matthieu castet113fab12005-11-13 16:07:39 -0800588 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (!arg) {
590 WARN_ON(1);
591 return 0;
592 }
matthieu castet113fab12005-11-13 16:07:39 -0800593 ret = _request_firmware(&fw, fw_work->name, fw_work->device,
Kay Sievers312c0042005-11-16 09:00:00 +0100594 fw_work->uevent);
matthieu castet113fab12005-11-13 16:07:39 -0800595 if (ret < 0)
596 fw_work->cont(NULL, fw_work->context);
597 else {
598 fw_work->cont(fw, fw_work->context);
599 release_firmware(fw);
600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 module_put(fw_work->module);
602 kfree(fw_work);
matthieu castet113fab12005-11-13 16:07:39 -0800603 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
606/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800607 * request_firmware_nowait: asynchronous version of request_firmware
608 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +0100609 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800610 * is non-zero else the firmware copy must be done manually.
611 * @name: name of firmware file
612 * @device: device for which firmware is being loaded
613 * @context: will be passed over to @cont, and
614 * @fw may be %NULL if firmware request fails.
615 * @cont: function will be called asynchronously when the firmware
616 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 * Asynchronous variant of request_firmware() for contexts where
619 * it is not possible to sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 **/
621int
622request_firmware_nowait(
Kay Sievers312c0042005-11-16 09:00:00 +0100623 struct module *module, int uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 const char *name, struct device *device, void *context,
625 void (*cont)(const struct firmware *fw, void *context))
626{
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700627 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
629 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 if (!fw_work)
632 return -ENOMEM;
633 if (!try_module_get(module)) {
634 kfree(fw_work);
635 return -EFAULT;
636 }
637
638 *fw_work = (struct firmware_work) {
639 .module = module,
640 .name = name,
641 .device = device,
642 .context = context,
643 .cont = cont,
Kay Sievers312c0042005-11-16 09:00:00 +0100644 .uevent = uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 };
646
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700647 task = kthread_run(request_firmware_work_func, fw_work,
648 "firmware/%s", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700650 if (IS_ERR(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 fw_work->cont(NULL, fw_work->context);
matthieu castet113fab12005-11-13 16:07:39 -0800652 module_put(fw_work->module);
653 kfree(fw_work);
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700654 return PTR_ERR(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656 return 0;
657}
658
659static int __init
660firmware_class_init(void)
661{
662 int error;
663 error = class_register(&firmware_class);
664 if (error) {
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800665 printk(KERN_ERR "%s: class_register failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return error;
667 }
668 error = class_create_file(&firmware_class, &class_attr_timeout);
669 if (error) {
670 printk(KERN_ERR "%s: class_create_file failed\n",
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800671 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 class_unregister(&firmware_class);
673 }
674 return error;
675
676}
677static void __exit
678firmware_class_exit(void)
679{
680 class_unregister(&firmware_class);
681}
682
Shaohua Lia30a6a22006-09-27 01:50:52 -0700683fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684module_exit(firmware_class_exit);
685
686EXPORT_SYMBOL(release_firmware);
687EXPORT_SYMBOL(request_firmware);
688EXPORT_SYMBOL(request_firmware_nowait);