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 | |
| 10 | #include <linux/device.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/timer.h> |
| 14 | #include <linux/vmalloc.h> |
| 15 | #include <linux/interrupt.h> |
| 16 | #include <linux/bitops.h> |
| 17 | #include <asm/semaphore.h> |
| 18 | |
| 19 | #include <linux/firmware.h> |
| 20 | #include "base.h" |
| 21 | |
| 22 | MODULE_AUTHOR("Manuel Estrada Sainz <ranty@debian.org>"); |
| 23 | MODULE_DESCRIPTION("Multi purpose firmware loading support"); |
| 24 | MODULE_LICENSE("GPL"); |
| 25 | |
| 26 | enum { |
| 27 | FW_STATUS_LOADING, |
| 28 | FW_STATUS_DONE, |
| 29 | FW_STATUS_ABORT, |
| 30 | FW_STATUS_READY, |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 31 | FW_STATUS_READY_NOHOTPLUG, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | static int loading_timeout = 10; /* In seconds */ |
| 35 | |
| 36 | /* fw_lock could be moved to 'struct firmware_priv' but since it is just |
| 37 | * guarding for corner cases a global lock should be OK */ |
| 38 | static DECLARE_MUTEX(fw_lock); |
| 39 | |
| 40 | struct firmware_priv { |
| 41 | char fw_id[FIRMWARE_NAME_MAX]; |
| 42 | struct completion completion; |
| 43 | struct bin_attribute attr_data; |
| 44 | struct firmware *fw; |
| 45 | unsigned long status; |
| 46 | int alloc_size; |
| 47 | struct timer_list timeout; |
| 48 | }; |
| 49 | |
| 50 | static inline void |
| 51 | fw_load_abort(struct firmware_priv *fw_priv) |
| 52 | { |
| 53 | set_bit(FW_STATUS_ABORT, &fw_priv->status); |
| 54 | wmb(); |
| 55 | complete(&fw_priv->completion); |
| 56 | } |
| 57 | |
| 58 | static ssize_t |
| 59 | firmware_timeout_show(struct class *class, char *buf) |
| 60 | { |
| 61 | return sprintf(buf, "%d\n", loading_timeout); |
| 62 | } |
| 63 | |
| 64 | /** |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 65 | * firmware_timeout_store - set number of seconds to wait for firmware |
| 66 | * @class: device class pointer |
| 67 | * @buf: buffer to scan for timeout value |
| 68 | * @count: number of bytes in @buf |
| 69 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | * Sets the number of seconds to wait for the firmware. Once |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 71 | * this expires an error will be returned to the driver and no |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | * firmware will be provided. |
| 73 | * |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 74 | * Note: zero means 'wait forever'. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | **/ |
| 76 | static ssize_t |
| 77 | firmware_timeout_store(struct class *class, const char *buf, size_t count) |
| 78 | { |
| 79 | loading_timeout = simple_strtol(buf, NULL, 10); |
Stanislaw W. Gruszka | b92eac0 | 2005-06-28 20:44:51 -0700 | [diff] [blame] | 80 | if (loading_timeout < 0) |
| 81 | loading_timeout = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | return count; |
| 83 | } |
| 84 | |
| 85 | static CLASS_ATTR(timeout, 0644, firmware_timeout_show, firmware_timeout_store); |
| 86 | |
| 87 | static void fw_class_dev_release(struct class_device *class_dev); |
| 88 | int firmware_class_hotplug(struct class_device *dev, char **envp, |
| 89 | int num_envp, char *buffer, int buffer_size); |
| 90 | |
| 91 | static struct class firmware_class = { |
| 92 | .name = "firmware", |
| 93 | .hotplug = firmware_class_hotplug, |
| 94 | .release = fw_class_dev_release, |
| 95 | }; |
| 96 | |
| 97 | int |
| 98 | firmware_class_hotplug(struct class_device *class_dev, char **envp, |
| 99 | int num_envp, char *buffer, int buffer_size) |
| 100 | { |
| 101 | struct firmware_priv *fw_priv = class_get_devdata(class_dev); |
| 102 | int i = 0, len = 0; |
| 103 | |
| 104 | if (!test_bit(FW_STATUS_READY, &fw_priv->status)) |
| 105 | return -ENODEV; |
| 106 | |
| 107 | if (add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size, &len, |
| 108 | "FIRMWARE=%s", fw_priv->fw_id)) |
| 109 | return -ENOMEM; |
kay.sievers@vrfy.org | 6897089 | 2005-04-18 21:57:31 -0700 | [diff] [blame] | 110 | if (add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size, &len, |
| 111 | "TIMEOUT=%i", loading_timeout)) |
| 112 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | envp[i] = NULL; |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static ssize_t |
| 120 | firmware_loading_show(struct class_device *class_dev, char *buf) |
| 121 | { |
| 122 | struct firmware_priv *fw_priv = class_get_devdata(class_dev); |
| 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 |
| 129 | * @class_dev: class_device pointer |
| 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 | **/ |
| 139 | static ssize_t |
| 140 | firmware_loading_store(struct class_device *class_dev, |
| 141 | const char *buf, size_t count) |
| 142 | { |
| 143 | struct firmware_priv *fw_priv = class_get_devdata(class_dev); |
| 144 | int loading = simple_strtol(buf, NULL, 10); |
| 145 | |
| 146 | switch (loading) { |
| 147 | case 1: |
| 148 | down(&fw_lock); |
Stanislaw W. Gruszka | b92eac0 | 2005-06-28 20:44:51 -0700 | [diff] [blame] | 149 | if (!fw_priv->fw) { |
| 150 | up(&fw_lock); |
| 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); |
| 158 | up(&fw_lock); |
| 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 | |
| 179 | static CLASS_DEVICE_ATTR(loading, 0644, |
| 180 | firmware_loading_show, firmware_loading_store); |
| 181 | |
| 182 | static ssize_t |
| 183 | firmware_data_read(struct kobject *kobj, |
| 184 | char *buffer, loff_t offset, size_t count) |
| 185 | { |
| 186 | struct class_device *class_dev = to_class_dev(kobj); |
| 187 | struct firmware_priv *fw_priv = class_get_devdata(class_dev); |
| 188 | struct firmware *fw; |
| 189 | ssize_t ret_count = count; |
| 190 | |
| 191 | down(&fw_lock); |
| 192 | fw = fw_priv->fw; |
Stanislaw W. Gruszka | b92eac0 | 2005-06-28 20:44:51 -0700 | [diff] [blame] | 193 | if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | ret_count = -ENODEV; |
| 195 | goto out; |
| 196 | } |
| 197 | if (offset > fw->size) { |
| 198 | ret_count = 0; |
| 199 | goto out; |
| 200 | } |
| 201 | if (offset + ret_count > fw->size) |
| 202 | ret_count = fw->size - offset; |
| 203 | |
| 204 | memcpy(buffer, fw->data + offset, ret_count); |
| 205 | out: |
| 206 | up(&fw_lock); |
| 207 | return ret_count; |
| 208 | } |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | static int |
| 211 | fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size) |
| 212 | { |
| 213 | u8 *new_data; |
| 214 | |
| 215 | if (min_size <= fw_priv->alloc_size) |
| 216 | return 0; |
| 217 | |
| 218 | new_data = vmalloc(fw_priv->alloc_size + PAGE_SIZE); |
| 219 | if (!new_data) { |
| 220 | printk(KERN_ERR "%s: unable to alloc buffer\n", __FUNCTION__); |
| 221 | /* Make sure that we don't keep incomplete data */ |
| 222 | fw_load_abort(fw_priv); |
| 223 | return -ENOMEM; |
| 224 | } |
| 225 | fw_priv->alloc_size += PAGE_SIZE; |
| 226 | if (fw_priv->fw->data) { |
| 227 | memcpy(new_data, fw_priv->fw->data, fw_priv->fw->size); |
| 228 | vfree(fw_priv->fw->data); |
| 229 | } |
| 230 | fw_priv->fw->data = new_data; |
| 231 | BUG_ON(min_size > fw_priv->alloc_size); |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | /** |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 236 | * firmware_data_write - write method for firmware |
| 237 | * @kobj: kobject for the class_device |
| 238 | * @buffer: buffer being written |
| 239 | * @offset: buffer offset for write in total data store area |
| 240 | * @count: buffer size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | * |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 242 | * Data written to the 'data' attribute will be later handed to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | * the driver as a firmware image. |
| 244 | **/ |
| 245 | static ssize_t |
| 246 | firmware_data_write(struct kobject *kobj, |
| 247 | char *buffer, loff_t offset, size_t count) |
| 248 | { |
| 249 | struct class_device *class_dev = to_class_dev(kobj); |
| 250 | struct firmware_priv *fw_priv = class_get_devdata(class_dev); |
| 251 | struct firmware *fw; |
| 252 | ssize_t retval; |
| 253 | |
| 254 | if (!capable(CAP_SYS_RAWIO)) |
| 255 | return -EPERM; |
Stanislaw W. Gruszka | b92eac0 | 2005-06-28 20:44:51 -0700 | [diff] [blame] | 256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | down(&fw_lock); |
| 258 | fw = fw_priv->fw; |
Stanislaw W. Gruszka | b92eac0 | 2005-06-28 20:44:51 -0700 | [diff] [blame] | 259 | if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | retval = -ENODEV; |
| 261 | goto out; |
| 262 | } |
| 263 | retval = fw_realloc_buffer(fw_priv, offset + count); |
| 264 | if (retval) |
| 265 | goto out; |
| 266 | |
| 267 | memcpy(fw->data + offset, buffer, count); |
| 268 | |
| 269 | fw->size = max_t(size_t, offset + count, fw->size); |
| 270 | retval = count; |
| 271 | out: |
| 272 | up(&fw_lock); |
| 273 | return retval; |
| 274 | } |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | static struct bin_attribute firmware_attr_data_tmpl = { |
| 277 | .attr = {.name = "data", .mode = 0644, .owner = THIS_MODULE}, |
| 278 | .size = 0, |
| 279 | .read = firmware_data_read, |
| 280 | .write = firmware_data_write, |
| 281 | }; |
| 282 | |
| 283 | static void |
| 284 | fw_class_dev_release(struct class_device *class_dev) |
| 285 | { |
| 286 | struct firmware_priv *fw_priv = class_get_devdata(class_dev); |
| 287 | |
| 288 | kfree(fw_priv); |
| 289 | kfree(class_dev); |
| 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 | |
| 301 | static inline void |
| 302 | fw_setup_class_device_id(struct class_device *class_dev, struct device *dev) |
| 303 | { |
| 304 | /* XXX warning we should watch out for name collisions */ |
| 305 | strlcpy(class_dev->class_id, dev->bus_id, BUS_ID_SIZE); |
| 306 | } |
| 307 | |
| 308 | static int |
| 309 | fw_register_class_device(struct class_device **class_dev_p, |
| 310 | const char *fw_name, struct device *device) |
| 311 | { |
| 312 | int retval; |
Jiri Slaby | 4aed064 | 2005-09-13 01:25:01 -0700 | [diff] [blame] | 313 | struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | GFP_KERNEL); |
Jiri Slaby | 4aed064 | 2005-09-13 01:25:01 -0700 | [diff] [blame] | 315 | struct class_device *class_dev = kzalloc(sizeof(*class_dev), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | GFP_KERNEL); |
| 317 | |
| 318 | *class_dev_p = NULL; |
| 319 | |
| 320 | if (!fw_priv || !class_dev) { |
| 321 | printk(KERN_ERR "%s: kmalloc failed\n", __FUNCTION__); |
| 322 | retval = -ENOMEM; |
| 323 | goto error_kfree; |
| 324 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
| 326 | init_completion(&fw_priv->completion); |
| 327 | fw_priv->attr_data = firmware_attr_data_tmpl; |
| 328 | strlcpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX); |
| 329 | |
| 330 | fw_priv->timeout.function = firmware_class_timeout; |
| 331 | fw_priv->timeout.data = (u_long) fw_priv; |
| 332 | init_timer(&fw_priv->timeout); |
| 333 | |
| 334 | fw_setup_class_device_id(class_dev, device); |
| 335 | class_dev->dev = device; |
| 336 | class_dev->class = &firmware_class; |
| 337 | class_set_devdata(class_dev, fw_priv); |
| 338 | retval = class_device_register(class_dev); |
| 339 | if (retval) { |
| 340 | printk(KERN_ERR "%s: class_device_register failed\n", |
| 341 | __FUNCTION__); |
| 342 | goto error_kfree; |
| 343 | } |
| 344 | *class_dev_p = class_dev; |
| 345 | return 0; |
| 346 | |
| 347 | error_kfree: |
| 348 | kfree(fw_priv); |
| 349 | kfree(class_dev); |
| 350 | return retval; |
| 351 | } |
| 352 | |
| 353 | static int |
| 354 | fw_setup_class_device(struct firmware *fw, struct class_device **class_dev_p, |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 355 | const char *fw_name, struct device *device, int hotplug) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | { |
| 357 | struct class_device *class_dev; |
| 358 | struct firmware_priv *fw_priv; |
| 359 | int retval; |
| 360 | |
| 361 | *class_dev_p = NULL; |
| 362 | retval = fw_register_class_device(&class_dev, fw_name, device); |
| 363 | if (retval) |
| 364 | goto out; |
| 365 | |
| 366 | /* Need to pin this module until class device is destroyed */ |
| 367 | __module_get(THIS_MODULE); |
| 368 | |
| 369 | fw_priv = class_get_devdata(class_dev); |
| 370 | |
| 371 | fw_priv->fw = fw; |
| 372 | retval = sysfs_create_bin_file(&class_dev->kobj, &fw_priv->attr_data); |
| 373 | if (retval) { |
| 374 | printk(KERN_ERR "%s: sysfs_create_bin_file failed\n", |
| 375 | __FUNCTION__); |
| 376 | goto error_unreg; |
| 377 | } |
| 378 | |
| 379 | retval = class_device_create_file(class_dev, |
| 380 | &class_device_attr_loading); |
| 381 | if (retval) { |
| 382 | printk(KERN_ERR "%s: class_device_create_file failed\n", |
| 383 | __FUNCTION__); |
| 384 | goto error_unreg; |
| 385 | } |
| 386 | |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 387 | if (hotplug) |
| 388 | set_bit(FW_STATUS_READY, &fw_priv->status); |
| 389 | else |
| 390 | set_bit(FW_STATUS_READY_NOHOTPLUG, &fw_priv->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | *class_dev_p = class_dev; |
| 392 | goto out; |
| 393 | |
| 394 | error_unreg: |
| 395 | class_device_unregister(class_dev); |
| 396 | out: |
| 397 | return retval; |
| 398 | } |
| 399 | |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 400 | static int |
| 401 | _request_firmware(const struct firmware **firmware_p, const char *name, |
| 402 | struct device *device, int hotplug) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | { |
| 404 | struct class_device *class_dev; |
| 405 | struct firmware_priv *fw_priv; |
| 406 | struct firmware *firmware; |
| 407 | int retval; |
| 408 | |
| 409 | if (!firmware_p) |
| 410 | return -EINVAL; |
| 411 | |
Jiri Slaby | 4aed064 | 2005-09-13 01:25:01 -0700 | [diff] [blame] | 412 | *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | if (!firmware) { |
| 414 | printk(KERN_ERR "%s: kmalloc(struct firmware) failed\n", |
| 415 | __FUNCTION__); |
| 416 | retval = -ENOMEM; |
| 417 | goto out; |
| 418 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 420 | retval = fw_setup_class_device(firmware, &class_dev, name, device, |
| 421 | hotplug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | if (retval) |
| 423 | goto error_kfree_fw; |
| 424 | |
| 425 | fw_priv = class_get_devdata(class_dev); |
| 426 | |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 427 | if (hotplug) { |
| 428 | if (loading_timeout > 0) { |
| 429 | fw_priv->timeout.expires = jiffies + loading_timeout * HZ; |
| 430 | add_timer(&fw_priv->timeout); |
| 431 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 433 | kobject_hotplug(&class_dev->kobj, KOBJ_ADD); |
| 434 | wait_for_completion(&fw_priv->completion); |
| 435 | set_bit(FW_STATUS_DONE, &fw_priv->status); |
| 436 | del_timer_sync(&fw_priv->timeout); |
| 437 | } else |
| 438 | wait_for_completion(&fw_priv->completion); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
| 440 | down(&fw_lock); |
| 441 | if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) { |
| 442 | retval = -ENOENT; |
| 443 | release_firmware(fw_priv->fw); |
| 444 | *firmware_p = NULL; |
| 445 | } |
| 446 | fw_priv->fw = NULL; |
| 447 | up(&fw_lock); |
| 448 | class_device_unregister(class_dev); |
| 449 | goto out; |
| 450 | |
| 451 | error_kfree_fw: |
| 452 | kfree(firmware); |
| 453 | *firmware_p = NULL; |
| 454 | out: |
| 455 | return retval; |
| 456 | } |
| 457 | |
| 458 | /** |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 459 | * request_firmware: - request firmware to hotplug and wait for it |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 460 | * @firmware_p: pointer to firmware image |
| 461 | * @name: name of firmware file |
| 462 | * @device: device for which firmware is being loaded |
| 463 | * |
| 464 | * @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] | 465 | * of @name for device @device. |
| 466 | * |
| 467 | * Should be called from user context where sleeping is allowed. |
| 468 | * |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 469 | * @name will be used as $FIRMWARE in the hotplug environment and |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 470 | * should be distinctive enough not to be confused with any other |
| 471 | * firmware image for this or any other device. |
| 472 | **/ |
| 473 | int |
| 474 | request_firmware(const struct firmware **firmware_p, const char *name, |
| 475 | struct device *device) |
| 476 | { |
| 477 | int hotplug = 1; |
| 478 | return _request_firmware(firmware_p, name, device, hotplug); |
| 479 | } |
| 480 | |
| 481 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | * release_firmware: - release the resource associated with a firmware image |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 483 | * @fw: firmware resource to release |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | **/ |
| 485 | void |
| 486 | release_firmware(const struct firmware *fw) |
| 487 | { |
| 488 | if (fw) { |
| 489 | vfree(fw->data); |
| 490 | kfree(fw); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * register_firmware: - provide a firmware image for later usage |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 496 | * @name: name of firmware image file |
| 497 | * @data: buffer pointer for the firmware image |
| 498 | * @size: size of the data buffer area |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | * Make sure that @data will be available by requesting firmware @name. |
| 501 | * |
| 502 | * Note: This will not be possible until some kind of persistence |
| 503 | * is available. |
| 504 | **/ |
| 505 | void |
| 506 | register_firmware(const char *name, const u8 *data, size_t size) |
| 507 | { |
| 508 | /* This is meaningless without firmware caching, so until we |
| 509 | * decide if firmware caching is reasonable just leave it as a |
| 510 | * noop */ |
| 511 | } |
| 512 | |
| 513 | /* Async support */ |
| 514 | struct firmware_work { |
| 515 | struct work_struct work; |
| 516 | struct module *module; |
| 517 | const char *name; |
| 518 | struct device *device; |
| 519 | void *context; |
| 520 | void (*cont)(const struct firmware *fw, void *context); |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 521 | int hotplug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | }; |
| 523 | |
| 524 | static int |
| 525 | request_firmware_work_func(void *arg) |
| 526 | { |
| 527 | struct firmware_work *fw_work = arg; |
| 528 | const struct firmware *fw; |
| 529 | if (!arg) { |
| 530 | WARN_ON(1); |
| 531 | return 0; |
| 532 | } |
| 533 | daemonize("%s/%s", "firmware", fw_work->name); |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 534 | _request_firmware(&fw, fw_work->name, fw_work->device, |
| 535 | fw_work->hotplug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | fw_work->cont(fw, fw_work->context); |
| 537 | release_firmware(fw); |
| 538 | module_put(fw_work->module); |
| 539 | kfree(fw_work); |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | /** |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 544 | * request_firmware_nowait: asynchronous version of request_firmware |
| 545 | * @module: module requesting the firmware |
| 546 | * @hotplug: invokes hotplug event to copy the firmware image if this flag |
| 547 | * is non-zero else the firmware copy must be done manually. |
| 548 | * @name: name of firmware file |
| 549 | * @device: device for which firmware is being loaded |
| 550 | * @context: will be passed over to @cont, and |
| 551 | * @fw may be %NULL if firmware request fails. |
| 552 | * @cont: function will be called asynchronously when the firmware |
| 553 | * request is over. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | * Asynchronous variant of request_firmware() for contexts where |
| 556 | * it is not possible to sleep. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | **/ |
| 558 | int |
| 559 | request_firmware_nowait( |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 560 | struct module *module, int hotplug, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | const char *name, struct device *device, void *context, |
| 562 | void (*cont)(const struct firmware *fw, void *context)) |
| 563 | { |
| 564 | struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work), |
| 565 | GFP_ATOMIC); |
| 566 | int ret; |
| 567 | |
| 568 | if (!fw_work) |
| 569 | return -ENOMEM; |
| 570 | if (!try_module_get(module)) { |
| 571 | kfree(fw_work); |
| 572 | return -EFAULT; |
| 573 | } |
| 574 | |
| 575 | *fw_work = (struct firmware_work) { |
| 576 | .module = module, |
| 577 | .name = name, |
| 578 | .device = device, |
| 579 | .context = context, |
| 580 | .cont = cont, |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 581 | .hotplug = hotplug, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | }; |
| 583 | |
| 584 | ret = kernel_thread(request_firmware_work_func, fw_work, |
| 585 | CLONE_FS | CLONE_FILES); |
| 586 | |
| 587 | if (ret < 0) { |
| 588 | fw_work->cont(NULL, fw_work->context); |
| 589 | return ret; |
| 590 | } |
| 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | static int __init |
| 595 | firmware_class_init(void) |
| 596 | { |
| 597 | int error; |
| 598 | error = class_register(&firmware_class); |
| 599 | if (error) { |
| 600 | printk(KERN_ERR "%s: class_register failed\n", __FUNCTION__); |
| 601 | return error; |
| 602 | } |
| 603 | error = class_create_file(&firmware_class, &class_attr_timeout); |
| 604 | if (error) { |
| 605 | printk(KERN_ERR "%s: class_create_file failed\n", |
| 606 | __FUNCTION__); |
| 607 | class_unregister(&firmware_class); |
| 608 | } |
| 609 | return error; |
| 610 | |
| 611 | } |
| 612 | static void __exit |
| 613 | firmware_class_exit(void) |
| 614 | { |
| 615 | class_unregister(&firmware_class); |
| 616 | } |
| 617 | |
| 618 | module_init(firmware_class_init); |
| 619 | module_exit(firmware_class_exit); |
| 620 | |
| 621 | EXPORT_SYMBOL(release_firmware); |
| 622 | EXPORT_SYMBOL(request_firmware); |
| 623 | EXPORT_SYMBOL(request_firmware_nowait); |
| 624 | EXPORT_SYMBOL(register_firmware); |