| 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/workqueue.h> |
| 20 | #include <linux/highmem.h> |
| 21 | #include <linux/firmware.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/sched.h> |
| 24 | #include <linux/file.h> |
| 25 | #include <linux/list.h> |
| 26 | #include <linux/async.h> |
| 27 | #include <linux/pm.h> |
| 28 | #include <linux/suspend.h> |
| 29 | #include <linux/syscore_ops.h> |
| 30 | #include <linux/reboot.h> |
| 31 | |
| 32 | #include <generated/utsrelease.h> |
| 33 | |
| 34 | #include "base.h" |
| 35 | |
| 36 | MODULE_AUTHOR("Manuel Estrada Sainz"); |
| 37 | MODULE_DESCRIPTION("Multi purpose firmware loading support"); |
| 38 | MODULE_LICENSE("GPL"); |
| 39 | |
| 40 | /* Builtin firmware support */ |
| 41 | |
| 42 | #ifdef CONFIG_FW_LOADER |
| 43 | |
| 44 | extern struct builtin_fw __start_builtin_fw[]; |
| 45 | extern struct builtin_fw __end_builtin_fw[]; |
| 46 | |
| 47 | static bool fw_get_builtin_firmware(struct firmware *fw, const char *name) |
| 48 | { |
| 49 | struct builtin_fw *b_fw; |
| 50 | |
| 51 | for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) { |
| 52 | if (strcmp(name, b_fw->name) == 0) { |
| 53 | fw->size = b_fw->size; |
| 54 | fw->data = b_fw->data; |
| 55 | return true; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | static bool fw_is_builtin_firmware(const struct firmware *fw) |
| 63 | { |
| 64 | struct builtin_fw *b_fw; |
| 65 | |
| 66 | for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) |
| 67 | if (fw->data == b_fw->data) |
| 68 | return true; |
| 69 | |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | #else /* Module case - no builtin firmware support */ |
| 74 | |
| 75 | static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name) |
| 76 | { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | static inline bool fw_is_builtin_firmware(const struct firmware *fw) |
| 81 | { |
| 82 | return false; |
| 83 | } |
| 84 | #endif |
| 85 | |
| 86 | enum { |
| 87 | FW_STATUS_LOADING, |
| 88 | FW_STATUS_DONE, |
| 89 | FW_STATUS_ABORT, |
| 90 | }; |
| 91 | |
| 92 | static int loading_timeout = 60; /* In seconds */ |
| 93 | |
| 94 | static inline long firmware_loading_timeout(void) |
| 95 | { |
| 96 | return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT; |
| 97 | } |
| 98 | |
| 99 | struct firmware_cache { |
| 100 | /* firmware_buf instance will be added into the below list */ |
| 101 | spinlock_t lock; |
| 102 | struct list_head head; |
| 103 | int state; |
| 104 | |
| 105 | #ifdef CONFIG_PM_SLEEP |
| 106 | /* |
| 107 | * Names of firmware images which have been cached successfully |
| 108 | * will be added into the below list so that device uncache |
| 109 | * helper can trace which firmware images have been cached |
| 110 | * before. |
| 111 | */ |
| 112 | spinlock_t name_lock; |
| 113 | struct list_head fw_names; |
| 114 | |
| 115 | struct delayed_work work; |
| 116 | |
| 117 | struct notifier_block pm_notify; |
| 118 | #endif |
| 119 | }; |
| 120 | |
| 121 | struct firmware_buf { |
| 122 | struct kref ref; |
| 123 | struct list_head list; |
| 124 | struct completion completion; |
| 125 | struct firmware_cache *fwc; |
| 126 | unsigned long status; |
| 127 | void *data; |
| 128 | size_t size; |
| 129 | #ifdef CONFIG_FW_LOADER_USER_HELPER |
| 130 | bool is_paged_buf; |
| 131 | struct page **pages; |
| 132 | int nr_pages; |
| 133 | int page_array_size; |
| 134 | struct list_head pending_list; |
| 135 | #endif |
| 136 | char fw_id[]; |
| 137 | }; |
| 138 | |
| 139 | struct fw_cache_entry { |
| 140 | struct list_head list; |
| 141 | char name[]; |
| 142 | }; |
| 143 | |
| 144 | struct fw_name_devm { |
| 145 | unsigned long magic; |
| 146 | char name[]; |
| 147 | }; |
| 148 | |
| 149 | #define to_fwbuf(d) container_of(d, struct firmware_buf, ref) |
| 150 | |
| 151 | #define FW_LOADER_NO_CACHE 0 |
| 152 | #define FW_LOADER_START_CACHE 1 |
| 153 | |
| 154 | static int fw_cache_piggyback_on_request(const char *name); |
| 155 | |
| 156 | /* fw_lock could be moved to 'struct firmware_priv' but since it is just |
| 157 | * guarding for corner cases a global lock should be OK */ |
| 158 | static DEFINE_MUTEX(fw_lock); |
| 159 | |
| 160 | static struct firmware_cache fw_cache; |
| 161 | |
| 162 | static struct firmware_buf *__allocate_fw_buf(const char *fw_name, |
| 163 | struct firmware_cache *fwc) |
| 164 | { |
| 165 | struct firmware_buf *buf; |
| 166 | |
| 167 | buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC); |
| 168 | |
| 169 | if (!buf) |
| 170 | return buf; |
| 171 | |
| 172 | kref_init(&buf->ref); |
| 173 | strcpy(buf->fw_id, fw_name); |
| 174 | buf->fwc = fwc; |
| 175 | init_completion(&buf->completion); |
| 176 | #ifdef CONFIG_FW_LOADER_USER_HELPER |
| 177 | INIT_LIST_HEAD(&buf->pending_list); |
| 178 | #endif |
| 179 | |
| 180 | pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf); |
| 181 | |
| 182 | return buf; |
| 183 | } |
| 184 | |
| 185 | static struct firmware_buf *__fw_lookup_buf(const char *fw_name) |
| 186 | { |
| 187 | struct firmware_buf *tmp; |
| 188 | struct firmware_cache *fwc = &fw_cache; |
| 189 | |
| 190 | list_for_each_entry(tmp, &fwc->head, list) |
| 191 | if (!strcmp(tmp->fw_id, fw_name)) |
| 192 | return tmp; |
| 193 | return NULL; |
| 194 | } |
| 195 | |
| 196 | static int fw_lookup_and_allocate_buf(const char *fw_name, |
| 197 | struct firmware_cache *fwc, |
| 198 | struct firmware_buf **buf) |
| 199 | { |
| 200 | struct firmware_buf *tmp; |
| 201 | |
| 202 | spin_lock(&fwc->lock); |
| 203 | tmp = __fw_lookup_buf(fw_name); |
| 204 | if (tmp) { |
| 205 | kref_get(&tmp->ref); |
| 206 | spin_unlock(&fwc->lock); |
| 207 | *buf = tmp; |
| 208 | return 1; |
| 209 | } |
| 210 | tmp = __allocate_fw_buf(fw_name, fwc); |
| 211 | if (tmp) |
| 212 | list_add(&tmp->list, &fwc->head); |
| 213 | spin_unlock(&fwc->lock); |
| 214 | |
| 215 | *buf = tmp; |
| 216 | |
| 217 | return tmp ? 0 : -ENOMEM; |
| 218 | } |
| 219 | |
| 220 | static struct firmware_buf *fw_lookup_buf(const char *fw_name) |
| 221 | { |
| 222 | struct firmware_buf *tmp; |
| 223 | struct firmware_cache *fwc = &fw_cache; |
| 224 | |
| 225 | spin_lock(&fwc->lock); |
| 226 | tmp = __fw_lookup_buf(fw_name); |
| 227 | spin_unlock(&fwc->lock); |
| 228 | |
| 229 | return tmp; |
| 230 | } |
| 231 | |
| 232 | static void __fw_free_buf(struct kref *ref) |
| 233 | { |
| 234 | struct firmware_buf *buf = to_fwbuf(ref); |
| 235 | struct firmware_cache *fwc = buf->fwc; |
| 236 | |
| 237 | pr_debug("%s: fw-%s buf=%p data=%p size=%u\n", |
| 238 | __func__, buf->fw_id, buf, buf->data, |
| 239 | (unsigned int)buf->size); |
| 240 | |
| 241 | list_del(&buf->list); |
| 242 | spin_unlock(&fwc->lock); |
| 243 | |
| 244 | #ifdef CONFIG_FW_LOADER_USER_HELPER |
| 245 | if (buf->is_paged_buf) { |
| 246 | int i; |
| 247 | vunmap(buf->data); |
| 248 | for (i = 0; i < buf->nr_pages; i++) |
| 249 | __free_page(buf->pages[i]); |
| 250 | kfree(buf->pages); |
| 251 | } else |
| 252 | #endif |
| 253 | vfree(buf->data); |
| 254 | kfree(buf); |
| 255 | } |
| 256 | |
| 257 | static void fw_free_buf(struct firmware_buf *buf) |
| 258 | { |
| 259 | struct firmware_cache *fwc = buf->fwc; |
| 260 | spin_lock(&fwc->lock); |
| 261 | if (!kref_put(&buf->ref, __fw_free_buf)) |
| 262 | spin_unlock(&fwc->lock); |
| 263 | } |
| 264 | |
| 265 | /* direct firmware loading support */ |
| 266 | static char fw_path_para[256]; |
| 267 | static const char * const fw_path[] = { |
| 268 | fw_path_para, |
| 269 | "/lib/firmware/updates/" UTS_RELEASE, |
| 270 | "/lib/firmware/updates", |
| 271 | "/lib/firmware/" UTS_RELEASE, |
| 272 | "/lib/firmware" |
| 273 | }; |
| 274 | |
| 275 | /* |
| 276 | * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH' |
| 277 | * from kernel command line because firmware_class is generally built in |
| 278 | * kernel instead of module. |
| 279 | */ |
| 280 | module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644); |
| 281 | MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path"); |
| 282 | |
| 283 | /* Don't inline this: 'struct kstat' is biggish */ |
| 284 | static noinline_for_stack long fw_file_size(struct file *file) |
| 285 | { |
| 286 | struct kstat st; |
| 287 | if (vfs_getattr(&file->f_path, &st)) |
| 288 | return -1; |
| 289 | if (!S_ISREG(st.mode)) |
| 290 | return -1; |
| 291 | if (st.size != (long)st.size) |
| 292 | return -1; |
| 293 | return st.size; |
| 294 | } |
| 295 | |
| 296 | static bool fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf) |
| 297 | { |
| 298 | long size; |
| 299 | char *buf; |
| 300 | |
| 301 | size = fw_file_size(file); |
| 302 | if (size <= 0) |
| 303 | return false; |
| 304 | buf = vmalloc(size); |
| 305 | if (!buf) |
| 306 | return false; |
| 307 | if (kernel_read(file, 0, buf, size) != size) { |
| 308 | vfree(buf); |
| 309 | return false; |
| 310 | } |
| 311 | fw_buf->data = buf; |
| 312 | fw_buf->size = size; |
| 313 | return true; |
| 314 | } |
| 315 | |
| 316 | static bool fw_get_filesystem_firmware(struct device *device, |
| 317 | struct firmware_buf *buf) |
| 318 | { |
| 319 | int i; |
| 320 | bool success = false; |
| 321 | char *path = __getname(); |
| 322 | |
| 323 | for (i = 0; i < ARRAY_SIZE(fw_path); i++) { |
| 324 | struct file *file; |
| 325 | |
| 326 | /* skip the unset customized path */ |
| 327 | if (!fw_path[i][0]) |
| 328 | continue; |
| 329 | |
| 330 | snprintf(path, PATH_MAX, "%s/%s", fw_path[i], buf->fw_id); |
| 331 | |
| 332 | file = filp_open(path, O_RDONLY, 0); |
| 333 | if (IS_ERR(file)) |
| 334 | continue; |
| 335 | success = fw_read_file_contents(file, buf); |
| 336 | fput(file); |
| 337 | if (success) |
| 338 | break; |
| 339 | } |
| 340 | __putname(path); |
| 341 | |
| 342 | if (success) { |
| 343 | dev_dbg(device, "firmware: direct-loading firmware %s\n", |
| 344 | buf->fw_id); |
| 345 | mutex_lock(&fw_lock); |
| 346 | set_bit(FW_STATUS_DONE, &buf->status); |
| 347 | complete_all(&buf->completion); |
| 348 | mutex_unlock(&fw_lock); |
| 349 | } |
| 350 | |
| 351 | return success; |
| 352 | } |
| 353 | |
| 354 | /* firmware holds the ownership of pages */ |
| 355 | static void firmware_free_data(const struct firmware *fw) |
| 356 | { |
| 357 | /* Loaded directly? */ |
| 358 | if (!fw->priv) { |
| 359 | vfree(fw->data); |
| 360 | return; |
| 361 | } |
| 362 | fw_free_buf(fw->priv); |
| 363 | } |
| 364 | |
| 365 | /* store the pages buffer info firmware from buf */ |
| 366 | static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw) |
| 367 | { |
| 368 | fw->priv = buf; |
| 369 | #ifdef CONFIG_FW_LOADER_USER_HELPER |
| 370 | fw->pages = buf->pages; |
| 371 | #endif |
| 372 | fw->size = buf->size; |
| 373 | fw->data = buf->data; |
| 374 | |
| 375 | pr_debug("%s: fw-%s buf=%p data=%p size=%u\n", |
| 376 | __func__, buf->fw_id, buf, buf->data, |
| 377 | (unsigned int)buf->size); |
| 378 | } |
| 379 | |
| 380 | #ifdef CONFIG_PM_SLEEP |
| 381 | static void fw_name_devm_release(struct device *dev, void *res) |
| 382 | { |
| 383 | struct fw_name_devm *fwn = res; |
| 384 | |
| 385 | if (fwn->magic == (unsigned long)&fw_cache) |
| 386 | pr_debug("%s: fw_name-%s devm-%p released\n", |
| 387 | __func__, fwn->name, res); |
| 388 | } |
| 389 | |
| 390 | static int fw_devm_match(struct device *dev, void *res, |
| 391 | void *match_data) |
| 392 | { |
| 393 | struct fw_name_devm *fwn = res; |
| 394 | |
| 395 | return (fwn->magic == (unsigned long)&fw_cache) && |
| 396 | !strcmp(fwn->name, match_data); |
| 397 | } |
| 398 | |
| 399 | static struct fw_name_devm *fw_find_devm_name(struct device *dev, |
| 400 | const char *name) |
| 401 | { |
| 402 | struct fw_name_devm *fwn; |
| 403 | |
| 404 | fwn = devres_find(dev, fw_name_devm_release, |
| 405 | fw_devm_match, (void *)name); |
| 406 | return fwn; |
| 407 | } |
| 408 | |
| 409 | /* add firmware name into devres list */ |
| 410 | static int fw_add_devm_name(struct device *dev, const char *name) |
| 411 | { |
| 412 | struct fw_name_devm *fwn; |
| 413 | |
| 414 | fwn = fw_find_devm_name(dev, name); |
| 415 | if (fwn) |
| 416 | return 1; |
| 417 | |
| 418 | fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) + |
| 419 | strlen(name) + 1, GFP_KERNEL); |
| 420 | if (!fwn) |
| 421 | return -ENOMEM; |
| 422 | |
| 423 | fwn->magic = (unsigned long)&fw_cache; |
| 424 | strcpy(fwn->name, name); |
| 425 | devres_add(dev, fwn); |
| 426 | |
| 427 | return 0; |
| 428 | } |
| 429 | #else |
| 430 | static int fw_add_devm_name(struct device *dev, const char *name) |
| 431 | { |
| 432 | return 0; |
| 433 | } |
| 434 | #endif |
| 435 | |
| 436 | |
| 437 | /* |
| 438 | * user-mode helper code |
| 439 | */ |
| 440 | #ifdef CONFIG_FW_LOADER_USER_HELPER |
| 441 | struct firmware_priv { |
| 442 | struct delayed_work timeout_work; |
| 443 | bool nowait; |
| 444 | struct device dev; |
| 445 | struct firmware_buf *buf; |
| 446 | struct firmware *fw; |
| 447 | }; |
| 448 | |
| 449 | static struct firmware_priv *to_firmware_priv(struct device *dev) |
| 450 | { |
| 451 | return container_of(dev, struct firmware_priv, dev); |
| 452 | } |
| 453 | |
| 454 | static void __fw_load_abort(struct firmware_buf *buf) |
| 455 | { |
| 456 | /* |
| 457 | * There is a small window in which user can write to 'loading' |
| 458 | * between loading done and disappearance of 'loading' |
| 459 | */ |
| 460 | if (test_bit(FW_STATUS_DONE, &buf->status)) |
| 461 | return; |
| 462 | |
| 463 | list_del_init(&buf->pending_list); |
| 464 | set_bit(FW_STATUS_ABORT, &buf->status); |
| 465 | complete_all(&buf->completion); |
| 466 | } |
| 467 | |
| 468 | static void fw_load_abort(struct firmware_priv *fw_priv) |
| 469 | { |
| 470 | struct firmware_buf *buf = fw_priv->buf; |
| 471 | |
| 472 | __fw_load_abort(buf); |
| 473 | |
| 474 | /* avoid user action after loading abort */ |
| 475 | fw_priv->buf = NULL; |
| 476 | } |
| 477 | |
| 478 | #define is_fw_load_aborted(buf) \ |
| 479 | test_bit(FW_STATUS_ABORT, &(buf)->status) |
| 480 | |
| 481 | static LIST_HEAD(pending_fw_head); |
| 482 | |
| 483 | /* reboot notifier for avoid deadlock with usermode_lock */ |
| 484 | static int fw_shutdown_notify(struct notifier_block *unused1, |
| 485 | unsigned long unused2, void *unused3) |
| 486 | { |
| 487 | mutex_lock(&fw_lock); |
| 488 | while (!list_empty(&pending_fw_head)) |
| 489 | __fw_load_abort(list_first_entry(&pending_fw_head, |
| 490 | struct firmware_buf, |
| 491 | pending_list)); |
| 492 | mutex_unlock(&fw_lock); |
| 493 | return NOTIFY_DONE; |
| 494 | } |
| 495 | |
| 496 | static struct notifier_block fw_shutdown_nb = { |
| 497 | .notifier_call = fw_shutdown_notify, |
| 498 | }; |
| 499 | |
| 500 | static ssize_t firmware_timeout_show(struct class *class, |
| 501 | struct class_attribute *attr, |
| 502 | char *buf) |
| 503 | { |
| 504 | return sprintf(buf, "%d\n", loading_timeout); |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * firmware_timeout_store - set number of seconds to wait for firmware |
| 509 | * @class: device class pointer |
| 510 | * @attr: device attribute pointer |
| 511 | * @buf: buffer to scan for timeout value |
| 512 | * @count: number of bytes in @buf |
| 513 | * |
| 514 | * Sets the number of seconds to wait for the firmware. Once |
| 515 | * this expires an error will be returned to the driver and no |
| 516 | * firmware will be provided. |
| 517 | * |
| 518 | * Note: zero means 'wait forever'. |
| 519 | **/ |
| 520 | static ssize_t firmware_timeout_store(struct class *class, |
| 521 | struct class_attribute *attr, |
| 522 | const char *buf, size_t count) |
| 523 | { |
| 524 | loading_timeout = simple_strtol(buf, NULL, 10); |
| 525 | if (loading_timeout < 0) |
| 526 | loading_timeout = 0; |
| 527 | |
| 528 | return count; |
| 529 | } |
| 530 | |
| 531 | static struct class_attribute firmware_class_attrs[] = { |
| 532 | __ATTR(timeout, S_IWUSR | S_IRUGO, |
| 533 | firmware_timeout_show, firmware_timeout_store), |
| 534 | __ATTR_NULL |
| 535 | }; |
| 536 | |
| 537 | static void fw_dev_release(struct device *dev) |
| 538 | { |
| 539 | struct firmware_priv *fw_priv = to_firmware_priv(dev); |
| 540 | |
| 541 | kfree(fw_priv); |
| 542 | |
| 543 | module_put(THIS_MODULE); |
| 544 | } |
| 545 | |
| 546 | static int do_firmware_uevent(struct firmware_priv *fw_priv, struct kobj_uevent_env *env) |
| 547 | { |
| 548 | if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id)) |
| 549 | return -ENOMEM; |
| 550 | if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout)) |
| 551 | return -ENOMEM; |
| 552 | if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait)) |
| 553 | return -ENOMEM; |
| 554 | |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 559 | { |
| 560 | struct firmware_priv *fw_priv = to_firmware_priv(dev); |
| 561 | int err = 0; |
| 562 | |
| 563 | mutex_lock(&fw_lock); |
| 564 | if (fw_priv->buf) |
| 565 | err = do_firmware_uevent(fw_priv, env); |
| 566 | mutex_unlock(&fw_lock); |
| 567 | return err; |
| 568 | } |
| 569 | |
| 570 | static struct class firmware_class = { |
| 571 | .name = "firmware", |
| 572 | .class_attrs = firmware_class_attrs, |
| 573 | .dev_uevent = firmware_uevent, |
| 574 | .dev_release = fw_dev_release, |
| 575 | }; |
| 576 | |
| 577 | static ssize_t firmware_loading_show(struct device *dev, |
| 578 | struct device_attribute *attr, char *buf) |
| 579 | { |
| 580 | struct firmware_priv *fw_priv = to_firmware_priv(dev); |
| 581 | int loading = 0; |
| 582 | |
| 583 | mutex_lock(&fw_lock); |
| 584 | if (fw_priv->buf) |
| 585 | loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status); |
| 586 | mutex_unlock(&fw_lock); |
| 587 | |
| 588 | return sprintf(buf, "%d\n", loading); |
| 589 | } |
| 590 | |
| 591 | /* Some architectures don't have PAGE_KERNEL_RO */ |
| 592 | #ifndef PAGE_KERNEL_RO |
| 593 | #define PAGE_KERNEL_RO PAGE_KERNEL |
| 594 | #endif |
| 595 | |
| 596 | /* one pages buffer should be mapped/unmapped only once */ |
| 597 | static int fw_map_pages_buf(struct firmware_buf *buf) |
| 598 | { |
| 599 | if (!buf->is_paged_buf) |
| 600 | return 0; |
| 601 | |
| 602 | if (buf->data) |
| 603 | vunmap(buf->data); |
| 604 | buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO); |
| 605 | if (!buf->data) |
| 606 | return -ENOMEM; |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * firmware_loading_store - set value in the 'loading' control file |
| 612 | * @dev: device pointer |
| 613 | * @attr: device attribute pointer |
| 614 | * @buf: buffer to scan for loading control value |
| 615 | * @count: number of bytes in @buf |
| 616 | * |
| 617 | * The relevant values are: |
| 618 | * |
| 619 | * 1: Start a load, discarding any previous partial load. |
| 620 | * 0: Conclude the load and hand the data to the driver code. |
| 621 | * -1: Conclude the load with an error and discard any written data. |
| 622 | **/ |
| 623 | static ssize_t firmware_loading_store(struct device *dev, |
| 624 | struct device_attribute *attr, |
| 625 | const char *buf, size_t count) |
| 626 | { |
| 627 | struct firmware_priv *fw_priv = to_firmware_priv(dev); |
| 628 | struct firmware_buf *fw_buf; |
| 629 | int loading = simple_strtol(buf, NULL, 10); |
| 630 | int i; |
| 631 | |
| 632 | mutex_lock(&fw_lock); |
| 633 | fw_buf = fw_priv->buf; |
| 634 | if (!fw_buf) |
| 635 | goto out; |
| 636 | |
| 637 | switch (loading) { |
| 638 | case 1: |
| 639 | /* discarding any previous partial load */ |
| 640 | if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) { |
| 641 | for (i = 0; i < fw_buf->nr_pages; i++) |
| 642 | __free_page(fw_buf->pages[i]); |
| 643 | kfree(fw_buf->pages); |
| 644 | fw_buf->pages = NULL; |
| 645 | fw_buf->page_array_size = 0; |
| 646 | fw_buf->nr_pages = 0; |
| 647 | set_bit(FW_STATUS_LOADING, &fw_buf->status); |
| 648 | } |
| 649 | break; |
| 650 | case 0: |
| 651 | if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) { |
| 652 | set_bit(FW_STATUS_DONE, &fw_buf->status); |
| 653 | clear_bit(FW_STATUS_LOADING, &fw_buf->status); |
| 654 | |
| 655 | /* |
| 656 | * Several loading requests may be pending on |
| 657 | * one same firmware buf, so let all requests |
| 658 | * see the mapped 'buf->data' once the loading |
| 659 | * is completed. |
| 660 | * */ |
| 661 | fw_map_pages_buf(fw_buf); |
| 662 | list_del_init(&fw_buf->pending_list); |
| 663 | complete_all(&fw_buf->completion); |
| 664 | break; |
| 665 | } |
| 666 | /* fallthrough */ |
| 667 | default: |
| 668 | dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading); |
| 669 | /* fallthrough */ |
| 670 | case -1: |
| 671 | fw_load_abort(fw_priv); |
| 672 | break; |
| 673 | } |
| 674 | out: |
| 675 | mutex_unlock(&fw_lock); |
| 676 | return count; |
| 677 | } |
| 678 | |
| 679 | static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store); |
| 680 | |
| 681 | static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj, |
| 682 | struct bin_attribute *bin_attr, |
| 683 | char *buffer, loff_t offset, size_t count) |
| 684 | { |
| 685 | struct device *dev = kobj_to_dev(kobj); |
| 686 | struct firmware_priv *fw_priv = to_firmware_priv(dev); |
| 687 | struct firmware_buf *buf; |
| 688 | ssize_t ret_count; |
| 689 | |
| 690 | mutex_lock(&fw_lock); |
| 691 | buf = fw_priv->buf; |
| 692 | if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) { |
| 693 | ret_count = -ENODEV; |
| 694 | goto out; |
| 695 | } |
| 696 | if (offset > buf->size) { |
| 697 | ret_count = 0; |
| 698 | goto out; |
| 699 | } |
| 700 | if (count > buf->size - offset) |
| 701 | count = buf->size - offset; |
| 702 | |
| 703 | ret_count = count; |
| 704 | |
| 705 | while (count) { |
| 706 | void *page_data; |
| 707 | int page_nr = offset >> PAGE_SHIFT; |
| 708 | int page_ofs = offset & (PAGE_SIZE-1); |
| 709 | int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count); |
| 710 | |
| 711 | page_data = kmap(buf->pages[page_nr]); |
| 712 | |
| 713 | memcpy(buffer, page_data + page_ofs, page_cnt); |
| 714 | |
| 715 | kunmap(buf->pages[page_nr]); |
| 716 | buffer += page_cnt; |
| 717 | offset += page_cnt; |
| 718 | count -= page_cnt; |
| 719 | } |
| 720 | out: |
| 721 | mutex_unlock(&fw_lock); |
| 722 | return ret_count; |
| 723 | } |
| 724 | |
| 725 | static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size) |
| 726 | { |
| 727 | struct firmware_buf *buf = fw_priv->buf; |
| 728 | int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT; |
| 729 | |
| 730 | /* If the array of pages is too small, grow it... */ |
| 731 | if (buf->page_array_size < pages_needed) { |
| 732 | int new_array_size = max(pages_needed, |
| 733 | buf->page_array_size * 2); |
| 734 | struct page **new_pages; |
| 735 | |
| 736 | new_pages = kmalloc(new_array_size * sizeof(void *), |
| 737 | GFP_KERNEL); |
| 738 | if (!new_pages) { |
| 739 | fw_load_abort(fw_priv); |
| 740 | return -ENOMEM; |
| 741 | } |
| 742 | memcpy(new_pages, buf->pages, |
| 743 | buf->page_array_size * sizeof(void *)); |
| 744 | memset(&new_pages[buf->page_array_size], 0, sizeof(void *) * |
| 745 | (new_array_size - buf->page_array_size)); |
| 746 | kfree(buf->pages); |
| 747 | buf->pages = new_pages; |
| 748 | buf->page_array_size = new_array_size; |
| 749 | } |
| 750 | |
| 751 | while (buf->nr_pages < pages_needed) { |
| 752 | buf->pages[buf->nr_pages] = |
| 753 | alloc_page(GFP_KERNEL | __GFP_HIGHMEM); |
| 754 | |
| 755 | if (!buf->pages[buf->nr_pages]) { |
| 756 | fw_load_abort(fw_priv); |
| 757 | return -ENOMEM; |
| 758 | } |
| 759 | buf->nr_pages++; |
| 760 | } |
| 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | /** |
| 765 | * firmware_data_write - write method for firmware |
| 766 | * @filp: open sysfs file |
| 767 | * @kobj: kobject for the device |
| 768 | * @bin_attr: bin_attr structure |
| 769 | * @buffer: buffer being written |
| 770 | * @offset: buffer offset for write in total data store area |
| 771 | * @count: buffer size |
| 772 | * |
| 773 | * Data written to the 'data' attribute will be later handed to |
| 774 | * the driver as a firmware image. |
| 775 | **/ |
| 776 | static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj, |
| 777 | struct bin_attribute *bin_attr, |
| 778 | char *buffer, loff_t offset, size_t count) |
| 779 | { |
| 780 | struct device *dev = kobj_to_dev(kobj); |
| 781 | struct firmware_priv *fw_priv = to_firmware_priv(dev); |
| 782 | struct firmware_buf *buf; |
| 783 | ssize_t retval; |
| 784 | |
| 785 | if (!capable(CAP_SYS_RAWIO)) |
| 786 | return -EPERM; |
| 787 | |
| 788 | mutex_lock(&fw_lock); |
| 789 | buf = fw_priv->buf; |
| 790 | if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) { |
| 791 | retval = -ENODEV; |
| 792 | goto out; |
| 793 | } |
| 794 | |
| 795 | retval = fw_realloc_buffer(fw_priv, offset + count); |
| 796 | if (retval) |
| 797 | goto out; |
| 798 | |
| 799 | retval = count; |
| 800 | |
| 801 | while (count) { |
| 802 | void *page_data; |
| 803 | int page_nr = offset >> PAGE_SHIFT; |
| 804 | int page_ofs = offset & (PAGE_SIZE - 1); |
| 805 | int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count); |
| 806 | |
| 807 | page_data = kmap(buf->pages[page_nr]); |
| 808 | |
| 809 | memcpy(page_data + page_ofs, buffer, page_cnt); |
| 810 | |
| 811 | kunmap(buf->pages[page_nr]); |
| 812 | buffer += page_cnt; |
| 813 | offset += page_cnt; |
| 814 | count -= page_cnt; |
| 815 | } |
| 816 | |
| 817 | buf->size = max_t(size_t, offset, buf->size); |
| 818 | out: |
| 819 | mutex_unlock(&fw_lock); |
| 820 | return retval; |
| 821 | } |
| 822 | |
| 823 | static struct bin_attribute firmware_attr_data = { |
| 824 | .attr = { .name = "data", .mode = 0644 }, |
| 825 | .size = 0, |
| 826 | .read = firmware_data_read, |
| 827 | .write = firmware_data_write, |
| 828 | }; |
| 829 | |
| 830 | static void firmware_class_timeout_work(struct work_struct *work) |
| 831 | { |
| 832 | struct firmware_priv *fw_priv = container_of(work, |
| 833 | struct firmware_priv, timeout_work.work); |
| 834 | |
| 835 | mutex_lock(&fw_lock); |
| 836 | fw_load_abort(fw_priv); |
| 837 | mutex_unlock(&fw_lock); |
| 838 | } |
| 839 | |
| 840 | static struct firmware_priv * |
| 841 | fw_create_instance(struct firmware *firmware, const char *fw_name, |
| 842 | struct device *device, bool uevent, bool nowait) |
| 843 | { |
| 844 | struct firmware_priv *fw_priv; |
| 845 | struct device *f_dev; |
| 846 | |
| 847 | fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL); |
| 848 | if (!fw_priv) { |
| 849 | dev_err(device, "%s: kmalloc failed\n", __func__); |
| 850 | fw_priv = ERR_PTR(-ENOMEM); |
| 851 | goto exit; |
| 852 | } |
| 853 | |
| 854 | fw_priv->nowait = nowait; |
| 855 | fw_priv->fw = firmware; |
| 856 | INIT_DELAYED_WORK(&fw_priv->timeout_work, |
| 857 | firmware_class_timeout_work); |
| 858 | |
| 859 | f_dev = &fw_priv->dev; |
| 860 | |
| 861 | device_initialize(f_dev); |
| 862 | dev_set_name(f_dev, "%s", fw_name); |
| 863 | f_dev->parent = device; |
| 864 | f_dev->class = &firmware_class; |
| 865 | exit: |
| 866 | return fw_priv; |
| 867 | } |
| 868 | |
| 869 | /* load a firmware via user helper */ |
| 870 | static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent, |
| 871 | long timeout) |
| 872 | { |
| 873 | int retval = 0; |
| 874 | struct device *f_dev = &fw_priv->dev; |
| 875 | struct firmware_buf *buf = fw_priv->buf; |
| 876 | |
| 877 | /* fall back on userspace loading */ |
| 878 | buf->is_paged_buf = true; |
| 879 | |
| 880 | dev_set_uevent_suppress(f_dev, true); |
| 881 | |
| 882 | /* Need to pin this module until class device is destroyed */ |
| 883 | __module_get(THIS_MODULE); |
| 884 | |
| 885 | retval = device_add(f_dev); |
| 886 | if (retval) { |
| 887 | dev_err(f_dev, "%s: device_register failed\n", __func__); |
| 888 | goto err_put_dev; |
| 889 | } |
| 890 | |
| 891 | retval = device_create_bin_file(f_dev, &firmware_attr_data); |
| 892 | if (retval) { |
| 893 | dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__); |
| 894 | goto err_del_dev; |
| 895 | } |
| 896 | |
| 897 | mutex_lock(&fw_lock); |
| 898 | list_add(&buf->pending_list, &pending_fw_head); |
| 899 | mutex_unlock(&fw_lock); |
| 900 | |
| 901 | retval = device_create_file(f_dev, &dev_attr_loading); |
| 902 | if (retval) { |
| 903 | mutex_lock(&fw_lock); |
| 904 | list_del_init(&buf->pending_list); |
| 905 | mutex_unlock(&fw_lock); |
| 906 | dev_err(f_dev, "%s: device_create_file failed\n", __func__); |
| 907 | goto err_del_bin_attr; |
| 908 | } |
| 909 | |
| 910 | if (uevent) { |
| 911 | dev_set_uevent_suppress(f_dev, false); |
| 912 | dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id); |
| 913 | if (timeout != MAX_SCHEDULE_TIMEOUT) |
| 914 | schedule_delayed_work(&fw_priv->timeout_work, timeout); |
| 915 | |
| 916 | kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD); |
| 917 | } |
| 918 | |
| 919 | wait_for_completion(&buf->completion); |
| 920 | |
| 921 | cancel_delayed_work_sync(&fw_priv->timeout_work); |
| 922 | |
| 923 | device_remove_file(f_dev, &dev_attr_loading); |
| 924 | err_del_bin_attr: |
| 925 | device_remove_bin_file(f_dev, &firmware_attr_data); |
| 926 | err_del_dev: |
| 927 | device_del(f_dev); |
| 928 | err_put_dev: |
| 929 | put_device(f_dev); |
| 930 | return retval; |
| 931 | } |
| 932 | |
| 933 | static int fw_load_from_user_helper(struct firmware *firmware, |
| 934 | const char *name, struct device *device, |
| 935 | bool uevent, bool nowait, long timeout) |
| 936 | { |
| 937 | struct firmware_priv *fw_priv; |
| 938 | |
| 939 | fw_priv = fw_create_instance(firmware, name, device, uevent, nowait); |
| 940 | if (IS_ERR(fw_priv)) |
| 941 | return PTR_ERR(fw_priv); |
| 942 | |
| 943 | fw_priv->buf = firmware->priv; |
| 944 | return _request_firmware_load(fw_priv, uevent, timeout); |
| 945 | } |
| 946 | #else /* CONFIG_FW_LOADER_USER_HELPER */ |
| 947 | static inline int |
| 948 | fw_load_from_user_helper(struct firmware *firmware, const char *name, |
| 949 | struct device *device, bool uevent, bool nowait, |
| 950 | long timeout) |
| 951 | { |
| 952 | return -ENOENT; |
| 953 | } |
| 954 | |
| 955 | /* No abort during direct loading */ |
| 956 | #define is_fw_load_aborted(buf) false |
| 957 | |
| 958 | #endif /* CONFIG_FW_LOADER_USER_HELPER */ |
| 959 | |
| 960 | |
| 961 | /* wait until the shared firmware_buf becomes ready (or error) */ |
| 962 | static int sync_cached_firmware_buf(struct firmware_buf *buf) |
| 963 | { |
| 964 | int ret = 0; |
| 965 | |
| 966 | mutex_lock(&fw_lock); |
| 967 | while (!test_bit(FW_STATUS_DONE, &buf->status)) { |
| 968 | if (is_fw_load_aborted(buf)) { |
| 969 | ret = -ENOENT; |
| 970 | break; |
| 971 | } |
| 972 | mutex_unlock(&fw_lock); |
| 973 | wait_for_completion(&buf->completion); |
| 974 | mutex_lock(&fw_lock); |
| 975 | } |
| 976 | mutex_unlock(&fw_lock); |
| 977 | return ret; |
| 978 | } |
| 979 | |
| 980 | /* prepare firmware and firmware_buf structs; |
| 981 | * return 0 if a firmware is already assigned, 1 if need to load one, |
| 982 | * or a negative error code |
| 983 | */ |
| 984 | static int |
| 985 | _request_firmware_prepare(struct firmware **firmware_p, const char *name, |
| 986 | struct device *device) |
| 987 | { |
| 988 | struct firmware *firmware; |
| 989 | struct firmware_buf *buf; |
| 990 | int ret; |
| 991 | |
| 992 | *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL); |
| 993 | if (!firmware) { |
| 994 | dev_err(device, "%s: kmalloc(struct firmware) failed\n", |
| 995 | __func__); |
| 996 | return -ENOMEM; |
| 997 | } |
| 998 | |
| 999 | if (fw_get_builtin_firmware(firmware, name)) { |
| 1000 | dev_dbg(device, "firmware: using built-in firmware %s\n", name); |
| 1001 | return 0; /* assigned */ |
| 1002 | } |
| 1003 | |
| 1004 | ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf); |
| 1005 | |
| 1006 | /* |
| 1007 | * bind with 'buf' now to avoid warning in failure path |
| 1008 | * of requesting firmware. |
| 1009 | */ |
| 1010 | firmware->priv = buf; |
| 1011 | |
| 1012 | if (ret > 0) { |
| 1013 | ret = sync_cached_firmware_buf(buf); |
| 1014 | if (!ret) { |
| 1015 | fw_set_page_data(buf, firmware); |
| 1016 | return 0; /* assigned */ |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | if (ret < 0) |
| 1021 | return ret; |
| 1022 | return 1; /* need to load */ |
| 1023 | } |
| 1024 | |
| 1025 | static int assign_firmware_buf(struct firmware *fw, struct device *device) |
| 1026 | { |
| 1027 | struct firmware_buf *buf = fw->priv; |
| 1028 | |
| 1029 | mutex_lock(&fw_lock); |
| 1030 | if (!buf->size || is_fw_load_aborted(buf)) { |
| 1031 | mutex_unlock(&fw_lock); |
| 1032 | return -ENOENT; |
| 1033 | } |
| 1034 | |
| 1035 | /* |
| 1036 | * add firmware name into devres list so that we can auto cache |
| 1037 | * and uncache firmware for device. |
| 1038 | * |
| 1039 | * device may has been deleted already, but the problem |
| 1040 | * should be fixed in devres or driver core. |
| 1041 | */ |
| 1042 | if (device) |
| 1043 | fw_add_devm_name(device, buf->fw_id); |
| 1044 | |
| 1045 | /* |
| 1046 | * After caching firmware image is started, let it piggyback |
| 1047 | * on request firmware. |
| 1048 | */ |
| 1049 | if (buf->fwc->state == FW_LOADER_START_CACHE) { |
| 1050 | if (fw_cache_piggyback_on_request(buf->fw_id)) |
| 1051 | kref_get(&buf->ref); |
| 1052 | } |
| 1053 | |
| 1054 | /* pass the pages buffer to driver at the last minute */ |
| 1055 | fw_set_page_data(buf, fw); |
| 1056 | mutex_unlock(&fw_lock); |
| 1057 | return 0; |
| 1058 | } |
| 1059 | |
| 1060 | /* called from request_firmware() and request_firmware_work_func() */ |
| 1061 | static int |
| 1062 | _request_firmware(const struct firmware **firmware_p, const char *name, |
| 1063 | struct device *device, bool uevent, bool nowait) |
| 1064 | { |
| 1065 | struct firmware *fw; |
| 1066 | long timeout; |
| 1067 | int ret; |
| 1068 | |
| 1069 | if (!firmware_p) |
| 1070 | return -EINVAL; |
| 1071 | |
| 1072 | if (!name || name[0] == '\0') |
| 1073 | return -EINVAL; |
| 1074 | |
| 1075 | ret = _request_firmware_prepare(&fw, name, device); |
| 1076 | if (ret <= 0) /* error or already assigned */ |
| 1077 | goto out; |
| 1078 | |
| 1079 | ret = 0; |
| 1080 | timeout = firmware_loading_timeout(); |
| 1081 | if (nowait) { |
| 1082 | timeout = usermodehelper_read_lock_wait(timeout); |
| 1083 | if (!timeout) { |
| 1084 | dev_dbg(device, "firmware: %s loading timed out\n", |
| 1085 | name); |
| 1086 | ret = -EBUSY; |
| 1087 | goto out; |
| 1088 | } |
| 1089 | } else { |
| 1090 | ret = usermodehelper_read_trylock(); |
| 1091 | if (WARN_ON(ret)) { |
| 1092 | dev_err(device, "firmware: %s will not be loaded\n", |
| 1093 | name); |
| 1094 | goto out; |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | if (!fw_get_filesystem_firmware(device, fw->priv)) |
| 1099 | ret = fw_load_from_user_helper(fw, name, device, |
| 1100 | uevent, nowait, timeout); |
| 1101 | if (!ret) |
| 1102 | ret = assign_firmware_buf(fw, device); |
| 1103 | |
| 1104 | usermodehelper_read_unlock(); |
| 1105 | |
| 1106 | out: |
| 1107 | if (ret < 0) { |
| 1108 | release_firmware(fw); |
| 1109 | fw = NULL; |
| 1110 | } |
| 1111 | |
| 1112 | *firmware_p = fw; |
| 1113 | return ret; |
| 1114 | } |
| 1115 | |
| 1116 | /** |
| 1117 | * request_firmware: - send firmware request and wait for it |
| 1118 | * @firmware_p: pointer to firmware image |
| 1119 | * @name: name of firmware file |
| 1120 | * @device: device for which firmware is being loaded |
| 1121 | * |
| 1122 | * @firmware_p will be used to return a firmware image by the name |
| 1123 | * of @name for device @device. |
| 1124 | * |
| 1125 | * Should be called from user context where sleeping is allowed. |
| 1126 | * |
| 1127 | * @name will be used as $FIRMWARE in the uevent environment and |
| 1128 | * should be distinctive enough not to be confused with any other |
| 1129 | * firmware image for this or any other device. |
| 1130 | * |
| 1131 | * Caller must hold the reference count of @device. |
| 1132 | * |
| 1133 | * The function can be called safely inside device's suspend and |
| 1134 | * resume callback. |
| 1135 | **/ |
| 1136 | int |
| 1137 | request_firmware(const struct firmware **firmware_p, const char *name, |
| 1138 | struct device *device) |
| 1139 | { |
| 1140 | return _request_firmware(firmware_p, name, device, true, false); |
| 1141 | } |
| 1142 | |
| 1143 | /** |
| 1144 | * release_firmware: - release the resource associated with a firmware image |
| 1145 | * @fw: firmware resource to release |
| 1146 | **/ |
| 1147 | void release_firmware(const struct firmware *fw) |
| 1148 | { |
| 1149 | if (fw) { |
| 1150 | if (!fw_is_builtin_firmware(fw)) |
| 1151 | firmware_free_data(fw); |
| 1152 | kfree(fw); |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | /* Async support */ |
| 1157 | struct firmware_work { |
| 1158 | struct work_struct work; |
| 1159 | struct module *module; |
| 1160 | const char *name; |
| 1161 | struct device *device; |
| 1162 | void *context; |
| 1163 | void (*cont)(const struct firmware *fw, void *context); |
| 1164 | bool uevent; |
| 1165 | }; |
| 1166 | |
| 1167 | static void request_firmware_work_func(struct work_struct *work) |
| 1168 | { |
| 1169 | struct firmware_work *fw_work; |
| 1170 | const struct firmware *fw; |
| 1171 | |
| 1172 | fw_work = container_of(work, struct firmware_work, work); |
| 1173 | |
| 1174 | _request_firmware(&fw, fw_work->name, fw_work->device, |
| 1175 | fw_work->uevent, true); |
| 1176 | fw_work->cont(fw, fw_work->context); |
| 1177 | put_device(fw_work->device); /* taken in request_firmware_nowait() */ |
| 1178 | |
| 1179 | module_put(fw_work->module); |
| 1180 | kfree(fw_work); |
| 1181 | } |
| 1182 | |
| 1183 | /** |
| 1184 | * request_firmware_nowait - asynchronous version of request_firmware |
| 1185 | * @module: module requesting the firmware |
| 1186 | * @uevent: sends uevent to copy the firmware image if this flag |
| 1187 | * is non-zero else the firmware copy must be done manually. |
| 1188 | * @name: name of firmware file |
| 1189 | * @device: device for which firmware is being loaded |
| 1190 | * @gfp: allocation flags |
| 1191 | * @context: will be passed over to @cont, and |
| 1192 | * @fw may be %NULL if firmware request fails. |
| 1193 | * @cont: function will be called asynchronously when the firmware |
| 1194 | * request is over. |
| 1195 | * |
| 1196 | * Caller must hold the reference count of @device. |
| 1197 | * |
| 1198 | * Asynchronous variant of request_firmware() for user contexts: |
| 1199 | * - sleep for as small periods as possible since it may |
| 1200 | * increase kernel boot time of built-in device drivers |
| 1201 | * requesting firmware in their ->probe() methods, if |
| 1202 | * @gfp is GFP_KERNEL. |
| 1203 | * |
| 1204 | * - can't sleep at all if @gfp is GFP_ATOMIC. |
| 1205 | **/ |
| 1206 | int |
| 1207 | request_firmware_nowait( |
| 1208 | struct module *module, bool uevent, |
| 1209 | const char *name, struct device *device, gfp_t gfp, void *context, |
| 1210 | void (*cont)(const struct firmware *fw, void *context)) |
| 1211 | { |
| 1212 | struct firmware_work *fw_work; |
| 1213 | |
| 1214 | fw_work = kzalloc(sizeof (struct firmware_work), gfp); |
| 1215 | if (!fw_work) |
| 1216 | return -ENOMEM; |
| 1217 | |
| 1218 | fw_work->module = module; |
| 1219 | fw_work->name = name; |
| 1220 | fw_work->device = device; |
| 1221 | fw_work->context = context; |
| 1222 | fw_work->cont = cont; |
| 1223 | fw_work->uevent = uevent; |
| 1224 | |
| 1225 | if (!try_module_get(module)) { |
| 1226 | kfree(fw_work); |
| 1227 | return -EFAULT; |
| 1228 | } |
| 1229 | |
| 1230 | get_device(fw_work->device); |
| 1231 | INIT_WORK(&fw_work->work, request_firmware_work_func); |
| 1232 | schedule_work(&fw_work->work); |
| 1233 | return 0; |
| 1234 | } |
| 1235 | |
| 1236 | /** |
| 1237 | * cache_firmware - cache one firmware image in kernel memory space |
| 1238 | * @fw_name: the firmware image name |
| 1239 | * |
| 1240 | * Cache firmware in kernel memory so that drivers can use it when |
| 1241 | * system isn't ready for them to request firmware image from userspace. |
| 1242 | * Once it returns successfully, driver can use request_firmware or its |
| 1243 | * nowait version to get the cached firmware without any interacting |
| 1244 | * with userspace |
| 1245 | * |
| 1246 | * Return 0 if the firmware image has been cached successfully |
| 1247 | * Return !0 otherwise |
| 1248 | * |
| 1249 | */ |
| 1250 | int cache_firmware(const char *fw_name) |
| 1251 | { |
| 1252 | int ret; |
| 1253 | const struct firmware *fw; |
| 1254 | |
| 1255 | pr_debug("%s: %s\n", __func__, fw_name); |
| 1256 | |
| 1257 | ret = request_firmware(&fw, fw_name, NULL); |
| 1258 | if (!ret) |
| 1259 | kfree(fw); |
| 1260 | |
| 1261 | pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret); |
| 1262 | |
| 1263 | return ret; |
| 1264 | } |
| 1265 | |
| 1266 | /** |
| 1267 | * uncache_firmware - remove one cached firmware image |
| 1268 | * @fw_name: the firmware image name |
| 1269 | * |
| 1270 | * Uncache one firmware image which has been cached successfully |
| 1271 | * before. |
| 1272 | * |
| 1273 | * Return 0 if the firmware cache has been removed successfully |
| 1274 | * Return !0 otherwise |
| 1275 | * |
| 1276 | */ |
| 1277 | int uncache_firmware(const char *fw_name) |
| 1278 | { |
| 1279 | struct firmware_buf *buf; |
| 1280 | struct firmware fw; |
| 1281 | |
| 1282 | pr_debug("%s: %s\n", __func__, fw_name); |
| 1283 | |
| 1284 | if (fw_get_builtin_firmware(&fw, fw_name)) |
| 1285 | return 0; |
| 1286 | |
| 1287 | buf = fw_lookup_buf(fw_name); |
| 1288 | if (buf) { |
| 1289 | fw_free_buf(buf); |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
| 1293 | return -EINVAL; |
| 1294 | } |
| 1295 | |
| 1296 | #ifdef CONFIG_PM_SLEEP |
| 1297 | static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain); |
| 1298 | |
| 1299 | static struct fw_cache_entry *alloc_fw_cache_entry(const char *name) |
| 1300 | { |
| 1301 | struct fw_cache_entry *fce; |
| 1302 | |
| 1303 | fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC); |
| 1304 | if (!fce) |
| 1305 | goto exit; |
| 1306 | |
| 1307 | strcpy(fce->name, name); |
| 1308 | exit: |
| 1309 | return fce; |
| 1310 | } |
| 1311 | |
| 1312 | static int __fw_entry_found(const char *name) |
| 1313 | { |
| 1314 | struct firmware_cache *fwc = &fw_cache; |
| 1315 | struct fw_cache_entry *fce; |
| 1316 | |
| 1317 | list_for_each_entry(fce, &fwc->fw_names, list) { |
| 1318 | if (!strcmp(fce->name, name)) |
| 1319 | return 1; |
| 1320 | } |
| 1321 | return 0; |
| 1322 | } |
| 1323 | |
| 1324 | static int fw_cache_piggyback_on_request(const char *name) |
| 1325 | { |
| 1326 | struct firmware_cache *fwc = &fw_cache; |
| 1327 | struct fw_cache_entry *fce; |
| 1328 | int ret = 0; |
| 1329 | |
| 1330 | spin_lock(&fwc->name_lock); |
| 1331 | if (__fw_entry_found(name)) |
| 1332 | goto found; |
| 1333 | |
| 1334 | fce = alloc_fw_cache_entry(name); |
| 1335 | if (fce) { |
| 1336 | ret = 1; |
| 1337 | list_add(&fce->list, &fwc->fw_names); |
| 1338 | pr_debug("%s: fw: %s\n", __func__, name); |
| 1339 | } |
| 1340 | found: |
| 1341 | spin_unlock(&fwc->name_lock); |
| 1342 | return ret; |
| 1343 | } |
| 1344 | |
| 1345 | static void free_fw_cache_entry(struct fw_cache_entry *fce) |
| 1346 | { |
| 1347 | kfree(fce); |
| 1348 | } |
| 1349 | |
| 1350 | static void __async_dev_cache_fw_image(void *fw_entry, |
| 1351 | async_cookie_t cookie) |
| 1352 | { |
| 1353 | struct fw_cache_entry *fce = fw_entry; |
| 1354 | struct firmware_cache *fwc = &fw_cache; |
| 1355 | int ret; |
| 1356 | |
| 1357 | ret = cache_firmware(fce->name); |
| 1358 | if (ret) { |
| 1359 | spin_lock(&fwc->name_lock); |
| 1360 | list_del(&fce->list); |
| 1361 | spin_unlock(&fwc->name_lock); |
| 1362 | |
| 1363 | free_fw_cache_entry(fce); |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | /* called with dev->devres_lock held */ |
| 1368 | static void dev_create_fw_entry(struct device *dev, void *res, |
| 1369 | void *data) |
| 1370 | { |
| 1371 | struct fw_name_devm *fwn = res; |
| 1372 | const char *fw_name = fwn->name; |
| 1373 | struct list_head *head = data; |
| 1374 | struct fw_cache_entry *fce; |
| 1375 | |
| 1376 | fce = alloc_fw_cache_entry(fw_name); |
| 1377 | if (fce) |
| 1378 | list_add(&fce->list, head); |
| 1379 | } |
| 1380 | |
| 1381 | static int devm_name_match(struct device *dev, void *res, |
| 1382 | void *match_data) |
| 1383 | { |
| 1384 | struct fw_name_devm *fwn = res; |
| 1385 | return (fwn->magic == (unsigned long)match_data); |
| 1386 | } |
| 1387 | |
| 1388 | static void dev_cache_fw_image(struct device *dev, void *data) |
| 1389 | { |
| 1390 | LIST_HEAD(todo); |
| 1391 | struct fw_cache_entry *fce; |
| 1392 | struct fw_cache_entry *fce_next; |
| 1393 | struct firmware_cache *fwc = &fw_cache; |
| 1394 | |
| 1395 | devres_for_each_res(dev, fw_name_devm_release, |
| 1396 | devm_name_match, &fw_cache, |
| 1397 | dev_create_fw_entry, &todo); |
| 1398 | |
| 1399 | list_for_each_entry_safe(fce, fce_next, &todo, list) { |
| 1400 | list_del(&fce->list); |
| 1401 | |
| 1402 | spin_lock(&fwc->name_lock); |
| 1403 | /* only one cache entry for one firmware */ |
| 1404 | if (!__fw_entry_found(fce->name)) { |
| 1405 | list_add(&fce->list, &fwc->fw_names); |
| 1406 | } else { |
| 1407 | free_fw_cache_entry(fce); |
| 1408 | fce = NULL; |
| 1409 | } |
| 1410 | spin_unlock(&fwc->name_lock); |
| 1411 | |
| 1412 | if (fce) |
| 1413 | async_schedule_domain(__async_dev_cache_fw_image, |
| 1414 | (void *)fce, |
| 1415 | &fw_cache_domain); |
| 1416 | } |
| 1417 | } |
| 1418 | |
| 1419 | static void __device_uncache_fw_images(void) |
| 1420 | { |
| 1421 | struct firmware_cache *fwc = &fw_cache; |
| 1422 | struct fw_cache_entry *fce; |
| 1423 | |
| 1424 | spin_lock(&fwc->name_lock); |
| 1425 | while (!list_empty(&fwc->fw_names)) { |
| 1426 | fce = list_entry(fwc->fw_names.next, |
| 1427 | struct fw_cache_entry, list); |
| 1428 | list_del(&fce->list); |
| 1429 | spin_unlock(&fwc->name_lock); |
| 1430 | |
| 1431 | uncache_firmware(fce->name); |
| 1432 | free_fw_cache_entry(fce); |
| 1433 | |
| 1434 | spin_lock(&fwc->name_lock); |
| 1435 | } |
| 1436 | spin_unlock(&fwc->name_lock); |
| 1437 | } |
| 1438 | |
| 1439 | /** |
| 1440 | * device_cache_fw_images - cache devices' firmware |
| 1441 | * |
| 1442 | * If one device called request_firmware or its nowait version |
| 1443 | * successfully before, the firmware names are recored into the |
| 1444 | * device's devres link list, so device_cache_fw_images can call |
| 1445 | * cache_firmware() to cache these firmwares for the device, |
| 1446 | * then the device driver can load its firmwares easily at |
| 1447 | * time when system is not ready to complete loading firmware. |
| 1448 | */ |
| 1449 | static void device_cache_fw_images(void) |
| 1450 | { |
| 1451 | struct firmware_cache *fwc = &fw_cache; |
| 1452 | int old_timeout; |
| 1453 | DEFINE_WAIT(wait); |
| 1454 | |
| 1455 | pr_debug("%s\n", __func__); |
| 1456 | |
| 1457 | /* cancel uncache work */ |
| 1458 | cancel_delayed_work_sync(&fwc->work); |
| 1459 | |
| 1460 | /* |
| 1461 | * use small loading timeout for caching devices' firmware |
| 1462 | * because all these firmware images have been loaded |
| 1463 | * successfully at lease once, also system is ready for |
| 1464 | * completing firmware loading now. The maximum size of |
| 1465 | * firmware in current distributions is about 2M bytes, |
| 1466 | * so 10 secs should be enough. |
| 1467 | */ |
| 1468 | old_timeout = loading_timeout; |
| 1469 | loading_timeout = 10; |
| 1470 | |
| 1471 | mutex_lock(&fw_lock); |
| 1472 | fwc->state = FW_LOADER_START_CACHE; |
| 1473 | dpm_for_each_dev(NULL, dev_cache_fw_image); |
| 1474 | mutex_unlock(&fw_lock); |
| 1475 | |
| 1476 | /* wait for completion of caching firmware for all devices */ |
| 1477 | async_synchronize_full_domain(&fw_cache_domain); |
| 1478 | |
| 1479 | loading_timeout = old_timeout; |
| 1480 | } |
| 1481 | |
| 1482 | /** |
| 1483 | * device_uncache_fw_images - uncache devices' firmware |
| 1484 | * |
| 1485 | * uncache all firmwares which have been cached successfully |
| 1486 | * by device_uncache_fw_images earlier |
| 1487 | */ |
| 1488 | static void device_uncache_fw_images(void) |
| 1489 | { |
| 1490 | pr_debug("%s\n", __func__); |
| 1491 | __device_uncache_fw_images(); |
| 1492 | } |
| 1493 | |
| 1494 | static void device_uncache_fw_images_work(struct work_struct *work) |
| 1495 | { |
| 1496 | device_uncache_fw_images(); |
| 1497 | } |
| 1498 | |
| 1499 | /** |
| 1500 | * device_uncache_fw_images_delay - uncache devices firmwares |
| 1501 | * @delay: number of milliseconds to delay uncache device firmwares |
| 1502 | * |
| 1503 | * uncache all devices's firmwares which has been cached successfully |
| 1504 | * by device_cache_fw_images after @delay milliseconds. |
| 1505 | */ |
| 1506 | static void device_uncache_fw_images_delay(unsigned long delay) |
| 1507 | { |
| 1508 | schedule_delayed_work(&fw_cache.work, |
| 1509 | msecs_to_jiffies(delay)); |
| 1510 | } |
| 1511 | |
| 1512 | static int fw_pm_notify(struct notifier_block *notify_block, |
| 1513 | unsigned long mode, void *unused) |
| 1514 | { |
| 1515 | switch (mode) { |
| 1516 | case PM_HIBERNATION_PREPARE: |
| 1517 | case PM_SUSPEND_PREPARE: |
| 1518 | device_cache_fw_images(); |
| 1519 | break; |
| 1520 | |
| 1521 | case PM_POST_SUSPEND: |
| 1522 | case PM_POST_HIBERNATION: |
| 1523 | case PM_POST_RESTORE: |
| 1524 | /* |
| 1525 | * In case that system sleep failed and syscore_suspend is |
| 1526 | * not called. |
| 1527 | */ |
| 1528 | mutex_lock(&fw_lock); |
| 1529 | fw_cache.state = FW_LOADER_NO_CACHE; |
| 1530 | mutex_unlock(&fw_lock); |
| 1531 | |
| 1532 | device_uncache_fw_images_delay(10 * MSEC_PER_SEC); |
| 1533 | break; |
| 1534 | } |
| 1535 | |
| 1536 | return 0; |
| 1537 | } |
| 1538 | |
| 1539 | /* stop caching firmware once syscore_suspend is reached */ |
| 1540 | static int fw_suspend(void) |
| 1541 | { |
| 1542 | fw_cache.state = FW_LOADER_NO_CACHE; |
| 1543 | return 0; |
| 1544 | } |
| 1545 | |
| 1546 | static struct syscore_ops fw_syscore_ops = { |
| 1547 | .suspend = fw_suspend, |
| 1548 | }; |
| 1549 | #else |
| 1550 | static int fw_cache_piggyback_on_request(const char *name) |
| 1551 | { |
| 1552 | return 0; |
| 1553 | } |
| 1554 | #endif |
| 1555 | |
| 1556 | static void __init fw_cache_init(void) |
| 1557 | { |
| 1558 | spin_lock_init(&fw_cache.lock); |
| 1559 | INIT_LIST_HEAD(&fw_cache.head); |
| 1560 | fw_cache.state = FW_LOADER_NO_CACHE; |
| 1561 | |
| 1562 | #ifdef CONFIG_PM_SLEEP |
| 1563 | spin_lock_init(&fw_cache.name_lock); |
| 1564 | INIT_LIST_HEAD(&fw_cache.fw_names); |
| 1565 | |
| 1566 | INIT_DELAYED_WORK(&fw_cache.work, |
| 1567 | device_uncache_fw_images_work); |
| 1568 | |
| 1569 | fw_cache.pm_notify.notifier_call = fw_pm_notify; |
| 1570 | register_pm_notifier(&fw_cache.pm_notify); |
| 1571 | |
| 1572 | register_syscore_ops(&fw_syscore_ops); |
| 1573 | #endif |
| 1574 | } |
| 1575 | |
| 1576 | static int __init firmware_class_init(void) |
| 1577 | { |
| 1578 | fw_cache_init(); |
| 1579 | #ifdef CONFIG_FW_LOADER_USER_HELPER |
| 1580 | register_reboot_notifier(&fw_shutdown_nb); |
| 1581 | return class_register(&firmware_class); |
| 1582 | #else |
| 1583 | return 0; |
| 1584 | #endif |
| 1585 | } |
| 1586 | |
| 1587 | static void __exit firmware_class_exit(void) |
| 1588 | { |
| 1589 | #ifdef CONFIG_PM_SLEEP |
| 1590 | unregister_syscore_ops(&fw_syscore_ops); |
| 1591 | unregister_pm_notifier(&fw_cache.pm_notify); |
| 1592 | #endif |
| 1593 | #ifdef CONFIG_FW_LOADER_USER_HELPER |
| 1594 | unregister_reboot_notifier(&fw_shutdown_nb); |
| 1595 | class_unregister(&firmware_class); |
| 1596 | #endif |
| 1597 | } |
| 1598 | |
| 1599 | fs_initcall(firmware_class_init); |
| 1600 | module_exit(firmware_class_exit); |
| 1601 | |
| 1602 | EXPORT_SYMBOL(release_firmware); |
| 1603 | EXPORT_SYMBOL(request_firmware); |
| 1604 | EXPORT_SYMBOL(request_firmware_nowait); |
| 1605 | EXPORT_SYMBOL_GPL(cache_firmware); |
| 1606 | EXPORT_SYMBOL_GPL(uncache_firmware); |