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