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