blob: 14615694ae9aa3e707435f2f04a1c6dde4586c5c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * firmware_class.c - Multi purpose firmware loading support
3 *
4 * Copyright (c) 2003 Manuel Estrada Sainz <ranty@debian.org>
5 *
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <linux/firmware.h>
22#include "base.h"
23
24MODULE_AUTHOR("Manuel Estrada Sainz <ranty@debian.org>");
25MODULE_DESCRIPTION("Multi purpose firmware loading support");
26MODULE_LICENSE("GPL");
27
28enum {
29 FW_STATUS_LOADING,
30 FW_STATUS_DONE,
31 FW_STATUS_ABORT,
32 FW_STATUS_READY,
Abhay Salunke6e3eaab2005-09-06 15:17:13 -070033 FW_STATUS_READY_NOHOTPLUG,
Linus Torvalds1da177e2005-04-16 15:20:36 -070034};
35
36static int loading_timeout = 10; /* In seconds */
37
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;
48 int alloc_size;
49 struct timer_list timeout;
50};
51
Arjan van de Ven858119e2006-01-14 13:20:43 -080052static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070053fw_load_abort(struct firmware_priv *fw_priv)
54{
55 set_bit(FW_STATUS_ABORT, &fw_priv->status);
56 wmb();
57 complete(&fw_priv->completion);
58}
59
60static ssize_t
61firmware_timeout_show(struct class *class, char *buf)
62{
63 return sprintf(buf, "%d\n", loading_timeout);
64}
65
66/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -080067 * firmware_timeout_store - set number of seconds to wait for firmware
68 * @class: device class pointer
69 * @buf: buffer to scan for timeout value
70 * @count: number of bytes in @buf
71 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -080073 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 * firmware will be provided.
75 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -080076 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 **/
78static ssize_t
79firmware_timeout_store(struct class *class, const char *buf, size_t count)
80{
81 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -070082 if (loading_timeout < 0)
83 loading_timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return count;
85}
86
87static CLASS_ATTR(timeout, 0644, firmware_timeout_show, firmware_timeout_store);
88
89static void fw_class_dev_release(struct class_device *class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Adrian Bunk1b81d662006-05-20 15:00:16 -070091static int firmware_class_uevent(struct class_device *class_dev, char **envp,
92 int num_envp, char *buffer, int buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
95 int i = 0, len = 0;
96
97 if (!test_bit(FW_STATUS_READY, &fw_priv->status))
98 return -ENODEV;
99
Kay Sievers312c0042005-11-16 09:00:00 +0100100 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
101 "FIRMWARE=%s", fw_priv->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return -ENOMEM;
Kay Sievers312c0042005-11-16 09:00:00 +0100103 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
104 "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700105 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 envp[i] = NULL;
107
108 return 0;
109}
110
Adrian Bunk1b81d662006-05-20 15:00:16 -0700111static struct class firmware_class = {
112 .name = "firmware",
113 .uevent = firmware_class_uevent,
114 .release = fw_class_dev_release,
115};
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117static ssize_t
118firmware_loading_show(struct class_device *class_dev, char *buf)
119{
120 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
121 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
122 return sprintf(buf, "%d\n", loading);
123}
124
125/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800126 * firmware_loading_store - set value in the 'loading' control file
127 * @class_dev: class_device pointer
128 * @buf: buffer to scan for loading control value
129 * @count: number of bytes in @buf
130 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * The relevant values are:
132 *
133 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800134 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 * -1: Conclude the load with an error and discard any written data.
136 **/
137static ssize_t
138firmware_loading_store(struct class_device *class_dev,
139 const char *buf, size_t count)
140{
141 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
142 int loading = simple_strtol(buf, NULL, 10);
143
144 switch (loading) {
145 case 1:
Laura Garciacad1e552006-05-23 23:22:38 +0200146 mutex_lock(&fw_lock);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700147 if (!fw_priv->fw) {
Laura Garciacad1e552006-05-23 23:22:38 +0200148 mutex_unlock(&fw_lock);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700149 break;
150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 vfree(fw_priv->fw->data);
152 fw_priv->fw->data = NULL;
153 fw_priv->fw->size = 0;
154 fw_priv->alloc_size = 0;
155 set_bit(FW_STATUS_LOADING, &fw_priv->status);
Laura Garciacad1e552006-05-23 23:22:38 +0200156 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 break;
158 case 0:
159 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
160 complete(&fw_priv->completion);
161 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
162 break;
163 }
164 /* fallthrough */
165 default:
166 printk(KERN_ERR "%s: unexpected value (%d)\n", __FUNCTION__,
167 loading);
168 /* fallthrough */
169 case -1:
170 fw_load_abort(fw_priv);
171 break;
172 }
173
174 return count;
175}
176
177static CLASS_DEVICE_ATTR(loading, 0644,
178 firmware_loading_show, firmware_loading_store);
179
180static ssize_t
181firmware_data_read(struct kobject *kobj,
182 char *buffer, loff_t offset, size_t count)
183{
184 struct class_device *class_dev = to_class_dev(kobj);
185 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
186 struct firmware *fw;
187 ssize_t ret_count = count;
188
Laura Garciacad1e552006-05-23 23:22:38 +0200189 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700191 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 ret_count = -ENODEV;
193 goto out;
194 }
195 if (offset > fw->size) {
196 ret_count = 0;
197 goto out;
198 }
199 if (offset + ret_count > fw->size)
200 ret_count = fw->size - offset;
201
202 memcpy(buffer, fw->data + offset, ret_count);
203out:
Laura Garciacad1e552006-05-23 23:22:38 +0200204 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return ret_count;
206}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208static int
209fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
210{
211 u8 *new_data;
Jeff Moyer30560ba2006-02-13 14:52:38 -0800212 int new_size = fw_priv->alloc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 if (min_size <= fw_priv->alloc_size)
215 return 0;
216
Jeff Moyer30560ba2006-02-13 14:52:38 -0800217 new_size = ALIGN(min_size, PAGE_SIZE);
218 new_data = vmalloc(new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (!new_data) {
220 printk(KERN_ERR "%s: unable to alloc buffer\n", __FUNCTION__);
221 /* Make sure that we don't keep incomplete data */
222 fw_load_abort(fw_priv);
223 return -ENOMEM;
224 }
Jeff Moyer30560ba2006-02-13 14:52:38 -0800225 fw_priv->alloc_size = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (fw_priv->fw->data) {
227 memcpy(new_data, fw_priv->fw->data, fw_priv->fw->size);
228 vfree(fw_priv->fw->data);
229 }
230 fw_priv->fw->data = new_data;
231 BUG_ON(min_size > fw_priv->alloc_size);
232 return 0;
233}
234
235/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800236 * firmware_data_write - write method for firmware
237 * @kobj: kobject for the class_device
238 * @buffer: buffer being written
239 * @offset: buffer offset for write in total data store area
240 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800242 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 * the driver as a firmware image.
244 **/
245static ssize_t
246firmware_data_write(struct kobject *kobj,
247 char *buffer, loff_t offset, size_t count)
248{
249 struct class_device *class_dev = to_class_dev(kobj);
250 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
251 struct firmware *fw;
252 ssize_t retval;
253
254 if (!capable(CAP_SYS_RAWIO))
255 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700256
Laura Garciacad1e552006-05-23 23:22:38 +0200257 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700259 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 retval = -ENODEV;
261 goto out;
262 }
263 retval = fw_realloc_buffer(fw_priv, offset + count);
264 if (retval)
265 goto out;
266
267 memcpy(fw->data + offset, buffer, count);
268
269 fw->size = max_t(size_t, offset + count, fw->size);
270 retval = count;
271out:
Laura Garciacad1e552006-05-23 23:22:38 +0200272 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return retval;
274}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276static struct bin_attribute firmware_attr_data_tmpl = {
277 .attr = {.name = "data", .mode = 0644, .owner = THIS_MODULE},
278 .size = 0,
279 .read = firmware_data_read,
280 .write = firmware_data_write,
281};
282
283static void
284fw_class_dev_release(struct class_device *class_dev)
285{
286 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
287
288 kfree(fw_priv);
289 kfree(class_dev);
290
291 module_put(THIS_MODULE);
292}
293
294static void
295firmware_class_timeout(u_long data)
296{
297 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
298 fw_load_abort(fw_priv);
299}
300
301static inline void
302fw_setup_class_device_id(struct class_device *class_dev, struct device *dev)
303{
304 /* XXX warning we should watch out for name collisions */
305 strlcpy(class_dev->class_id, dev->bus_id, BUS_ID_SIZE);
306}
307
308static int
309fw_register_class_device(struct class_device **class_dev_p,
310 const char *fw_name, struct device *device)
311{
312 int retval;
Jiri Slaby4aed0642005-09-13 01:25:01 -0700313 struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 GFP_KERNEL);
Jiri Slaby4aed0642005-09-13 01:25:01 -0700315 struct class_device *class_dev = kzalloc(sizeof(*class_dev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 GFP_KERNEL);
317
318 *class_dev_p = NULL;
319
320 if (!fw_priv || !class_dev) {
321 printk(KERN_ERR "%s: kmalloc failed\n", __FUNCTION__);
322 retval = -ENOMEM;
323 goto error_kfree;
324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 init_completion(&fw_priv->completion);
327 fw_priv->attr_data = firmware_attr_data_tmpl;
328 strlcpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX);
329
330 fw_priv->timeout.function = firmware_class_timeout;
331 fw_priv->timeout.data = (u_long) fw_priv;
332 init_timer(&fw_priv->timeout);
333
334 fw_setup_class_device_id(class_dev, device);
335 class_dev->dev = device;
336 class_dev->class = &firmware_class;
337 class_set_devdata(class_dev, fw_priv);
338 retval = class_device_register(class_dev);
339 if (retval) {
340 printk(KERN_ERR "%s: class_device_register failed\n",
341 __FUNCTION__);
342 goto error_kfree;
343 }
344 *class_dev_p = class_dev;
345 return 0;
346
347error_kfree:
348 kfree(fw_priv);
349 kfree(class_dev);
350 return retval;
351}
352
353static int
354fw_setup_class_device(struct firmware *fw, struct class_device **class_dev_p,
Kay Sievers312c0042005-11-16 09:00:00 +0100355 const char *fw_name, struct device *device, int uevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 struct class_device *class_dev;
358 struct firmware_priv *fw_priv;
359 int retval;
360
361 *class_dev_p = NULL;
362 retval = fw_register_class_device(&class_dev, fw_name, device);
363 if (retval)
364 goto out;
365
366 /* Need to pin this module until class device is destroyed */
367 __module_get(THIS_MODULE);
368
369 fw_priv = class_get_devdata(class_dev);
370
371 fw_priv->fw = fw;
372 retval = sysfs_create_bin_file(&class_dev->kobj, &fw_priv->attr_data);
373 if (retval) {
374 printk(KERN_ERR "%s: sysfs_create_bin_file failed\n",
375 __FUNCTION__);
376 goto error_unreg;
377 }
378
379 retval = class_device_create_file(class_dev,
380 &class_device_attr_loading);
381 if (retval) {
382 printk(KERN_ERR "%s: class_device_create_file failed\n",
383 __FUNCTION__);
384 goto error_unreg;
385 }
386
Kay Sievers312c0042005-11-16 09:00:00 +0100387 if (uevent)
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700388 set_bit(FW_STATUS_READY, &fw_priv->status);
389 else
390 set_bit(FW_STATUS_READY_NOHOTPLUG, &fw_priv->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 *class_dev_p = class_dev;
392 goto out;
393
394error_unreg:
395 class_device_unregister(class_dev);
396out:
397 return retval;
398}
399
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700400static int
401_request_firmware(const struct firmware **firmware_p, const char *name,
Kay Sievers312c0042005-11-16 09:00:00 +0100402 struct device *device, int uevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 struct class_device *class_dev;
405 struct firmware_priv *fw_priv;
406 struct firmware *firmware;
407 int retval;
408
409 if (!firmware_p)
410 return -EINVAL;
411
Jiri Slaby4aed0642005-09-13 01:25:01 -0700412 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (!firmware) {
414 printk(KERN_ERR "%s: kmalloc(struct firmware) failed\n",
415 __FUNCTION__);
416 retval = -ENOMEM;
417 goto out;
418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700420 retval = fw_setup_class_device(firmware, &class_dev, name, device,
Kay Sievers312c0042005-11-16 09:00:00 +0100421 uevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (retval)
423 goto error_kfree_fw;
424
425 fw_priv = class_get_devdata(class_dev);
426
Kay Sievers312c0042005-11-16 09:00:00 +0100427 if (uevent) {
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700428 if (loading_timeout > 0) {
429 fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
430 add_timer(&fw_priv->timeout);
431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Kay Sievers312c0042005-11-16 09:00:00 +0100433 kobject_uevent(&class_dev->kobj, KOBJ_ADD);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700434 wait_for_completion(&fw_priv->completion);
435 set_bit(FW_STATUS_DONE, &fw_priv->status);
436 del_timer_sync(&fw_priv->timeout);
437 } else
438 wait_for_completion(&fw_priv->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Laura Garciacad1e552006-05-23 23:22:38 +0200440 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
442 retval = -ENOENT;
443 release_firmware(fw_priv->fw);
444 *firmware_p = NULL;
445 }
446 fw_priv->fw = NULL;
Laura Garciacad1e552006-05-23 23:22:38 +0200447 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 class_device_unregister(class_dev);
449 goto out;
450
451error_kfree_fw:
452 kfree(firmware);
453 *firmware_p = NULL;
454out:
455 return retval;
456}
457
458/**
Kay Sievers312c0042005-11-16 09:00:00 +0100459 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800460 * @firmware_p: pointer to firmware image
461 * @name: name of firmware file
462 * @device: device for which firmware is being loaded
463 *
464 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700465 * of @name for device @device.
466 *
467 * Should be called from user context where sleeping is allowed.
468 *
Kay Sievers312c0042005-11-16 09:00:00 +0100469 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700470 * should be distinctive enough not to be confused with any other
471 * firmware image for this or any other device.
472 **/
473int
474request_firmware(const struct firmware **firmware_p, const char *name,
475 struct device *device)
476{
Kay Sievers312c0042005-11-16 09:00:00 +0100477 int uevent = 1;
478 return _request_firmware(firmware_p, name, device, uevent);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700479}
480
481/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800483 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 **/
485void
486release_firmware(const struct firmware *fw)
487{
488 if (fw) {
489 vfree(fw->data);
490 kfree(fw);
491 }
492}
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494/* Async support */
495struct firmware_work {
496 struct work_struct work;
497 struct module *module;
498 const char *name;
499 struct device *device;
500 void *context;
501 void (*cont)(const struct firmware *fw, void *context);
Kay Sievers312c0042005-11-16 09:00:00 +0100502 int uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503};
504
505static int
506request_firmware_work_func(void *arg)
507{
508 struct firmware_work *fw_work = arg;
509 const struct firmware *fw;
matthieu castet113fab12005-11-13 16:07:39 -0800510 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (!arg) {
512 WARN_ON(1);
513 return 0;
514 }
matthieu castet113fab12005-11-13 16:07:39 -0800515 ret = _request_firmware(&fw, fw_work->name, fw_work->device,
Kay Sievers312c0042005-11-16 09:00:00 +0100516 fw_work->uevent);
matthieu castet113fab12005-11-13 16:07:39 -0800517 if (ret < 0)
518 fw_work->cont(NULL, fw_work->context);
519 else {
520 fw_work->cont(fw, fw_work->context);
521 release_firmware(fw);
522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 module_put(fw_work->module);
524 kfree(fw_work);
matthieu castet113fab12005-11-13 16:07:39 -0800525 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
528/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800529 * request_firmware_nowait: asynchronous version of request_firmware
530 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +0100531 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800532 * is non-zero else the firmware copy must be done manually.
533 * @name: name of firmware file
534 * @device: device for which firmware is being loaded
535 * @context: will be passed over to @cont, and
536 * @fw may be %NULL if firmware request fails.
537 * @cont: function will be called asynchronously when the firmware
538 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 * Asynchronous variant of request_firmware() for contexts where
541 * it is not possible to sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 **/
543int
544request_firmware_nowait(
Kay Sievers312c0042005-11-16 09:00:00 +0100545 struct module *module, int uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 const char *name, struct device *device, void *context,
547 void (*cont)(const struct firmware *fw, void *context))
548{
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700549 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
551 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 if (!fw_work)
554 return -ENOMEM;
555 if (!try_module_get(module)) {
556 kfree(fw_work);
557 return -EFAULT;
558 }
559
560 *fw_work = (struct firmware_work) {
561 .module = module,
562 .name = name,
563 .device = device,
564 .context = context,
565 .cont = cont,
Kay Sievers312c0042005-11-16 09:00:00 +0100566 .uevent = uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 };
568
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700569 task = kthread_run(request_firmware_work_func, fw_work,
570 "firmware/%s", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700572 if (IS_ERR(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 fw_work->cont(NULL, fw_work->context);
matthieu castet113fab12005-11-13 16:07:39 -0800574 module_put(fw_work->module);
575 kfree(fw_work);
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700576 return PTR_ERR(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578 return 0;
579}
580
581static int __init
582firmware_class_init(void)
583{
584 int error;
585 error = class_register(&firmware_class);
586 if (error) {
587 printk(KERN_ERR "%s: class_register failed\n", __FUNCTION__);
588 return error;
589 }
590 error = class_create_file(&firmware_class, &class_attr_timeout);
591 if (error) {
592 printk(KERN_ERR "%s: class_create_file failed\n",
593 __FUNCTION__);
594 class_unregister(&firmware_class);
595 }
596 return error;
597
598}
599static void __exit
600firmware_class_exit(void)
601{
602 class_unregister(&firmware_class);
603}
604
Shaohua Lia30a6a22006-09-27 01:50:52 -0700605fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606module_exit(firmware_class_exit);
607
608EXPORT_SYMBOL(release_firmware);
609EXPORT_SYMBOL(request_firmware);
610EXPORT_SYMBOL(request_firmware_nowait);