blob: c0a979a5074bbec75ef691cfe1f13b269c1332fb [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
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -070024#define to_dev(obj) container_of(obj, struct device, kobj)
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026MODULE_AUTHOR("Manuel Estrada Sainz <ranty@debian.org>");
27MODULE_DESCRIPTION("Multi purpose firmware loading support");
28MODULE_LICENSE("GPL");
29
30enum {
31 FW_STATUS_LOADING,
32 FW_STATUS_DONE,
33 FW_STATUS_ABORT,
34 FW_STATUS_READY,
Abhay Salunke6e3eaab2005-09-06 15:17:13 -070035 FW_STATUS_READY_NOHOTPLUG,
Linus Torvalds1da177e2005-04-16 15:20:36 -070036};
37
Dave Jones2f651682007-01-25 15:56:15 -050038static int loading_timeout = 60; /* In seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/* fw_lock could be moved to 'struct firmware_priv' but since it is just
41 * guarding for corner cases a global lock should be OK */
Laura Garciacad1e552006-05-23 23:22:38 +020042static DEFINE_MUTEX(fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44struct firmware_priv {
45 char fw_id[FIRMWARE_NAME_MAX];
46 struct completion completion;
47 struct bin_attribute attr_data;
48 struct firmware *fw;
49 unsigned long status;
50 int alloc_size;
51 struct timer_list timeout;
52};
53
Arjan van de Ven858119e2006-01-14 13:20:43 -080054static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070055fw_load_abort(struct firmware_priv *fw_priv)
56{
57 set_bit(FW_STATUS_ABORT, &fw_priv->status);
58 wmb();
59 complete(&fw_priv->completion);
60}
61
62static ssize_t
63firmware_timeout_show(struct class *class, char *buf)
64{
65 return sprintf(buf, "%d\n", loading_timeout);
66}
67
68/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -080069 * firmware_timeout_store - set number of seconds to wait for firmware
70 * @class: device class pointer
71 * @buf: buffer to scan for timeout value
72 * @count: number of bytes in @buf
73 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -080075 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 * firmware will be provided.
77 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -080078 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 **/
80static ssize_t
81firmware_timeout_store(struct class *class, const char *buf, size_t count)
82{
83 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -070084 if (loading_timeout < 0)
85 loading_timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return count;
87}
88
89static CLASS_ATTR(timeout, 0644, firmware_timeout_show, firmware_timeout_store);
90
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -070091static void fw_dev_release(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -070093static int firmware_uevent(struct device *dev, char **envp, int num_envp,
94 char *buffer, int buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -070096 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 int i = 0, len = 0;
98
99 if (!test_bit(FW_STATUS_READY, &fw_priv->status))
100 return -ENODEV;
101
Kay Sievers312c0042005-11-16 09:00:00 +0100102 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
103 "FIRMWARE=%s", fw_priv->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return -ENOMEM;
Kay Sievers312c0042005-11-16 09:00:00 +0100105 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
106 "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700107 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 envp[i] = NULL;
109
110 return 0;
111}
112
Adrian Bunk1b81d662006-05-20 15:00:16 -0700113static struct class firmware_class = {
114 .name = "firmware",
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700115 .dev_uevent = firmware_uevent,
116 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700117};
118
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700119static ssize_t firmware_loading_show(struct device *dev,
120 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700122 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
124 return sprintf(buf, "%d\n", loading);
125}
126
127/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800128 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700129 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800130 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800131 * @buf: buffer to scan for loading control value
132 * @count: number of bytes in @buf
133 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 * The relevant values are:
135 *
136 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800137 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 * -1: Conclude the load with an error and discard any written data.
139 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700140static ssize_t firmware_loading_store(struct device *dev,
141 struct device_attribute *attr,
142 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700144 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 int loading = simple_strtol(buf, NULL, 10);
146
147 switch (loading) {
148 case 1:
Laura Garciacad1e552006-05-23 23:22:38 +0200149 mutex_lock(&fw_lock);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700150 if (!fw_priv->fw) {
Laura Garciacad1e552006-05-23 23:22:38 +0200151 mutex_unlock(&fw_lock);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700152 break;
153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 vfree(fw_priv->fw->data);
155 fw_priv->fw->data = NULL;
156 fw_priv->fw->size = 0;
157 fw_priv->alloc_size = 0;
158 set_bit(FW_STATUS_LOADING, &fw_priv->status);
Laura Garciacad1e552006-05-23 23:22:38 +0200159 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 break;
161 case 0:
162 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
163 complete(&fw_priv->completion);
164 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
165 break;
166 }
167 /* fallthrough */
168 default:
169 printk(KERN_ERR "%s: unexpected value (%d)\n", __FUNCTION__,
170 loading);
171 /* fallthrough */
172 case -1:
173 fw_load_abort(fw_priv);
174 break;
175 }
176
177 return count;
178}
179
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700180static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182static ssize_t
183firmware_data_read(struct kobject *kobj,
184 char *buffer, loff_t offset, size_t count)
185{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700186 struct device *dev = to_dev(kobj);
187 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 struct firmware *fw;
189 ssize_t ret_count = count;
190
Laura Garciacad1e552006-05-23 23:22:38 +0200191 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700193 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 ret_count = -ENODEV;
195 goto out;
196 }
197 if (offset > fw->size) {
198 ret_count = 0;
199 goto out;
200 }
201 if (offset + ret_count > fw->size)
202 ret_count = fw->size - offset;
203
204 memcpy(buffer, fw->data + offset, ret_count);
205out:
Laura Garciacad1e552006-05-23 23:22:38 +0200206 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return ret_count;
208}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210static int
211fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
212{
213 u8 *new_data;
Jeff Moyer30560ba2006-02-13 14:52:38 -0800214 int new_size = fw_priv->alloc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 if (min_size <= fw_priv->alloc_size)
217 return 0;
218
Jeff Moyer30560ba2006-02-13 14:52:38 -0800219 new_size = ALIGN(min_size, PAGE_SIZE);
220 new_data = vmalloc(new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (!new_data) {
222 printk(KERN_ERR "%s: unable to alloc buffer\n", __FUNCTION__);
223 /* Make sure that we don't keep incomplete data */
224 fw_load_abort(fw_priv);
225 return -ENOMEM;
226 }
Jeff Moyer30560ba2006-02-13 14:52:38 -0800227 fw_priv->alloc_size = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (fw_priv->fw->data) {
229 memcpy(new_data, fw_priv->fw->data, fw_priv->fw->size);
230 vfree(fw_priv->fw->data);
231 }
232 fw_priv->fw->data = new_data;
233 BUG_ON(min_size > fw_priv->alloc_size);
234 return 0;
235}
236
237/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800238 * firmware_data_write - write method for firmware
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700239 * @kobj: kobject for the device
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800240 * @buffer: buffer being written
241 * @offset: buffer offset for write in total data store area
242 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800244 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 * the driver as a firmware image.
246 **/
247static ssize_t
248firmware_data_write(struct kobject *kobj,
249 char *buffer, loff_t offset, size_t count)
250{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700251 struct device *dev = to_dev(kobj);
252 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 struct firmware *fw;
254 ssize_t retval;
255
256 if (!capable(CAP_SYS_RAWIO))
257 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700258
Laura Garciacad1e552006-05-23 23:22:38 +0200259 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700261 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 retval = -ENODEV;
263 goto out;
264 }
265 retval = fw_realloc_buffer(fw_priv, offset + count);
266 if (retval)
267 goto out;
268
269 memcpy(fw->data + offset, buffer, count);
270
271 fw->size = max_t(size_t, offset + count, fw->size);
272 retval = count;
273out:
Laura Garciacad1e552006-05-23 23:22:38 +0200274 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return retval;
276}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278static struct bin_attribute firmware_attr_data_tmpl = {
279 .attr = {.name = "data", .mode = 0644, .owner = THIS_MODULE},
280 .size = 0,
281 .read = firmware_data_read,
282 .write = firmware_data_write,
283};
284
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700285static void fw_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700287 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 kfree(fw_priv);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700290 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 module_put(THIS_MODULE);
293}
294
295static void
296firmware_class_timeout(u_long data)
297{
298 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
299 fw_load_abort(fw_priv);
300}
301
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700302static inline void fw_setup_device_id(struct device *f_dev, struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 /* XXX warning we should watch out for name collisions */
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700305 strlcpy(f_dev->bus_id, dev->bus_id, BUS_ID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700308static int fw_register_device(struct device **dev_p, const char *fw_name,
309 struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
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);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700314 struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700316 *dev_p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700318 if (!fw_priv || !f_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 printk(KERN_ERR "%s: kmalloc failed\n", __FUNCTION__);
320 retval = -ENOMEM;
321 goto error_kfree;
322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 init_completion(&fw_priv->completion);
325 fw_priv->attr_data = firmware_attr_data_tmpl;
326 strlcpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX);
327
328 fw_priv->timeout.function = firmware_class_timeout;
329 fw_priv->timeout.data = (u_long) fw_priv;
330 init_timer(&fw_priv->timeout);
331
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700332 fw_setup_device_id(f_dev, device);
333 f_dev->parent = device;
334 f_dev->class = &firmware_class;
335 dev_set_drvdata(f_dev, fw_priv);
336 retval = device_register(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (retval) {
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700338 printk(KERN_ERR "%s: device_register failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 __FUNCTION__);
340 goto error_kfree;
341 }
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700342 *dev_p = f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return 0;
344
345error_kfree:
346 kfree(fw_priv);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700347 kfree(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return retval;
349}
350
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700351static int fw_setup_device(struct firmware *fw, struct device **dev_p,
352 const char *fw_name, struct device *device,
353 int uevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700355 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 struct firmware_priv *fw_priv;
357 int retval;
358
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700359 *dev_p = NULL;
360 retval = fw_register_device(&f_dev, fw_name, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (retval)
362 goto out;
363
364 /* Need to pin this module until class device is destroyed */
365 __module_get(THIS_MODULE);
366
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700367 fw_priv = dev_get_drvdata(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 fw_priv->fw = fw;
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700370 retval = sysfs_create_bin_file(&f_dev->kobj, &fw_priv->attr_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (retval) {
372 printk(KERN_ERR "%s: sysfs_create_bin_file failed\n",
373 __FUNCTION__);
374 goto error_unreg;
375 }
376
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700377 retval = device_create_file(f_dev, &dev_attr_loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (retval) {
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700379 printk(KERN_ERR "%s: device_create_file failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 __FUNCTION__);
381 goto error_unreg;
382 }
383
Kay Sievers312c0042005-11-16 09:00:00 +0100384 if (uevent)
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700385 set_bit(FW_STATUS_READY, &fw_priv->status);
386 else
387 set_bit(FW_STATUS_READY_NOHOTPLUG, &fw_priv->status);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700388 *dev_p = f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 goto out;
390
391error_unreg:
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700392 device_unregister(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393out:
394 return retval;
395}
396
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700397static int
398_request_firmware(const struct firmware **firmware_p, const char *name,
Kay Sievers312c0042005-11-16 09:00:00 +0100399 struct device *device, int uevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700401 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 struct firmware_priv *fw_priv;
403 struct firmware *firmware;
404 int retval;
405
406 if (!firmware_p)
407 return -EINVAL;
408
Jiri Slaby4aed0642005-09-13 01:25:01 -0700409 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (!firmware) {
411 printk(KERN_ERR "%s: kmalloc(struct firmware) failed\n",
412 __FUNCTION__);
413 retval = -ENOMEM;
414 goto out;
415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700417 retval = fw_setup_device(firmware, &f_dev, name, device, uevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (retval)
419 goto error_kfree_fw;
420
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700421 fw_priv = dev_get_drvdata(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Kay Sievers312c0042005-11-16 09:00:00 +0100423 if (uevent) {
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700424 if (loading_timeout > 0) {
425 fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
426 add_timer(&fw_priv->timeout);
427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700429 kobject_uevent(&f_dev->kobj, KOBJ_ADD);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700430 wait_for_completion(&fw_priv->completion);
431 set_bit(FW_STATUS_DONE, &fw_priv->status);
432 del_timer_sync(&fw_priv->timeout);
433 } else
434 wait_for_completion(&fw_priv->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Laura Garciacad1e552006-05-23 23:22:38 +0200436 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
438 retval = -ENOENT;
439 release_firmware(fw_priv->fw);
440 *firmware_p = NULL;
441 }
442 fw_priv->fw = NULL;
Laura Garciacad1e552006-05-23 23:22:38 +0200443 mutex_unlock(&fw_lock);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700444 device_unregister(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 goto out;
446
447error_kfree_fw:
448 kfree(firmware);
449 *firmware_p = NULL;
450out:
451 return retval;
452}
453
454/**
Kay Sievers312c0042005-11-16 09:00:00 +0100455 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800456 * @firmware_p: pointer to firmware image
457 * @name: name of firmware file
458 * @device: device for which firmware is being loaded
459 *
460 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700461 * of @name for device @device.
462 *
463 * Should be called from user context where sleeping is allowed.
464 *
Kay Sievers312c0042005-11-16 09:00:00 +0100465 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700466 * should be distinctive enough not to be confused with any other
467 * firmware image for this or any other device.
468 **/
469int
470request_firmware(const struct firmware **firmware_p, const char *name,
471 struct device *device)
472{
Kay Sievers312c0042005-11-16 09:00:00 +0100473 int uevent = 1;
474 return _request_firmware(firmware_p, name, device, uevent);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700475}
476
477/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800479 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 **/
481void
482release_firmware(const struct firmware *fw)
483{
484 if (fw) {
485 vfree(fw->data);
486 kfree(fw);
487 }
488}
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490/* Async support */
491struct firmware_work {
492 struct work_struct work;
493 struct module *module;
494 const char *name;
495 struct device *device;
496 void *context;
497 void (*cont)(const struct firmware *fw, void *context);
Kay Sievers312c0042005-11-16 09:00:00 +0100498 int uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499};
500
501static int
502request_firmware_work_func(void *arg)
503{
504 struct firmware_work *fw_work = arg;
505 const struct firmware *fw;
matthieu castet113fab12005-11-13 16:07:39 -0800506 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (!arg) {
508 WARN_ON(1);
509 return 0;
510 }
matthieu castet113fab12005-11-13 16:07:39 -0800511 ret = _request_firmware(&fw, fw_work->name, fw_work->device,
Kay Sievers312c0042005-11-16 09:00:00 +0100512 fw_work->uevent);
matthieu castet113fab12005-11-13 16:07:39 -0800513 if (ret < 0)
514 fw_work->cont(NULL, fw_work->context);
515 else {
516 fw_work->cont(fw, fw_work->context);
517 release_firmware(fw);
518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 module_put(fw_work->module);
520 kfree(fw_work);
matthieu castet113fab12005-11-13 16:07:39 -0800521 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
524/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800525 * request_firmware_nowait: asynchronous version of request_firmware
526 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +0100527 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800528 * is non-zero else the firmware copy must be done manually.
529 * @name: name of firmware file
530 * @device: device for which firmware is being loaded
531 * @context: will be passed over to @cont, and
532 * @fw may be %NULL if firmware request fails.
533 * @cont: function will be called asynchronously when the firmware
534 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 * Asynchronous variant of request_firmware() for contexts where
537 * it is not possible to sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 **/
539int
540request_firmware_nowait(
Kay Sievers312c0042005-11-16 09:00:00 +0100541 struct module *module, int uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 const char *name, struct device *device, void *context,
543 void (*cont)(const struct firmware *fw, void *context))
544{
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700545 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
547 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 if (!fw_work)
550 return -ENOMEM;
551 if (!try_module_get(module)) {
552 kfree(fw_work);
553 return -EFAULT;
554 }
555
556 *fw_work = (struct firmware_work) {
557 .module = module,
558 .name = name,
559 .device = device,
560 .context = context,
561 .cont = cont,
Kay Sievers312c0042005-11-16 09:00:00 +0100562 .uevent = uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 };
564
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700565 task = kthread_run(request_firmware_work_func, fw_work,
566 "firmware/%s", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700568 if (IS_ERR(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 fw_work->cont(NULL, fw_work->context);
matthieu castet113fab12005-11-13 16:07:39 -0800570 module_put(fw_work->module);
571 kfree(fw_work);
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700572 return PTR_ERR(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574 return 0;
575}
576
577static int __init
578firmware_class_init(void)
579{
580 int error;
581 error = class_register(&firmware_class);
582 if (error) {
583 printk(KERN_ERR "%s: class_register failed\n", __FUNCTION__);
584 return error;
585 }
586 error = class_create_file(&firmware_class, &class_attr_timeout);
587 if (error) {
588 printk(KERN_ERR "%s: class_create_file failed\n",
589 __FUNCTION__);
590 class_unregister(&firmware_class);
591 }
592 return error;
593
594}
595static void __exit
596firmware_class_exit(void)
597{
598 class_unregister(&firmware_class);
599}
600
Shaohua Lia30a6a22006-09-27 01:50:52 -0700601fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602module_exit(firmware_class_exit);
603
604EXPORT_SYMBOL(release_firmware);
605EXPORT_SYMBOL(request_firmware);
606EXPORT_SYMBOL(request_firmware_nowait);