Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * firmware_class.c - Multi purpose firmware loading support |
| 3 | * |
Markus Rechberger | 87d37a4 | 2007-06-04 18:45:44 +0200 | [diff] [blame] | 4 | * Copyright (c) 2003 Manuel Estrada Sainz |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * Please see Documentation/firmware_class/ for more information. |
| 7 | * |
| 8 | */ |
| 9 | |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 10 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #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 Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 18 | #include <linux/mutex.h> |
Sukadev Bhattiprolu | 563d075 | 2006-09-29 01:59:31 -0700 | [diff] [blame] | 19 | #include <linux/kthread.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | #include <linux/firmware.h> |
| 22 | #include "base.h" |
| 23 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 24 | #define to_dev(obj) container_of(obj, struct device, kobj) |
| 25 | |
Markus Rechberger | 87d37a4 | 2007-06-04 18:45:44 +0200 | [diff] [blame] | 26 | MODULE_AUTHOR("Manuel Estrada Sainz"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | MODULE_DESCRIPTION("Multi purpose firmware loading support"); |
| 28 | MODULE_LICENSE("GPL"); |
| 29 | |
| 30 | enum { |
| 31 | FW_STATUS_LOADING, |
| 32 | FW_STATUS_DONE, |
| 33 | FW_STATUS_ABORT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
Dave Jones | 2f65168 | 2007-01-25 15:56:15 -0500 | [diff] [blame] | 36 | static int loading_timeout = 60; /* In seconds */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | /* fw_lock could be moved to 'struct firmware_priv' but since it is just |
| 39 | * guarding for corner cases a global lock should be OK */ |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 40 | static DEFINE_MUTEX(fw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | struct 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 Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 52 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | fw_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 | |
| 60 | static ssize_t |
| 61 | firmware_timeout_show(struct class *class, char *buf) |
| 62 | { |
| 63 | return sprintf(buf, "%d\n", loading_timeout); |
| 64 | } |
| 65 | |
| 66 | /** |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 67 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | * Sets the number of seconds to wait for the firmware. Once |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 73 | * this expires an error will be returned to the driver and no |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | * firmware will be provided. |
| 75 | * |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 76 | * Note: zero means 'wait forever'. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | **/ |
| 78 | static ssize_t |
| 79 | firmware_timeout_store(struct class *class, const char *buf, size_t count) |
| 80 | { |
| 81 | loading_timeout = simple_strtol(buf, NULL, 10); |
Stanislaw W. Gruszka | b92eac0 | 2005-06-28 20:44:51 -0700 | [diff] [blame] | 82 | if (loading_timeout < 0) |
| 83 | loading_timeout = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | return count; |
| 85 | } |
| 86 | |
| 87 | static CLASS_ATTR(timeout, 0644, firmware_timeout_show, firmware_timeout_store); |
| 88 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 89 | static void fw_dev_release(struct device *dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 91 | static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | { |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 93 | struct firmware_priv *fw_priv = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 95 | if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | return -ENOMEM; |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 97 | if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout)) |
kay.sievers@vrfy.org | 6897089 | 2005-04-18 21:57:31 -0700 | [diff] [blame] | 98 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
Adrian Bunk | 1b81d66 | 2006-05-20 15:00:16 -0700 | [diff] [blame] | 103 | static struct class firmware_class = { |
| 104 | .name = "firmware", |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 105 | .dev_uevent = firmware_uevent, |
| 106 | .dev_release = fw_dev_release, |
Adrian Bunk | 1b81d66 | 2006-05-20 15:00:16 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 109 | static ssize_t firmware_loading_show(struct device *dev, |
| 110 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | { |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 112 | struct firmware_priv *fw_priv = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status); |
| 114 | return sprintf(buf, "%d\n", loading); |
| 115 | } |
| 116 | |
| 117 | /** |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 118 | * firmware_loading_store - set value in the 'loading' control file |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 119 | * @dev: device pointer |
Randy Dunlap | af9997e | 2006-12-22 01:06:52 -0800 | [diff] [blame] | 120 | * @attr: device attribute pointer |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 121 | * @buf: buffer to scan for loading control value |
| 122 | * @count: number of bytes in @buf |
| 123 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | * The relevant values are: |
| 125 | * |
| 126 | * 1: Start a load, discarding any previous partial load. |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 127 | * 0: Conclude the load and hand the data to the driver code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | * -1: Conclude the load with an error and discard any written data. |
| 129 | **/ |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 130 | static ssize_t firmware_loading_store(struct device *dev, |
| 131 | struct device_attribute *attr, |
| 132 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 134 | struct firmware_priv *fw_priv = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | int loading = simple_strtol(buf, NULL, 10); |
| 136 | |
| 137 | switch (loading) { |
| 138 | case 1: |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 139 | mutex_lock(&fw_lock); |
Stanislaw W. Gruszka | b92eac0 | 2005-06-28 20:44:51 -0700 | [diff] [blame] | 140 | if (!fw_priv->fw) { |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 141 | mutex_unlock(&fw_lock); |
Stanislaw W. Gruszka | b92eac0 | 2005-06-28 20:44:51 -0700 | [diff] [blame] | 142 | break; |
| 143 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | 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 Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 149 | mutex_unlock(&fw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | 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: |
| 159 | printk(KERN_ERR "%s: unexpected value (%d)\n", __FUNCTION__, |
| 160 | loading); |
| 161 | /* fallthrough */ |
| 162 | case -1: |
| 163 | fw_load_abort(fw_priv); |
| 164 | break; |
| 165 | } |
| 166 | |
| 167 | return count; |
| 168 | } |
| 169 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 170 | static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
| 172 | static ssize_t |
Zhang Rui | 91a6902 | 2007-06-09 13:57:22 +0800 | [diff] [blame] | 173 | firmware_data_read(struct kobject *kobj, struct bin_attribute *bin_attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | char *buffer, loff_t offset, size_t count) |
| 175 | { |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 176 | struct device *dev = to_dev(kobj); |
| 177 | struct firmware_priv *fw_priv = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | struct firmware *fw; |
| 179 | ssize_t ret_count = count; |
| 180 | |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 181 | mutex_lock(&fw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | fw = fw_priv->fw; |
Stanislaw W. Gruszka | b92eac0 | 2005-06-28 20:44:51 -0700 | [diff] [blame] | 183 | if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | 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); |
| 195 | out: |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 196 | mutex_unlock(&fw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | return ret_count; |
| 198 | } |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | static int |
| 201 | fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size) |
| 202 | { |
| 203 | u8 *new_data; |
Jeff Moyer | 30560ba | 2006-02-13 14:52:38 -0800 | [diff] [blame] | 204 | int new_size = fw_priv->alloc_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
| 206 | if (min_size <= fw_priv->alloc_size) |
| 207 | return 0; |
| 208 | |
Jeff Moyer | 30560ba | 2006-02-13 14:52:38 -0800 | [diff] [blame] | 209 | new_size = ALIGN(min_size, PAGE_SIZE); |
| 210 | new_data = vmalloc(new_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | if (!new_data) { |
| 212 | printk(KERN_ERR "%s: unable to alloc buffer\n", __FUNCTION__); |
| 213 | /* Make sure that we don't keep incomplete data */ |
| 214 | fw_load_abort(fw_priv); |
| 215 | return -ENOMEM; |
| 216 | } |
Jeff Moyer | 30560ba | 2006-02-13 14:52:38 -0800 | [diff] [blame] | 217 | fw_priv->alloc_size = new_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | 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 Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 228 | * firmware_data_write - write method for firmware |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 229 | * @kobj: kobject for the device |
Randy Dunlap | 42e61f4 | 2007-07-23 21:42:11 -0700 | [diff] [blame] | 230 | * @bin_attr: bin_attr structure |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 231 | * @buffer: buffer being written |
| 232 | * @offset: buffer offset for write in total data store area |
| 233 | * @count: buffer size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | * |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 235 | * Data written to the 'data' attribute will be later handed to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | * the driver as a firmware image. |
| 237 | **/ |
| 238 | static ssize_t |
Zhang Rui | 91a6902 | 2007-06-09 13:57:22 +0800 | [diff] [blame] | 239 | firmware_data_write(struct kobject *kobj, struct bin_attribute *bin_attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | char *buffer, loff_t offset, size_t count) |
| 241 | { |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 242 | struct device *dev = to_dev(kobj); |
| 243 | struct firmware_priv *fw_priv = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | struct firmware *fw; |
| 245 | ssize_t retval; |
| 246 | |
| 247 | if (!capable(CAP_SYS_RAWIO)) |
| 248 | return -EPERM; |
Stanislaw W. Gruszka | b92eac0 | 2005-06-28 20:44:51 -0700 | [diff] [blame] | 249 | |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 250 | mutex_lock(&fw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | fw = fw_priv->fw; |
Stanislaw W. Gruszka | b92eac0 | 2005-06-28 20:44:51 -0700 | [diff] [blame] | 252 | if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | 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; |
| 264 | out: |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 265 | mutex_unlock(&fw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | return retval; |
| 267 | } |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | static struct bin_attribute firmware_attr_data_tmpl = { |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 270 | .attr = {.name = "data", .mode = 0644}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | .size = 0, |
| 272 | .read = firmware_data_read, |
| 273 | .write = firmware_data_write, |
| 274 | }; |
| 275 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 276 | static void fw_dev_release(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | { |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 278 | struct firmware_priv *fw_priv = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
| 280 | kfree(fw_priv); |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 281 | kfree(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | |
| 283 | module_put(THIS_MODULE); |
| 284 | } |
| 285 | |
| 286 | static void |
| 287 | firmware_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-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 293 | static inline void fw_setup_device_id(struct device *f_dev, struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | { |
Michael E Brown | 7d640c4 | 2008-01-29 15:35:01 -0600 | [diff] [blame] | 295 | /* XXX warning we should watch out for name collisions */ |
| 296 | strlcpy(f_dev->bus_id, dev->bus_id, BUS_ID_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 299 | static int fw_register_device(struct device **dev_p, const char *fw_name, |
| 300 | struct device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | { |
| 302 | int retval; |
Jiri Slaby | 4aed064 | 2005-09-13 01:25:01 -0700 | [diff] [blame] | 303 | struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | GFP_KERNEL); |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 305 | struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 307 | *dev_p = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 309 | if (!fw_priv || !f_dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | printk(KERN_ERR "%s: kmalloc failed\n", __FUNCTION__); |
| 311 | retval = -ENOMEM; |
| 312 | goto error_kfree; |
| 313 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | |
| 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-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 323 | 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 Huck | bdc4960 | 2007-03-29 11:12:14 +0200 | [diff] [blame] | 327 | f_dev->uevent_suppress = 1; |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 328 | retval = device_register(f_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | if (retval) { |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 330 | printk(KERN_ERR "%s: device_register failed\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | __FUNCTION__); |
| 332 | goto error_kfree; |
| 333 | } |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 334 | *dev_p = f_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | return 0; |
| 336 | |
| 337 | error_kfree: |
| 338 | kfree(fw_priv); |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 339 | kfree(f_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | return retval; |
| 341 | } |
| 342 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 343 | static int fw_setup_device(struct firmware *fw, struct device **dev_p, |
| 344 | const char *fw_name, struct device *device, |
| 345 | int uevent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | { |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 347 | struct device *f_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | struct firmware_priv *fw_priv; |
| 349 | int retval; |
| 350 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 351 | *dev_p = NULL; |
| 352 | retval = fw_register_device(&f_dev, fw_name, device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | 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-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 359 | fw_priv = dev_get_drvdata(f_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
| 361 | fw_priv->fw = fw; |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 362 | retval = sysfs_create_bin_file(&f_dev->kobj, &fw_priv->attr_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | if (retval) { |
| 364 | printk(KERN_ERR "%s: sysfs_create_bin_file failed\n", |
| 365 | __FUNCTION__); |
| 366 | goto error_unreg; |
| 367 | } |
| 368 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 369 | retval = device_create_file(f_dev, &dev_attr_loading); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | if (retval) { |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 371 | printk(KERN_ERR "%s: device_create_file failed\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | __FUNCTION__); |
| 373 | goto error_unreg; |
| 374 | } |
| 375 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 376 | if (uevent) |
Cornelia Huck | bdc4960 | 2007-03-29 11:12:14 +0200 | [diff] [blame] | 377 | f_dev->uevent_suppress = 0; |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 378 | *dev_p = f_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | goto out; |
| 380 | |
| 381 | error_unreg: |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 382 | device_unregister(f_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | out: |
| 384 | return retval; |
| 385 | } |
| 386 | |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 387 | static int |
| 388 | _request_firmware(const struct firmware **firmware_p, const char *name, |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 389 | struct device *device, int uevent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | { |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 391 | struct device *f_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | struct firmware_priv *fw_priv; |
| 393 | struct firmware *firmware; |
| 394 | int retval; |
| 395 | |
| 396 | if (!firmware_p) |
| 397 | return -EINVAL; |
| 398 | |
Jiri Slaby | 4aed064 | 2005-09-13 01:25:01 -0700 | [diff] [blame] | 399 | *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | if (!firmware) { |
| 401 | printk(KERN_ERR "%s: kmalloc(struct firmware) failed\n", |
| 402 | __FUNCTION__); |
| 403 | retval = -ENOMEM; |
| 404 | goto out; |
| 405 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 407 | retval = fw_setup_device(firmware, &f_dev, name, device, uevent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | if (retval) |
| 409 | goto error_kfree_fw; |
| 410 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 411 | fw_priv = dev_get_drvdata(f_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 413 | if (uevent) { |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 414 | if (loading_timeout > 0) { |
| 415 | fw_priv->timeout.expires = jiffies + loading_timeout * HZ; |
| 416 | add_timer(&fw_priv->timeout); |
| 417 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 419 | kobject_uevent(&f_dev->kobj, KOBJ_ADD); |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 420 | wait_for_completion(&fw_priv->completion); |
| 421 | set_bit(FW_STATUS_DONE, &fw_priv->status); |
| 422 | del_timer_sync(&fw_priv->timeout); |
| 423 | } else |
| 424 | wait_for_completion(&fw_priv->completion); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 426 | mutex_lock(&fw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) { |
| 428 | retval = -ENOENT; |
| 429 | release_firmware(fw_priv->fw); |
| 430 | *firmware_p = NULL; |
| 431 | } |
| 432 | fw_priv->fw = NULL; |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 433 | mutex_unlock(&fw_lock); |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 434 | device_unregister(f_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | goto out; |
| 436 | |
| 437 | error_kfree_fw: |
| 438 | kfree(firmware); |
| 439 | *firmware_p = NULL; |
| 440 | out: |
| 441 | return retval; |
| 442 | } |
| 443 | |
| 444 | /** |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 445 | * request_firmware: - send firmware request and wait for it |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 446 | * @firmware_p: pointer to firmware image |
| 447 | * @name: name of firmware file |
| 448 | * @device: device for which firmware is being loaded |
| 449 | * |
| 450 | * @firmware_p will be used to return a firmware image by the name |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 451 | * of @name for device @device. |
| 452 | * |
| 453 | * Should be called from user context where sleeping is allowed. |
| 454 | * |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 455 | * @name will be used as $FIRMWARE in the uevent environment and |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 456 | * should be distinctive enough not to be confused with any other |
| 457 | * firmware image for this or any other device. |
| 458 | **/ |
| 459 | int |
| 460 | request_firmware(const struct firmware **firmware_p, const char *name, |
| 461 | struct device *device) |
| 462 | { |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 463 | int uevent = 1; |
| 464 | return _request_firmware(firmware_p, name, device, uevent); |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | * release_firmware: - release the resource associated with a firmware image |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 469 | * @fw: firmware resource to release |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | **/ |
| 471 | void |
| 472 | release_firmware(const struct firmware *fw) |
| 473 | { |
| 474 | if (fw) { |
| 475 | vfree(fw->data); |
| 476 | kfree(fw); |
| 477 | } |
| 478 | } |
| 479 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | /* Async support */ |
| 481 | struct firmware_work { |
| 482 | struct work_struct work; |
| 483 | struct module *module; |
| 484 | const char *name; |
| 485 | struct device *device; |
| 486 | void *context; |
| 487 | void (*cont)(const struct firmware *fw, void *context); |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 488 | int uevent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | }; |
| 490 | |
| 491 | static int |
| 492 | request_firmware_work_func(void *arg) |
| 493 | { |
| 494 | struct firmware_work *fw_work = arg; |
| 495 | const struct firmware *fw; |
matthieu castet | 113fab1 | 2005-11-13 16:07:39 -0800 | [diff] [blame] | 496 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | if (!arg) { |
| 498 | WARN_ON(1); |
| 499 | return 0; |
| 500 | } |
matthieu castet | 113fab1 | 2005-11-13 16:07:39 -0800 | [diff] [blame] | 501 | ret = _request_firmware(&fw, fw_work->name, fw_work->device, |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 502 | fw_work->uevent); |
matthieu castet | 113fab1 | 2005-11-13 16:07:39 -0800 | [diff] [blame] | 503 | if (ret < 0) |
| 504 | fw_work->cont(NULL, fw_work->context); |
| 505 | else { |
| 506 | fw_work->cont(fw, fw_work->context); |
| 507 | release_firmware(fw); |
| 508 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | module_put(fw_work->module); |
| 510 | kfree(fw_work); |
matthieu castet | 113fab1 | 2005-11-13 16:07:39 -0800 | [diff] [blame] | 511 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | /** |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 515 | * request_firmware_nowait: asynchronous version of request_firmware |
| 516 | * @module: module requesting the firmware |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 517 | * @uevent: sends uevent to copy the firmware image if this flag |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 518 | * is non-zero else the firmware copy must be done manually. |
| 519 | * @name: name of firmware file |
| 520 | * @device: device for which firmware is being loaded |
| 521 | * @context: will be passed over to @cont, and |
| 522 | * @fw may be %NULL if firmware request fails. |
| 523 | * @cont: function will be called asynchronously when the firmware |
| 524 | * request is over. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | * Asynchronous variant of request_firmware() for contexts where |
| 527 | * it is not possible to sleep. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | **/ |
| 529 | int |
| 530 | request_firmware_nowait( |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 531 | struct module *module, int uevent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | const char *name, struct device *device, void *context, |
| 533 | void (*cont)(const struct firmware *fw, void *context)) |
| 534 | { |
Sukadev Bhattiprolu | 563d075 | 2006-09-29 01:59:31 -0700 | [diff] [blame] | 535 | struct task_struct *task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work), |
| 537 | GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
| 539 | if (!fw_work) |
| 540 | return -ENOMEM; |
| 541 | if (!try_module_get(module)) { |
| 542 | kfree(fw_work); |
| 543 | return -EFAULT; |
| 544 | } |
| 545 | |
| 546 | *fw_work = (struct firmware_work) { |
| 547 | .module = module, |
| 548 | .name = name, |
| 549 | .device = device, |
| 550 | .context = context, |
| 551 | .cont = cont, |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 552 | .uevent = uevent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | }; |
| 554 | |
Sukadev Bhattiprolu | 563d075 | 2006-09-29 01:59:31 -0700 | [diff] [blame] | 555 | task = kthread_run(request_firmware_work_func, fw_work, |
| 556 | "firmware/%s", name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | |
Sukadev Bhattiprolu | 563d075 | 2006-09-29 01:59:31 -0700 | [diff] [blame] | 558 | if (IS_ERR(task)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | fw_work->cont(NULL, fw_work->context); |
matthieu castet | 113fab1 | 2005-11-13 16:07:39 -0800 | [diff] [blame] | 560 | module_put(fw_work->module); |
| 561 | kfree(fw_work); |
Sukadev Bhattiprolu | 563d075 | 2006-09-29 01:59:31 -0700 | [diff] [blame] | 562 | return PTR_ERR(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | } |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | static int __init |
| 568 | firmware_class_init(void) |
| 569 | { |
| 570 | int error; |
| 571 | error = class_register(&firmware_class); |
| 572 | if (error) { |
| 573 | printk(KERN_ERR "%s: class_register failed\n", __FUNCTION__); |
| 574 | return error; |
| 575 | } |
| 576 | error = class_create_file(&firmware_class, &class_attr_timeout); |
| 577 | if (error) { |
| 578 | printk(KERN_ERR "%s: class_create_file failed\n", |
| 579 | __FUNCTION__); |
| 580 | class_unregister(&firmware_class); |
| 581 | } |
| 582 | return error; |
| 583 | |
| 584 | } |
| 585 | static void __exit |
| 586 | firmware_class_exit(void) |
| 587 | { |
| 588 | class_unregister(&firmware_class); |
| 589 | } |
| 590 | |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 591 | fs_initcall(firmware_class_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | module_exit(firmware_class_exit); |
| 593 | |
| 594 | EXPORT_SYMBOL(release_firmware); |
| 595 | EXPORT_SYMBOL(request_firmware); |
| 596 | EXPORT_SYMBOL(request_firmware_nowait); |