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