blob: b7e571031ecd144e12b7b8c361a9e6fb6ffd4961 [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
David Woodhouse5658c762008-05-23 13:52:42 +010052#ifdef CONFIG_FW_LOADER
53extern struct builtin_fw __start_builtin_fw[];
54extern struct builtin_fw __end_builtin_fw[];
55#else /* Module case. Avoid ifdefs later; it'll all optimise out */
56static struct builtin_fw *__start_builtin_fw;
57static struct builtin_fw *__end_builtin_fw;
58#endif
59
Arjan van de Ven858119e2006-01-14 13:20:43 -080060static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070061fw_load_abort(struct firmware_priv *fw_priv)
62{
63 set_bit(FW_STATUS_ABORT, &fw_priv->status);
64 wmb();
65 complete(&fw_priv->completion);
66}
67
68static ssize_t
69firmware_timeout_show(struct class *class, char *buf)
70{
71 return sprintf(buf, "%d\n", loading_timeout);
72}
73
74/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -080075 * firmware_timeout_store - set number of seconds to wait for firmware
76 * @class: device class pointer
77 * @buf: buffer to scan for timeout value
78 * @count: number of bytes in @buf
79 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -080081 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * firmware will be provided.
83 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -080084 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 **/
86static ssize_t
87firmware_timeout_store(struct class *class, const char *buf, size_t count)
88{
89 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -070090 if (loading_timeout < 0)
91 loading_timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return count;
93}
94
95static CLASS_ATTR(timeout, 0644, firmware_timeout_show, firmware_timeout_store);
96
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -070097static void fw_dev_release(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Kay Sievers7eff2e72007-08-14 15:15:12 +020099static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700101 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Kay Sievers7eff2e72007-08-14 15:15:12 +0200103 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200105 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700106 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 return 0;
109}
110
Adrian Bunk1b81d662006-05-20 15:00:16 -0700111static struct class firmware_class = {
112 .name = "firmware",
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700113 .dev_uevent = firmware_uevent,
114 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700115};
116
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700117static ssize_t firmware_loading_show(struct device *dev,
118 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700120 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 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
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700127 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800128 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800129 * @buf: buffer to scan for loading control value
130 * @count: number of bytes in @buf
131 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 * The relevant values are:
133 *
134 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800135 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 * -1: Conclude the load with an error and discard any written data.
137 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700138static ssize_t firmware_loading_store(struct device *dev,
139 struct device_attribute *attr,
140 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700142 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 int loading = simple_strtol(buf, NULL, 10);
144
145 switch (loading) {
146 case 1:
Laura Garciacad1e552006-05-23 23:22:38 +0200147 mutex_lock(&fw_lock);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700148 if (!fw_priv->fw) {
Laura Garciacad1e552006-05-23 23:22:38 +0200149 mutex_unlock(&fw_lock);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700150 break;
151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 vfree(fw_priv->fw->data);
153 fw_priv->fw->data = NULL;
154 fw_priv->fw->size = 0;
155 fw_priv->alloc_size = 0;
156 set_bit(FW_STATUS_LOADING, &fw_priv->status);
Laura Garciacad1e552006-05-23 23:22:38 +0200157 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 break;
159 case 0:
160 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
161 complete(&fw_priv->completion);
162 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
163 break;
164 }
165 /* fallthrough */
166 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700167 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 /* fallthrough */
169 case -1:
170 fw_load_abort(fw_priv);
171 break;
172 }
173
174 return count;
175}
176
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700177static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800180firmware_data_read(struct kobject *kobj, struct bin_attribute *bin_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 char *buffer, loff_t offset, size_t count)
182{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700183 struct device *dev = to_dev(kobj);
184 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 struct firmware *fw;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700186 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
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 }
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700194 ret_count = memory_read_from_buffer(buffer, count, &offset,
195 fw->data, fw->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196out:
Laura Garciacad1e552006-05-23 23:22:38 +0200197 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return ret_count;
199}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static int
202fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
203{
204 u8 *new_data;
Jeff Moyer30560ba2006-02-13 14:52:38 -0800205 int new_size = fw_priv->alloc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 if (min_size <= fw_priv->alloc_size)
208 return 0;
209
Jeff Moyer30560ba2006-02-13 14:52:38 -0800210 new_size = ALIGN(min_size, PAGE_SIZE);
211 new_data = vmalloc(new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (!new_data) {
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800213 printk(KERN_ERR "%s: unable to alloc buffer\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 /* Make sure that we don't keep incomplete data */
215 fw_load_abort(fw_priv);
216 return -ENOMEM;
217 }
Jeff Moyer30560ba2006-02-13 14:52:38 -0800218 fw_priv->alloc_size = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (fw_priv->fw->data) {
220 memcpy(new_data, fw_priv->fw->data, fw_priv->fw->size);
221 vfree(fw_priv->fw->data);
222 }
223 fw_priv->fw->data = new_data;
224 BUG_ON(min_size > fw_priv->alloc_size);
225 return 0;
226}
227
228/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800229 * firmware_data_write - write method for firmware
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700230 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700231 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800232 * @buffer: buffer being written
233 * @offset: buffer offset for write in total data store area
234 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800236 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 * the driver as a firmware image.
238 **/
239static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800240firmware_data_write(struct kobject *kobj, struct bin_attribute *bin_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 char *buffer, loff_t offset, size_t count)
242{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700243 struct device *dev = to_dev(kobj);
244 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 struct firmware *fw;
246 ssize_t retval;
247
248 if (!capable(CAP_SYS_RAWIO))
249 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700250
Laura Garciacad1e552006-05-23 23:22:38 +0200251 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 fw = fw_priv->fw;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700253 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 retval = -ENODEV;
255 goto out;
256 }
257 retval = fw_realloc_buffer(fw_priv, offset + count);
258 if (retval)
259 goto out;
260
David Woodhouseb7a39bd2008-05-23 18:38:49 +0100261 memcpy((u8 *)fw->data + offset, buffer, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 fw->size = max_t(size_t, offset + count, fw->size);
264 retval = count;
265out:
Laura Garciacad1e552006-05-23 23:22:38 +0200266 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return retval;
268}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270static struct bin_attribute firmware_attr_data_tmpl = {
Tejun Heo7b595752007-06-14 03:45:17 +0900271 .attr = {.name = "data", .mode = 0644},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 .size = 0,
273 .read = firmware_data_read,
274 .write = firmware_data_write,
275};
276
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700277static void fw_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700279 struct firmware_priv *fw_priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 kfree(fw_priv);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700282 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 module_put(THIS_MODULE);
285}
286
287static void
288firmware_class_timeout(u_long data)
289{
290 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
291 fw_load_abort(fw_priv);
292}
293
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700294static inline void fw_setup_device_id(struct device *f_dev, struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Michael E Brown7d640c42008-01-29 15:35:01 -0600296 /* XXX warning we should watch out for name collisions */
297 strlcpy(f_dev->bus_id, dev->bus_id, BUS_ID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700300static int fw_register_device(struct device **dev_p, const char *fw_name,
301 struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 int retval;
Jiri Slaby4aed0642005-09-13 01:25:01 -0700304 struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 GFP_KERNEL);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700306 struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700308 *dev_p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700310 if (!fw_priv || !f_dev) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700311 dev_err(device, "%s: kmalloc failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 retval = -ENOMEM;
313 goto error_kfree;
314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 init_completion(&fw_priv->completion);
317 fw_priv->attr_data = firmware_attr_data_tmpl;
318 strlcpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX);
319
320 fw_priv->timeout.function = firmware_class_timeout;
321 fw_priv->timeout.data = (u_long) fw_priv;
322 init_timer(&fw_priv->timeout);
323
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700324 fw_setup_device_id(f_dev, device);
325 f_dev->parent = device;
326 f_dev->class = &firmware_class;
327 dev_set_drvdata(f_dev, fw_priv);
Cornelia Huckbdc49602007-03-29 11:12:14 +0200328 f_dev->uevent_suppress = 1;
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700329 retval = device_register(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (retval) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700331 dev_err(device, "%s: device_register failed\n", __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) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700364 dev_err(device, "%s: sysfs_create_bin_file failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 goto error_unreg;
366 }
367
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700368 retval = device_create_file(f_dev, &dev_attr_loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (retval) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700370 dev_err(device, "%s: device_create_file failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 goto error_unreg;
372 }
373
Kay Sievers312c0042005-11-16 09:00:00 +0100374 if (uevent)
Cornelia Huckbdc49602007-03-29 11:12:14 +0200375 f_dev->uevent_suppress = 0;
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700376 *dev_p = f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 goto out;
378
379error_unreg:
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700380 device_unregister(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381out:
382 return retval;
383}
384
Abhay Salunke6e3eaab2005-09-06 15:17:13 -0700385static int
386_request_firmware(const struct firmware **firmware_p, const char *name,
Kay Sievers312c0042005-11-16 09:00:00 +0100387 struct device *device, int uevent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700389 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 struct firmware_priv *fw_priv;
391 struct firmware *firmware;
David Woodhouse5658c762008-05-23 13:52:42 +0100392 struct builtin_fw *builtin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 int retval;
394
395 if (!firmware_p)
396 return -EINVAL;
397
Jiri Slaby4aed0642005-09-13 01:25:01 -0700398 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700400 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
401 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 retval = -ENOMEM;
403 goto out;
404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
David Woodhouse5658c762008-05-23 13:52:42 +0100406 for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
407 builtin++) {
408 if (strcmp(name, builtin->name))
409 continue;
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700410 dev_info(device, "firmware: using built-in firmware %s\n",
411 name);
David Woodhouse5658c762008-05-23 13:52:42 +0100412 firmware->size = builtin->size;
413 firmware->data = builtin->data;
414 return 0;
415 }
416
417 if (uevent)
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700418 dev_info(device, "firmware: requesting %s\n", name);
David Woodhouse5658c762008-05-23 13:52:42 +0100419
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700420 retval = fw_setup_device(firmware, &f_dev, name, device, uevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (retval)
422 goto error_kfree_fw;
423
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700424 fw_priv = dev_get_drvdata(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
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
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700432 kobject_uevent(&f_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);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700447 device_unregister(f_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 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{
David Woodhouse5658c762008-05-23 13:52:42 +0100487 struct builtin_fw *builtin;
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (fw) {
David Woodhouse5658c762008-05-23 13:52:42 +0100490 for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
491 builtin++) {
492 if (fw->data == builtin->data)
493 goto free_fw;
494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 vfree(fw->data);
David Woodhouse5658c762008-05-23 13:52:42 +0100496 free_fw:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 kfree(fw);
498 }
499}
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501/* Async support */
502struct firmware_work {
503 struct work_struct work;
504 struct module *module;
505 const char *name;
506 struct device *device;
507 void *context;
508 void (*cont)(const struct firmware *fw, void *context);
Kay Sievers312c0042005-11-16 09:00:00 +0100509 int uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510};
511
512static int
513request_firmware_work_func(void *arg)
514{
515 struct firmware_work *fw_work = arg;
516 const struct firmware *fw;
matthieu castet113fab12005-11-13 16:07:39 -0800517 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (!arg) {
519 WARN_ON(1);
520 return 0;
521 }
matthieu castet113fab12005-11-13 16:07:39 -0800522 ret = _request_firmware(&fw, fw_work->name, fw_work->device,
Kay Sievers312c0042005-11-16 09:00:00 +0100523 fw_work->uevent);
matthieu castet113fab12005-11-13 16:07:39 -0800524 if (ret < 0)
525 fw_work->cont(NULL, fw_work->context);
526 else {
527 fw_work->cont(fw, fw_work->context);
528 release_firmware(fw);
529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 module_put(fw_work->module);
531 kfree(fw_work);
matthieu castet113fab12005-11-13 16:07:39 -0800532 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
535/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800536 * request_firmware_nowait: asynchronous version of request_firmware
537 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +0100538 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800539 * is non-zero else the firmware copy must be done manually.
540 * @name: name of firmware file
541 * @device: device for which firmware is being loaded
542 * @context: will be passed over to @cont, and
543 * @fw may be %NULL if firmware request fails.
544 * @cont: function will be called asynchronously when the firmware
545 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 * Asynchronous variant of request_firmware() for contexts where
548 * it is not possible to sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 **/
550int
551request_firmware_nowait(
Kay Sievers312c0042005-11-16 09:00:00 +0100552 struct module *module, int uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 const char *name, struct device *device, void *context,
554 void (*cont)(const struct firmware *fw, void *context))
555{
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700556 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
558 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 if (!fw_work)
561 return -ENOMEM;
562 if (!try_module_get(module)) {
563 kfree(fw_work);
564 return -EFAULT;
565 }
566
567 *fw_work = (struct firmware_work) {
568 .module = module,
569 .name = name,
570 .device = device,
571 .context = context,
572 .cont = cont,
Kay Sievers312c0042005-11-16 09:00:00 +0100573 .uevent = uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 };
575
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700576 task = kthread_run(request_firmware_work_func, fw_work,
577 "firmware/%s", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700579 if (IS_ERR(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 fw_work->cont(NULL, fw_work->context);
matthieu castet113fab12005-11-13 16:07:39 -0800581 module_put(fw_work->module);
582 kfree(fw_work);
Sukadev Bhattiprolu563d0752006-09-29 01:59:31 -0700583 return PTR_ERR(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585 return 0;
586}
587
588static int __init
589firmware_class_init(void)
590{
591 int error;
592 error = class_register(&firmware_class);
593 if (error) {
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800594 printk(KERN_ERR "%s: class_register failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return error;
596 }
597 error = class_create_file(&firmware_class, &class_attr_timeout);
598 if (error) {
599 printk(KERN_ERR "%s: class_create_file failed\n",
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800600 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 class_unregister(&firmware_class);
602 }
603 return error;
604
605}
606static void __exit
607firmware_class_exit(void)
608{
609 class_unregister(&firmware_class);
610}
611
Shaohua Lia30a6a22006-09-27 01:50:52 -0700612fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613module_exit(firmware_class_exit);
614
615EXPORT_SYMBOL(release_firmware);
616EXPORT_SYMBOL(request_firmware);
617EXPORT_SYMBOL(request_firmware_nowait);