blob: 9fd4a8534146e1747160e0a987fe8e4224eae01f [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>
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
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;
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
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -070089static void fw_dev_release(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Kay Sievers7eff2e72007-08-14 15:15:12 +020091static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -070093 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Kay Sievers7eff2e72007-08-14 15:15:12 +020095 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +020097 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -070098 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 return 0;
101}
102
Adrian Bunk1b81d662006-05-20 15:00:16 -0700103static struct class firmware_class = {
104 .name = "firmware",
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700105 .dev_uevent = firmware_uevent,
106 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700107};
108
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700109static ssize_t firmware_loading_show(struct device *dev,
110 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700112 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
114 return sprintf(buf, "%d\n", loading);
115}
116
117/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800118 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700119 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800120 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800121 * @buf: buffer to scan for loading control value
122 * @count: number of bytes in @buf
123 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 * The relevant values are:
125 *
126 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800127 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 * -1: Conclude the load with an error and discard any written data.
129 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700130static ssize_t firmware_loading_store(struct device *dev,
131 struct device_attribute *attr,
132 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700134 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 int loading = simple_strtol(buf, NULL, 10);
136
137 switch (loading) {
138 case 1:
Laura Garciacad1e552006-05-23 23:22:38 +0200139 mutex_lock(&fw_lock);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700140 if (!fw_priv->fw) {
Laura Garciacad1e552006-05-23 23:22:38 +0200141 mutex_unlock(&fw_lock);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700142 break;
143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 vfree(fw_priv->fw->data);
145 fw_priv->fw->data = NULL;
146 fw_priv->fw->size = 0;
147 fw_priv->alloc_size = 0;
148 set_bit(FW_STATUS_LOADING, &fw_priv->status);
Laura Garciacad1e552006-05-23 23:22:38 +0200149 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 break;
151 case 0:
152 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
153 complete(&fw_priv->completion);
154 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
155 break;
156 }
157 /* fallthrough */
158 default:
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800159 printk(KERN_ERR "%s: unexpected value (%d)\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 loading);
161 /* fallthrough */
162 case -1:
163 fw_load_abort(fw_priv);
164 break;
165 }
166
167 return count;
168}
169
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700170static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800173firmware_data_read(struct kobject *kobj, struct bin_attribute *bin_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 char *buffer, loff_t offset, size_t count)
175{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700176 struct device *dev = to_dev(kobj);
177 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 struct firmware *fw;
179 ssize_t ret_count = count;
180
Laura Garciacad1e552006-05-23 23:22:38 +0200181 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700183 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 ret_count = -ENODEV;
185 goto out;
186 }
187 if (offset > fw->size) {
188 ret_count = 0;
189 goto out;
190 }
191 if (offset + ret_count > fw->size)
192 ret_count = fw->size - offset;
193
194 memcpy(buffer, fw->data + offset, ret_count);
195out:
Laura Garciacad1e552006-05-23 23:22:38 +0200196 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return ret_count;
198}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200static int
201fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
202{
203 u8 *new_data;
Jeff Moyer30560ba2006-02-13 14:52:38 -0800204 int new_size = fw_priv->alloc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 if (min_size <= fw_priv->alloc_size)
207 return 0;
208
Jeff Moyer30560ba2006-02-13 14:52:38 -0800209 new_size = ALIGN(min_size, PAGE_SIZE);
210 new_data = vmalloc(new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (!new_data) {
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800212 printk(KERN_ERR "%s: unable to alloc buffer\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* Make sure that we don't keep incomplete data */
214 fw_load_abort(fw_priv);
215 return -ENOMEM;
216 }
Jeff Moyer30560ba2006-02-13 14:52:38 -0800217 fw_priv->alloc_size = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (fw_priv->fw->data) {
219 memcpy(new_data, fw_priv->fw->data, fw_priv->fw->size);
220 vfree(fw_priv->fw->data);
221 }
222 fw_priv->fw->data = new_data;
223 BUG_ON(min_size > fw_priv->alloc_size);
224 return 0;
225}
226
227/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800228 * firmware_data_write - write method for firmware
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700229 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700230 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800231 * @buffer: buffer being written
232 * @offset: buffer offset for write in total data store area
233 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800235 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 * the driver as a firmware image.
237 **/
238static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800239firmware_data_write(struct kobject *kobj, struct bin_attribute *bin_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 char *buffer, loff_t offset, size_t count)
241{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700242 struct device *dev = to_dev(kobj);
243 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 struct firmware *fw;
245 ssize_t retval;
246
247 if (!capable(CAP_SYS_RAWIO))
248 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700249
Laura Garciacad1e552006-05-23 23:22:38 +0200250 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700252 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 retval = -ENODEV;
254 goto out;
255 }
256 retval = fw_realloc_buffer(fw_priv, offset + count);
257 if (retval)
258 goto out;
259
260 memcpy(fw->data + offset, buffer, count);
261
262 fw->size = max_t(size_t, offset + count, fw->size);
263 retval = count;
264out:
Laura Garciacad1e552006-05-23 23:22:38 +0200265 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return retval;
267}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269static struct bin_attribute firmware_attr_data_tmpl = {
Tejun Heo7b595752007-06-14 03:45:17 +0900270 .attr = {.name = "data", .mode = 0644},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 .size = 0,
272 .read = firmware_data_read,
273 .write = firmware_data_write,
274};
275
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700276static void fw_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700278 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 kfree(fw_priv);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700281 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 module_put(THIS_MODULE);
284}
285
286static void
287firmware_class_timeout(u_long data)
288{
289 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
290 fw_load_abort(fw_priv);
291}
292
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700293static inline void fw_setup_device_id(struct device *f_dev, struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Michael E Brown7d640c42008-01-29 15:35:01 -0600295 /* XXX warning we should watch out for name collisions */
296 strlcpy(f_dev->bus_id, dev->bus_id, BUS_ID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700299static int fw_register_device(struct device **dev_p, const char *fw_name,
300 struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 int retval;
Jiri Slaby4aed0642005-09-13 01:25:01 -0700303 struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 GFP_KERNEL);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700305 struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700307 *dev_p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700309 if (!fw_priv || !f_dev) {
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800310 printk(KERN_ERR "%s: kmalloc failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 retval = -ENOMEM;
312 goto error_kfree;
313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 init_completion(&fw_priv->completion);
316 fw_priv->attr_data = firmware_attr_data_tmpl;
317 strlcpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX);
318
319 fw_priv->timeout.function = firmware_class_timeout;
320 fw_priv->timeout.data = (u_long) fw_priv;
321 init_timer(&fw_priv->timeout);
322
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700323 fw_setup_device_id(f_dev, device);
324 f_dev->parent = device;
325 f_dev->class = &firmware_class;
326 dev_set_drvdata(f_dev, fw_priv);
Cornelia Huckbdc49602007-03-29 11:12:14 +0200327 f_dev->uevent_suppress = 1;
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700328 retval = device_register(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (retval) {
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700330 printk(KERN_ERR "%s: device_register failed\n",
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800331 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 goto error_kfree;
333 }
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700334 *dev_p = f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return 0;
336
337error_kfree:
338 kfree(fw_priv);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700339 kfree(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return retval;
341}
342
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700343static int fw_setup_device(struct firmware *fw, struct device **dev_p,
344 const char *fw_name, struct device *device,
345 int uevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700347 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 struct firmware_priv *fw_priv;
349 int retval;
350
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700351 *dev_p = NULL;
352 retval = fw_register_device(&f_dev, fw_name, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (retval)
354 goto out;
355
356 /* Need to pin this module until class device is destroyed */
357 __module_get(THIS_MODULE);
358
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700359 fw_priv = dev_get_drvdata(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 fw_priv->fw = fw;
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700362 retval = sysfs_create_bin_file(&f_dev->kobj, &fw_priv->attr_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (retval) {
364 printk(KERN_ERR "%s: sysfs_create_bin_file failed\n",
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800365 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 goto error_unreg;
367 }
368
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700369 retval = device_create_file(f_dev, &dev_attr_loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (retval) {
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700371 printk(KERN_ERR "%s: device_create_file failed\n",
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800372 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 goto error_unreg;
374 }
375
Kay Sievers312c0042005-11-16 09:00:00 +0100376 if (uevent)
Cornelia Huckbdc49602007-03-29 11:12:14 +0200377 f_dev->uevent_suppress = 0;
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700378 *dev_p = f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 goto out;
380
381error_unreg:
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700382 device_unregister(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383out:
384 return retval;
385}
386
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700387static int
388_request_firmware(const struct firmware **firmware_p, const char *name,
Kay Sievers312c0042005-11-16 09:00:00 +0100389 struct device *device, int uevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700391 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 struct firmware_priv *fw_priv;
393 struct firmware *firmware;
394 int retval;
395
396 if (!firmware_p)
397 return -EINVAL;
398
Ciaran McCreesh9a3be322008-04-29 00:59:35 -0700399 printk(KERN_INFO "firmware: requesting %s\n", name);
400
Jiri Slaby4aed0642005-09-13 01:25:01 -0700401 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (!firmware) {
403 printk(KERN_ERR "%s: kmalloc(struct firmware) failed\n",
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800404 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 retval = -ENOMEM;
406 goto out;
407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700409 retval = fw_setup_device(firmware, &f_dev, name, device, uevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (retval)
411 goto error_kfree_fw;
412
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700413 fw_priv = dev_get_drvdata(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Kay Sievers312c0042005-11-16 09:00:00 +0100415 if (uevent) {
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700416 if (loading_timeout > 0) {
417 fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
418 add_timer(&fw_priv->timeout);
419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700421 kobject_uevent(&f_dev->kobj, KOBJ_ADD);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700422 wait_for_completion(&fw_priv->completion);
423 set_bit(FW_STATUS_DONE, &fw_priv->status);
424 del_timer_sync(&fw_priv->timeout);
425 } else
426 wait_for_completion(&fw_priv->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Laura Garciacad1e552006-05-23 23:22:38 +0200428 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
430 retval = -ENOENT;
431 release_firmware(fw_priv->fw);
432 *firmware_p = NULL;
433 }
434 fw_priv->fw = NULL;
Laura Garciacad1e552006-05-23 23:22:38 +0200435 mutex_unlock(&fw_lock);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700436 device_unregister(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 goto out;
438
439error_kfree_fw:
440 kfree(firmware);
441 *firmware_p = NULL;
442out:
443 return retval;
444}
445
446/**
Kay Sievers312c0042005-11-16 09:00:00 +0100447 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800448 * @firmware_p: pointer to firmware image
449 * @name: name of firmware file
450 * @device: device for which firmware is being loaded
451 *
452 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700453 * of @name for device @device.
454 *
455 * Should be called from user context where sleeping is allowed.
456 *
Kay Sievers312c0042005-11-16 09:00:00 +0100457 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700458 * should be distinctive enough not to be confused with any other
459 * firmware image for this or any other device.
460 **/
461int
462request_firmware(const struct firmware **firmware_p, const char *name,
463 struct device *device)
464{
Kay Sievers312c0042005-11-16 09:00:00 +0100465 int uevent = 1;
466 return _request_firmware(firmware_p, name, device, uevent);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700467}
468
469/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800471 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 **/
473void
474release_firmware(const struct firmware *fw)
475{
476 if (fw) {
477 vfree(fw->data);
478 kfree(fw);
479 }
480}
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482/* Async support */
483struct firmware_work {
484 struct work_struct work;
485 struct module *module;
486 const char *name;
487 struct device *device;
488 void *context;
489 void (*cont)(const struct firmware *fw, void *context);
Kay Sievers312c0042005-11-16 09:00:00 +0100490 int uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491};
492
493static int
494request_firmware_work_func(void *arg)
495{
496 struct firmware_work *fw_work = arg;
497 const struct firmware *fw;
matthieu castet113fab12005-11-13 16:07:39 -0800498 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (!arg) {
500 WARN_ON(1);
501 return 0;
502 }
matthieu castet113fab12005-11-13 16:07:39 -0800503 ret = _request_firmware(&fw, fw_work->name, fw_work->device,
Kay Sievers312c0042005-11-16 09:00:00 +0100504 fw_work->uevent);
matthieu castet113fab12005-11-13 16:07:39 -0800505 if (ret < 0)
506 fw_work->cont(NULL, fw_work->context);
507 else {
508 fw_work->cont(fw, fw_work->context);
509 release_firmware(fw);
510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 module_put(fw_work->module);
512 kfree(fw_work);
matthieu castet113fab12005-11-13 16:07:39 -0800513 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
516/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800517 * request_firmware_nowait: asynchronous version of request_firmware
518 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +0100519 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800520 * is non-zero else the firmware copy must be done manually.
521 * @name: name of firmware file
522 * @device: device for which firmware is being loaded
523 * @context: will be passed over to @cont, and
524 * @fw may be %NULL if firmware request fails.
525 * @cont: function will be called asynchronously when the firmware
526 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 * Asynchronous variant of request_firmware() for contexts where
529 * it is not possible to sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 **/
531int
532request_firmware_nowait(
Kay Sievers312c0042005-11-16 09:00:00 +0100533 struct module *module, int uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 const char *name, struct device *device, void *context,
535 void (*cont)(const struct firmware *fw, void *context))
536{
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700537 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
539 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 if (!fw_work)
542 return -ENOMEM;
543 if (!try_module_get(module)) {
544 kfree(fw_work);
545 return -EFAULT;
546 }
547
548 *fw_work = (struct firmware_work) {
549 .module = module,
550 .name = name,
551 .device = device,
552 .context = context,
553 .cont = cont,
Kay Sievers312c0042005-11-16 09:00:00 +0100554 .uevent = uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 };
556
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700557 task = kthread_run(request_firmware_work_func, fw_work,
558 "firmware/%s", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700560 if (IS_ERR(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 fw_work->cont(NULL, fw_work->context);
matthieu castet113fab12005-11-13 16:07:39 -0800562 module_put(fw_work->module);
563 kfree(fw_work);
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700564 return PTR_ERR(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566 return 0;
567}
568
569static int __init
570firmware_class_init(void)
571{
572 int error;
573 error = class_register(&firmware_class);
574 if (error) {
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800575 printk(KERN_ERR "%s: class_register failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return error;
577 }
578 error = class_create_file(&firmware_class, &class_attr_timeout);
579 if (error) {
580 printk(KERN_ERR "%s: class_create_file failed\n",
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800581 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 class_unregister(&firmware_class);
583 }
584 return error;
585
586}
587static void __exit
588firmware_class_exit(void)
589{
590 class_unregister(&firmware_class);
591}
592
Shaohua Lia30a6a22006-09-27 01:50:52 -0700593fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594module_exit(firmware_class_exit);
595
596EXPORT_SYMBOL(release_firmware);
597EXPORT_SYMBOL(request_firmware);
598EXPORT_SYMBOL(request_firmware_nowait);