blob: 5d6c011183f5b7259297276b251f1648580421c7 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include <linux/firmware.h>
21#include "base.h"
22
23MODULE_AUTHOR("Manuel Estrada Sainz <ranty@debian.org>");
24MODULE_DESCRIPTION("Multi purpose firmware loading support");
25MODULE_LICENSE("GPL");
26
27enum {
28 FW_STATUS_LOADING,
29 FW_STATUS_DONE,
30 FW_STATUS_ABORT,
31 FW_STATUS_READY,
Abhay Salunke6e3eaab2005-09-06 15:17:13 -070032 FW_STATUS_READY_NOHOTPLUG,
Linus Torvalds1da177e2005-04-16 15:20:36 -070033};
34
35static int loading_timeout = 10; /* In seconds */
36
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 {
42 char fw_id[FIRMWARE_NAME_MAX];
43 struct completion completion;
44 struct bin_attribute attr_data;
45 struct firmware *fw;
46 unsigned long status;
47 int alloc_size;
48 struct timer_list timeout;
49};
50
Arjan van de Ven858119e2006-01-14 13:20:43 -080051static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070052fw_load_abort(struct firmware_priv *fw_priv)
53{
54 set_bit(FW_STATUS_ABORT, &fw_priv->status);
55 wmb();
56 complete(&fw_priv->completion);
57}
58
59static ssize_t
60firmware_timeout_show(struct class *class, char *buf)
61{
62 return sprintf(buf, "%d\n", loading_timeout);
63}
64
65/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -080066 * firmware_timeout_store - set number of seconds to wait for firmware
67 * @class: device class pointer
68 * @buf: buffer to scan for timeout value
69 * @count: number of bytes in @buf
70 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -080072 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * firmware will be provided.
74 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -080075 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 **/
77static ssize_t
78firmware_timeout_store(struct class *class, const char *buf, size_t count)
79{
80 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -070081 if (loading_timeout < 0)
82 loading_timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return count;
84}
85
86static CLASS_ATTR(timeout, 0644, firmware_timeout_show, firmware_timeout_store);
87
88static void fw_class_dev_release(struct class_device *class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Adrian Bunk1b81d662006-05-20 15:00:16 -070090static int firmware_class_uevent(struct class_device *class_dev, char **envp,
91 int num_envp, char *buffer, int buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
94 int i = 0, len = 0;
95
96 if (!test_bit(FW_STATUS_READY, &fw_priv->status))
97 return -ENODEV;
98
Kay Sievers312c0042005-11-16 09:00:00 +010099 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
100 "FIRMWARE=%s", fw_priv->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return -ENOMEM;
Kay Sievers312c0042005-11-16 09:00:00 +0100102 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
103 "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700104 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 envp[i] = NULL;
106
107 return 0;
108}
109
Adrian Bunk1b81d662006-05-20 15:00:16 -0700110static struct class firmware_class = {
111 .name = "firmware",
112 .uevent = firmware_class_uevent,
113 .release = fw_class_dev_release,
114};
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116static ssize_t
117firmware_loading_show(struct class_device *class_dev, char *buf)
118{
119 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
120 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
121 return sprintf(buf, "%d\n", loading);
122}
123
124/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800125 * firmware_loading_store - set value in the 'loading' control file
126 * @class_dev: class_device pointer
127 * @buf: buffer to scan for loading control value
128 * @count: number of bytes in @buf
129 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * The relevant values are:
131 *
132 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800133 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 * -1: Conclude the load with an error and discard any written data.
135 **/
136static ssize_t
137firmware_loading_store(struct class_device *class_dev,
138 const char *buf, size_t count)
139{
140 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
141 int loading = simple_strtol(buf, NULL, 10);
142
143 switch (loading) {
144 case 1:
Laura Garciacad1e552006-05-23 23:22:38 +0200145 mutex_lock(&fw_lock);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700146 if (!fw_priv->fw) {
Laura Garciacad1e552006-05-23 23:22:38 +0200147 mutex_unlock(&fw_lock);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700148 break;
149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 vfree(fw_priv->fw->data);
151 fw_priv->fw->data = NULL;
152 fw_priv->fw->size = 0;
153 fw_priv->alloc_size = 0;
154 set_bit(FW_STATUS_LOADING, &fw_priv->status);
Laura Garciacad1e552006-05-23 23:22:38 +0200155 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 break;
157 case 0:
158 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
159 complete(&fw_priv->completion);
160 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
161 break;
162 }
163 /* fallthrough */
164 default:
165 printk(KERN_ERR "%s: unexpected value (%d)\n", __FUNCTION__,
166 loading);
167 /* fallthrough */
168 case -1:
169 fw_load_abort(fw_priv);
170 break;
171 }
172
173 return count;
174}
175
176static CLASS_DEVICE_ATTR(loading, 0644,
177 firmware_loading_show, firmware_loading_store);
178
179static ssize_t
180firmware_data_read(struct kobject *kobj,
181 char *buffer, loff_t offset, size_t count)
182{
183 struct class_device *class_dev = to_class_dev(kobj);
184 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
185 struct firmware *fw;
186 ssize_t ret_count = count;
187
Laura Garciacad1e552006-05-23 23:22:38 +0200188 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700190 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 ret_count = -ENODEV;
192 goto out;
193 }
194 if (offset > fw->size) {
195 ret_count = 0;
196 goto out;
197 }
198 if (offset + ret_count > fw->size)
199 ret_count = fw->size - offset;
200
201 memcpy(buffer, fw->data + offset, ret_count);
202out:
Laura Garciacad1e552006-05-23 23:22:38 +0200203 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return ret_count;
205}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207static int
208fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
209{
210 u8 *new_data;
Jeff Moyer30560ba2006-02-13 14:52:38 -0800211 int new_size = fw_priv->alloc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 if (min_size <= fw_priv->alloc_size)
214 return 0;
215
Jeff Moyer30560ba2006-02-13 14:52:38 -0800216 new_size = ALIGN(min_size, PAGE_SIZE);
217 new_data = vmalloc(new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (!new_data) {
219 printk(KERN_ERR "%s: unable to alloc buffer\n", __FUNCTION__);
220 /* Make sure that we don't keep incomplete data */
221 fw_load_abort(fw_priv);
222 return -ENOMEM;
223 }
Jeff Moyer30560ba2006-02-13 14:52:38 -0800224 fw_priv->alloc_size = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (fw_priv->fw->data) {
226 memcpy(new_data, fw_priv->fw->data, fw_priv->fw->size);
227 vfree(fw_priv->fw->data);
228 }
229 fw_priv->fw->data = new_data;
230 BUG_ON(min_size > fw_priv->alloc_size);
231 return 0;
232}
233
234/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800235 * firmware_data_write - write method for firmware
236 * @kobj: kobject for the class_device
237 * @buffer: buffer being written
238 * @offset: buffer offset for write in total data store area
239 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800241 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 * the driver as a firmware image.
243 **/
244static ssize_t
245firmware_data_write(struct kobject *kobj,
246 char *buffer, loff_t offset, size_t count)
247{
248 struct class_device *class_dev = to_class_dev(kobj);
249 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
250 struct firmware *fw;
251 ssize_t retval;
252
253 if (!capable(CAP_SYS_RAWIO))
254 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700255
Laura Garciacad1e552006-05-23 23:22:38 +0200256 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700258 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 retval = -ENODEV;
260 goto out;
261 }
262 retval = fw_realloc_buffer(fw_priv, offset + count);
263 if (retval)
264 goto out;
265
266 memcpy(fw->data + offset, buffer, count);
267
268 fw->size = max_t(size_t, offset + count, fw->size);
269 retval = count;
270out:
Laura Garciacad1e552006-05-23 23:22:38 +0200271 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return retval;
273}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275static struct bin_attribute firmware_attr_data_tmpl = {
276 .attr = {.name = "data", .mode = 0644, .owner = THIS_MODULE},
277 .size = 0,
278 .read = firmware_data_read,
279 .write = firmware_data_write,
280};
281
282static void
283fw_class_dev_release(struct class_device *class_dev)
284{
285 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
286
287 kfree(fw_priv);
288 kfree(class_dev);
289
290 module_put(THIS_MODULE);
291}
292
293static void
294firmware_class_timeout(u_long data)
295{
296 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
297 fw_load_abort(fw_priv);
298}
299
300static inline void
301fw_setup_class_device_id(struct class_device *class_dev, struct device *dev)
302{
303 /* XXX warning we should watch out for name collisions */
304 strlcpy(class_dev->class_id, dev->bus_id, BUS_ID_SIZE);
305}
306
307static int
308fw_register_class_device(struct class_device **class_dev_p,
309 const char *fw_name, struct device *device)
310{
311 int retval;
Jiri Slaby4aed0642005-09-13 01:25:01 -0700312 struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 GFP_KERNEL);
Jiri Slaby4aed0642005-09-13 01:25:01 -0700314 struct class_device *class_dev = kzalloc(sizeof(*class_dev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 GFP_KERNEL);
316
317 *class_dev_p = NULL;
318
319 if (!fw_priv || !class_dev) {
320 printk(KERN_ERR "%s: kmalloc failed\n", __FUNCTION__);
321 retval = -ENOMEM;
322 goto error_kfree;
323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 init_completion(&fw_priv->completion);
326 fw_priv->attr_data = firmware_attr_data_tmpl;
327 strlcpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX);
328
329 fw_priv->timeout.function = firmware_class_timeout;
330 fw_priv->timeout.data = (u_long) fw_priv;
331 init_timer(&fw_priv->timeout);
332
333 fw_setup_class_device_id(class_dev, device);
334 class_dev->dev = device;
335 class_dev->class = &firmware_class;
336 class_set_devdata(class_dev, fw_priv);
337 retval = class_device_register(class_dev);
338 if (retval) {
339 printk(KERN_ERR "%s: class_device_register failed\n",
340 __FUNCTION__);
341 goto error_kfree;
342 }
343 *class_dev_p = class_dev;
344 return 0;
345
346error_kfree:
347 kfree(fw_priv);
348 kfree(class_dev);
349 return retval;
350}
351
352static int
353fw_setup_class_device(struct firmware *fw, struct class_device **class_dev_p,
Kay Sievers312c0042005-11-16 09:00:00 +0100354 const char *fw_name, struct device *device, int uevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 struct class_device *class_dev;
357 struct firmware_priv *fw_priv;
358 int retval;
359
360 *class_dev_p = NULL;
361 retval = fw_register_class_device(&class_dev, fw_name, device);
362 if (retval)
363 goto out;
364
365 /* Need to pin this module until class device is destroyed */
366 __module_get(THIS_MODULE);
367
368 fw_priv = class_get_devdata(class_dev);
369
370 fw_priv->fw = fw;
371 retval = sysfs_create_bin_file(&class_dev->kobj, &fw_priv->attr_data);
372 if (retval) {
373 printk(KERN_ERR "%s: sysfs_create_bin_file failed\n",
374 __FUNCTION__);
375 goto error_unreg;
376 }
377
378 retval = class_device_create_file(class_dev,
379 &class_device_attr_loading);
380 if (retval) {
381 printk(KERN_ERR "%s: class_device_create_file failed\n",
382 __FUNCTION__);
383 goto error_unreg;
384 }
385
Kay Sievers312c0042005-11-16 09:00:00 +0100386 if (uevent)
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700387 set_bit(FW_STATUS_READY, &fw_priv->status);
388 else
389 set_bit(FW_STATUS_READY_NOHOTPLUG, &fw_priv->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 *class_dev_p = class_dev;
391 goto out;
392
393error_unreg:
394 class_device_unregister(class_dev);
395out:
396 return retval;
397}
398
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700399static int
400_request_firmware(const struct firmware **firmware_p, const char *name,
Kay Sievers312c0042005-11-16 09:00:00 +0100401 struct device *device, int uevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 struct class_device *class_dev;
404 struct firmware_priv *fw_priv;
405 struct firmware *firmware;
406 int retval;
407
408 if (!firmware_p)
409 return -EINVAL;
410
Jiri Slaby4aed0642005-09-13 01:25:01 -0700411 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (!firmware) {
413 printk(KERN_ERR "%s: kmalloc(struct firmware) failed\n",
414 __FUNCTION__);
415 retval = -ENOMEM;
416 goto out;
417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700419 retval = fw_setup_class_device(firmware, &class_dev, name, device,
Kay Sievers312c0042005-11-16 09:00:00 +0100420 uevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (retval)
422 goto error_kfree_fw;
423
424 fw_priv = class_get_devdata(class_dev);
425
Kay Sievers312c0042005-11-16 09:00:00 +0100426 if (uevent) {
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700427 if (loading_timeout > 0) {
428 fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
429 add_timer(&fw_priv->timeout);
430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Kay Sievers312c0042005-11-16 09:00:00 +0100432 kobject_uevent(&class_dev->kobj, KOBJ_ADD);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700433 wait_for_completion(&fw_priv->completion);
434 set_bit(FW_STATUS_DONE, &fw_priv->status);
435 del_timer_sync(&fw_priv->timeout);
436 } else
437 wait_for_completion(&fw_priv->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Laura Garciacad1e552006-05-23 23:22:38 +0200439 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
441 retval = -ENOENT;
442 release_firmware(fw_priv->fw);
443 *firmware_p = NULL;
444 }
445 fw_priv->fw = NULL;
Laura Garciacad1e552006-05-23 23:22:38 +0200446 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 class_device_unregister(class_dev);
448 goto out;
449
450error_kfree_fw:
451 kfree(firmware);
452 *firmware_p = NULL;
453out:
454 return retval;
455}
456
457/**
Kay Sievers312c0042005-11-16 09:00:00 +0100458 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800459 * @firmware_p: pointer to firmware image
460 * @name: name of firmware file
461 * @device: device for which firmware is being loaded
462 *
463 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700464 * of @name for device @device.
465 *
466 * Should be called from user context where sleeping is allowed.
467 *
Kay Sievers312c0042005-11-16 09:00:00 +0100468 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700469 * should be distinctive enough not to be confused with any other
470 * firmware image for this or any other device.
471 **/
472int
473request_firmware(const struct firmware **firmware_p, const char *name,
474 struct device *device)
475{
Kay Sievers312c0042005-11-16 09:00:00 +0100476 int uevent = 1;
477 return _request_firmware(firmware_p, name, device, uevent);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700478}
479
480/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800482 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 **/
484void
485release_firmware(const struct firmware *fw)
486{
487 if (fw) {
488 vfree(fw->data);
489 kfree(fw);
490 }
491}
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493/* Async support */
494struct firmware_work {
495 struct work_struct work;
496 struct module *module;
497 const char *name;
498 struct device *device;
499 void *context;
500 void (*cont)(const struct firmware *fw, void *context);
Kay Sievers312c0042005-11-16 09:00:00 +0100501 int uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502};
503
504static int
505request_firmware_work_func(void *arg)
506{
507 struct firmware_work *fw_work = arg;
508 const struct firmware *fw;
matthieu castet113fab12005-11-13 16:07:39 -0800509 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (!arg) {
511 WARN_ON(1);
512 return 0;
513 }
514 daemonize("%s/%s", "firmware", fw_work->name);
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{
549 struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
550 GFP_ATOMIC);
551 int ret;
552
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
569 ret = kernel_thread(request_firmware_work_func, fw_work,
570 CLONE_FS | CLONE_FILES);
571
572 if (ret < 0) {
573 fw_work->cont(NULL, fw_work->context);
matthieu castet113fab12005-11-13 16:07:39 -0800574 module_put(fw_work->module);
575 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return ret;
577 }
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
605module_init(firmware_class_init);
606module_exit(firmware_class_exit);
607
608EXPORT_SYMBOL(release_firmware);
609EXPORT_SYMBOL(request_firmware);
610EXPORT_SYMBOL(request_firmware_nowait);