mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / base / firmware_class.c
blobcdd151e2d122263a8741dee0d16dbc1ae0ccbfe3
1 /*
2 * firmware_class.c - Multi purpose firmware loading support
4 * Copyright (c) 2003 Manuel Estrada Sainz
6 * Please see Documentation/firmware_class/ for more information.
8 */
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>
32 #include <generated/utsrelease.h>
34 #include "base.h"
36 MODULE_AUTHOR("Manuel Estrada Sainz");
37 MODULE_DESCRIPTION("Multi purpose firmware loading support");
38 MODULE_LICENSE("GPL");
40 /* Builtin firmware support */
42 #ifdef CONFIG_FW_LOADER
44 extern struct builtin_fw __start_builtin_fw[];
45 extern struct builtin_fw __end_builtin_fw[];
47 static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
49 struct builtin_fw *b_fw;
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;
59 return false;
62 static bool fw_is_builtin_firmware(const struct firmware *fw)
64 struct builtin_fw *b_fw;
66 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
67 if (fw->data == b_fw->data)
68 return true;
70 return false;
73 #else /* Module case - no builtin firmware support */
75 static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
77 return false;
80 static inline bool fw_is_builtin_firmware(const struct firmware *fw)
82 return false;
84 #endif
86 enum {
87 FW_STATUS_LOADING,
88 FW_STATUS_DONE,
89 FW_STATUS_ABORT,
92 static int loading_timeout = 60; /* In seconds */
94 static inline long firmware_loading_timeout(void)
96 return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
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;
105 #ifdef CONFIG_PM_SLEEP
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.
112 spinlock_t name_lock;
113 struct list_head fw_names;
115 struct delayed_work work;
117 struct notifier_block pm_notify;
118 #endif
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 bool need_uevent;
132 struct page **pages;
133 int nr_pages;
134 int page_array_size;
135 struct list_head pending_list;
136 #endif
137 char fw_id[];
140 struct fw_cache_entry {
141 struct list_head list;
142 char name[];
145 struct fw_name_devm {
146 unsigned long magic;
147 char name[];
150 #define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
152 #define FW_LOADER_NO_CACHE 0
153 #define FW_LOADER_START_CACHE 1
155 static int fw_cache_piggyback_on_request(const char *name);
157 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
158 * guarding for corner cases a global lock should be OK */
159 static DEFINE_MUTEX(fw_lock);
161 static struct firmware_cache fw_cache;
163 static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
164 struct firmware_cache *fwc)
166 struct firmware_buf *buf;
168 buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
170 if (!buf)
171 return buf;
173 kref_init(&buf->ref);
174 strcpy(buf->fw_id, fw_name);
175 buf->fwc = fwc;
176 init_completion(&buf->completion);
177 #ifdef CONFIG_FW_LOADER_USER_HELPER
178 INIT_LIST_HEAD(&buf->pending_list);
179 #endif
181 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
183 return buf;
186 static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
188 struct firmware_buf *tmp;
189 struct firmware_cache *fwc = &fw_cache;
191 list_for_each_entry(tmp, &fwc->head, list)
192 if (!strcmp(tmp->fw_id, fw_name))
193 return tmp;
194 return NULL;
197 static int fw_lookup_and_allocate_buf(const char *fw_name,
198 struct firmware_cache *fwc,
199 struct firmware_buf **buf)
201 struct firmware_buf *tmp;
203 spin_lock(&fwc->lock);
204 tmp = __fw_lookup_buf(fw_name);
205 if (tmp) {
206 kref_get(&tmp->ref);
207 spin_unlock(&fwc->lock);
208 *buf = tmp;
209 return 1;
211 tmp = __allocate_fw_buf(fw_name, fwc);
212 if (tmp)
213 list_add(&tmp->list, &fwc->head);
214 spin_unlock(&fwc->lock);
216 *buf = tmp;
218 return tmp ? 0 : -ENOMEM;
221 static void __fw_free_buf(struct kref *ref)
223 struct firmware_buf *buf = to_fwbuf(ref);
224 struct firmware_cache *fwc = buf->fwc;
226 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
227 __func__, buf->fw_id, buf, buf->data,
228 (unsigned int)buf->size);
230 list_del(&buf->list);
231 spin_unlock(&fwc->lock);
233 #ifdef CONFIG_FW_LOADER_USER_HELPER
234 if (buf->is_paged_buf) {
235 int i;
236 vunmap(buf->data);
237 for (i = 0; i < buf->nr_pages; i++)
238 __free_page(buf->pages[i]);
239 kfree(buf->pages);
240 } else
241 #endif
242 vfree(buf->data);
243 kfree(buf);
246 static void fw_free_buf(struct firmware_buf *buf)
248 struct firmware_cache *fwc = buf->fwc;
249 spin_lock(&fwc->lock);
250 if (!kref_put(&buf->ref, __fw_free_buf))
251 spin_unlock(&fwc->lock);
254 /* direct firmware loading support */
255 static char fw_path_para[256];
256 static const char * const fw_path[] = {
257 fw_path_para,
258 "/lib/firmware/updates/" UTS_RELEASE,
259 "/lib/firmware/updates",
260 "/lib/firmware/" UTS_RELEASE,
261 "/lib/firmware"
265 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
266 * from kernel command line because firmware_class is generally built in
267 * kernel instead of module.
269 module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
270 MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
272 /* Don't inline this: 'struct kstat' is biggish */
273 static noinline_for_stack long fw_file_size(struct file *file)
275 struct kstat st;
276 if (vfs_getattr(&file->f_path, &st))
277 return -1;
278 if (!S_ISREG(st.mode))
279 return -1;
280 if (st.size != (long)st.size)
281 return -1;
282 return st.size;
285 static bool fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf)
287 long size;
288 char *buf;
290 size = fw_file_size(file);
291 if (size <= 0)
292 return false;
293 buf = vmalloc(size);
294 if (!buf)
295 return false;
296 if (kernel_read(file, 0, buf, size) != size) {
297 vfree(buf);
298 return false;
300 fw_buf->data = buf;
301 fw_buf->size = size;
302 return true;
305 static bool fw_get_filesystem_firmware(struct device *device,
306 struct firmware_buf *buf)
308 int i;
309 bool success = false;
310 char *path = __getname();
312 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
313 struct file *file;
315 /* skip the unset customized path */
316 if (!fw_path[i][0])
317 continue;
319 snprintf(path, PATH_MAX, "%s/%s", fw_path[i], buf->fw_id);
321 file = filp_open(path, O_RDONLY, 0);
322 if (IS_ERR(file))
323 continue;
324 success = fw_read_file_contents(file, buf);
325 fput(file);
326 if (success)
327 break;
329 __putname(path);
331 if (success) {
332 dev_dbg(device, "firmware: direct-loading firmware %s\n",
333 buf->fw_id);
334 mutex_lock(&fw_lock);
335 set_bit(FW_STATUS_DONE, &buf->status);
336 complete_all(&buf->completion);
337 mutex_unlock(&fw_lock);
340 return success;
343 /* firmware holds the ownership of pages */
344 static void firmware_free_data(const struct firmware *fw)
346 /* Loaded directly? */
347 if (!fw->priv) {
348 vfree(fw->data);
349 return;
351 fw_free_buf(fw->priv);
354 /* store the pages buffer info firmware from buf */
355 static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
357 fw->priv = buf;
358 #ifdef CONFIG_FW_LOADER_USER_HELPER
359 fw->pages = buf->pages;
360 #endif
361 fw->size = buf->size;
362 fw->data = buf->data;
364 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
365 __func__, buf->fw_id, buf, buf->data,
366 (unsigned int)buf->size);
369 #ifdef CONFIG_PM_SLEEP
370 static void fw_name_devm_release(struct device *dev, void *res)
372 struct fw_name_devm *fwn = res;
374 if (fwn->magic == (unsigned long)&fw_cache)
375 pr_debug("%s: fw_name-%s devm-%p released\n",
376 __func__, fwn->name, res);
379 static int fw_devm_match(struct device *dev, void *res,
380 void *match_data)
382 struct fw_name_devm *fwn = res;
384 return (fwn->magic == (unsigned long)&fw_cache) &&
385 !strcmp(fwn->name, match_data);
388 static struct fw_name_devm *fw_find_devm_name(struct device *dev,
389 const char *name)
391 struct fw_name_devm *fwn;
393 fwn = devres_find(dev, fw_name_devm_release,
394 fw_devm_match, (void *)name);
395 return fwn;
398 /* add firmware name into devres list */
399 static int fw_add_devm_name(struct device *dev, const char *name)
401 struct fw_name_devm *fwn;
403 fwn = fw_find_devm_name(dev, name);
404 if (fwn)
405 return 1;
407 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
408 strlen(name) + 1, GFP_KERNEL);
409 if (!fwn)
410 return -ENOMEM;
412 fwn->magic = (unsigned long)&fw_cache;
413 strcpy(fwn->name, name);
414 devres_add(dev, fwn);
416 return 0;
418 #else
419 static int fw_add_devm_name(struct device *dev, const char *name)
421 return 0;
423 #endif
427 * user-mode helper code
429 #ifdef CONFIG_FW_LOADER_USER_HELPER
430 struct firmware_priv {
431 struct delayed_work timeout_work;
432 bool nowait;
433 struct device dev;
434 struct firmware_buf *buf;
435 struct firmware *fw;
438 static struct firmware_priv *to_firmware_priv(struct device *dev)
440 return container_of(dev, struct firmware_priv, dev);
443 static void __fw_load_abort(struct firmware_buf *buf)
446 * There is a small window in which user can write to 'loading'
447 * between loading done and disappearance of 'loading'
449 if (test_bit(FW_STATUS_DONE, &buf->status))
450 return;
452 list_del_init(&buf->pending_list);
453 set_bit(FW_STATUS_ABORT, &buf->status);
454 complete_all(&buf->completion);
457 static void fw_load_abort(struct firmware_priv *fw_priv)
459 struct firmware_buf *buf = fw_priv->buf;
461 __fw_load_abort(buf);
463 /* avoid user action after loading abort */
464 fw_priv->buf = NULL;
467 #define is_fw_load_aborted(buf) \
468 test_bit(FW_STATUS_ABORT, &(buf)->status)
470 static LIST_HEAD(pending_fw_head);
472 /* reboot notifier for avoid deadlock with usermode_lock */
473 static int fw_shutdown_notify(struct notifier_block *unused1,
474 unsigned long unused2, void *unused3)
476 mutex_lock(&fw_lock);
477 while (!list_empty(&pending_fw_head))
478 __fw_load_abort(list_first_entry(&pending_fw_head,
479 struct firmware_buf,
480 pending_list));
481 mutex_unlock(&fw_lock);
482 return NOTIFY_DONE;
485 static struct notifier_block fw_shutdown_nb = {
486 .notifier_call = fw_shutdown_notify,
489 static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
490 char *buf)
492 return sprintf(buf, "%d\n", loading_timeout);
496 * firmware_timeout_store - set number of seconds to wait for firmware
497 * @class: device class pointer
498 * @attr: device attribute pointer
499 * @buf: buffer to scan for timeout value
500 * @count: number of bytes in @buf
502 * Sets the number of seconds to wait for the firmware. Once
503 * this expires an error will be returned to the driver and no
504 * firmware will be provided.
506 * Note: zero means 'wait forever'.
508 static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
509 const char *buf, size_t count)
511 loading_timeout = simple_strtol(buf, NULL, 10);
512 if (loading_timeout < 0)
513 loading_timeout = 0;
515 return count;
518 static struct class_attribute firmware_class_attrs[] = {
519 __ATTR_RW(timeout),
520 __ATTR_NULL
523 static void fw_dev_release(struct device *dev)
525 struct firmware_priv *fw_priv = to_firmware_priv(dev);
527 kfree(fw_priv);
530 static int do_firmware_uevent(struct firmware_priv *fw_priv, struct kobj_uevent_env *env)
532 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
533 return -ENOMEM;
534 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
535 return -ENOMEM;
536 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
537 return -ENOMEM;
539 return 0;
542 static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
544 struct firmware_priv *fw_priv = to_firmware_priv(dev);
545 int err = 0;
547 mutex_lock(&fw_lock);
548 if (fw_priv->buf)
549 err = do_firmware_uevent(fw_priv, env);
550 mutex_unlock(&fw_lock);
551 return err;
554 static struct class firmware_class = {
555 .name = "firmware",
556 .class_attrs = firmware_class_attrs,
557 .dev_uevent = firmware_uevent,
558 .dev_release = fw_dev_release,
561 static ssize_t firmware_loading_show(struct device *dev,
562 struct device_attribute *attr, char *buf)
564 struct firmware_priv *fw_priv = to_firmware_priv(dev);
565 int loading = 0;
567 mutex_lock(&fw_lock);
568 if (fw_priv->buf)
569 loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
570 mutex_unlock(&fw_lock);
572 return sprintf(buf, "%d\n", loading);
575 /* Some architectures don't have PAGE_KERNEL_RO */
576 #ifndef PAGE_KERNEL_RO
577 #define PAGE_KERNEL_RO PAGE_KERNEL
578 #endif
580 /* one pages buffer should be mapped/unmapped only once */
581 static int fw_map_pages_buf(struct firmware_buf *buf)
583 if (!buf->is_paged_buf)
584 return 0;
586 if (buf->data)
587 vunmap(buf->data);
588 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
589 if (!buf->data)
590 return -ENOMEM;
591 return 0;
595 * firmware_loading_store - set value in the 'loading' control file
596 * @dev: device pointer
597 * @attr: device attribute pointer
598 * @buf: buffer to scan for loading control value
599 * @count: number of bytes in @buf
601 * The relevant values are:
603 * 1: Start a load, discarding any previous partial load.
604 * 0: Conclude the load and hand the data to the driver code.
605 * -1: Conclude the load with an error and discard any written data.
607 static ssize_t firmware_loading_store(struct device *dev,
608 struct device_attribute *attr,
609 const char *buf, size_t count)
611 struct firmware_priv *fw_priv = to_firmware_priv(dev);
612 struct firmware_buf *fw_buf;
613 int loading = simple_strtol(buf, NULL, 10);
614 int i;
616 mutex_lock(&fw_lock);
617 fw_buf = fw_priv->buf;
618 if (!fw_buf)
619 goto out;
621 switch (loading) {
622 case 1:
623 /* discarding any previous partial load */
624 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
625 for (i = 0; i < fw_buf->nr_pages; i++)
626 __free_page(fw_buf->pages[i]);
627 kfree(fw_buf->pages);
628 fw_buf->pages = NULL;
629 fw_buf->page_array_size = 0;
630 fw_buf->nr_pages = 0;
631 set_bit(FW_STATUS_LOADING, &fw_buf->status);
633 break;
634 case 0:
635 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
636 set_bit(FW_STATUS_DONE, &fw_buf->status);
637 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
640 * Several loading requests may be pending on
641 * one same firmware buf, so let all requests
642 * see the mapped 'buf->data' once the loading
643 * is completed.
644 * */
645 fw_map_pages_buf(fw_buf);
646 list_del_init(&fw_buf->pending_list);
647 complete_all(&fw_buf->completion);
648 break;
650 /* fallthrough */
651 default:
652 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
653 /* fallthrough */
654 case -1:
655 fw_load_abort(fw_priv);
656 break;
658 out:
659 mutex_unlock(&fw_lock);
660 return count;
663 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
665 static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
666 struct bin_attribute *bin_attr,
667 char *buffer, loff_t offset, size_t count)
669 struct device *dev = kobj_to_dev(kobj);
670 struct firmware_priv *fw_priv = to_firmware_priv(dev);
671 struct firmware_buf *buf;
672 ssize_t ret_count;
674 mutex_lock(&fw_lock);
675 buf = fw_priv->buf;
676 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
677 ret_count = -ENODEV;
678 goto out;
680 if (offset > buf->size) {
681 ret_count = 0;
682 goto out;
684 if (count > buf->size - offset)
685 count = buf->size - offset;
687 ret_count = count;
689 while (count) {
690 void *page_data;
691 int page_nr = offset >> PAGE_SHIFT;
692 int page_ofs = offset & (PAGE_SIZE-1);
693 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
695 page_data = kmap(buf->pages[page_nr]);
697 memcpy(buffer, page_data + page_ofs, page_cnt);
699 kunmap(buf->pages[page_nr]);
700 buffer += page_cnt;
701 offset += page_cnt;
702 count -= page_cnt;
704 out:
705 mutex_unlock(&fw_lock);
706 return ret_count;
709 static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
711 struct firmware_buf *buf = fw_priv->buf;
712 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
714 /* If the array of pages is too small, grow it... */
715 if (buf->page_array_size < pages_needed) {
716 int new_array_size = max(pages_needed,
717 buf->page_array_size * 2);
718 struct page **new_pages;
720 new_pages = kmalloc(new_array_size * sizeof(void *),
721 GFP_KERNEL);
722 if (!new_pages) {
723 fw_load_abort(fw_priv);
724 return -ENOMEM;
726 memcpy(new_pages, buf->pages,
727 buf->page_array_size * sizeof(void *));
728 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
729 (new_array_size - buf->page_array_size));
730 kfree(buf->pages);
731 buf->pages = new_pages;
732 buf->page_array_size = new_array_size;
735 while (buf->nr_pages < pages_needed) {
736 buf->pages[buf->nr_pages] =
737 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
739 if (!buf->pages[buf->nr_pages]) {
740 fw_load_abort(fw_priv);
741 return -ENOMEM;
743 buf->nr_pages++;
745 return 0;
749 * firmware_data_write - write method for firmware
750 * @filp: open sysfs file
751 * @kobj: kobject for the device
752 * @bin_attr: bin_attr structure
753 * @buffer: buffer being written
754 * @offset: buffer offset for write in total data store area
755 * @count: buffer size
757 * Data written to the 'data' attribute will be later handed to
758 * the driver as a firmware image.
760 static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
761 struct bin_attribute *bin_attr,
762 char *buffer, loff_t offset, size_t count)
764 struct device *dev = kobj_to_dev(kobj);
765 struct firmware_priv *fw_priv = to_firmware_priv(dev);
766 struct firmware_buf *buf;
767 ssize_t retval;
769 if (!capable(CAP_SYS_RAWIO))
770 return -EPERM;
772 mutex_lock(&fw_lock);
773 buf = fw_priv->buf;
774 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
775 retval = -ENODEV;
776 goto out;
779 retval = fw_realloc_buffer(fw_priv, offset + count);
780 if (retval)
781 goto out;
783 retval = count;
785 while (count) {
786 void *page_data;
787 int page_nr = offset >> PAGE_SHIFT;
788 int page_ofs = offset & (PAGE_SIZE - 1);
789 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
791 page_data = kmap(buf->pages[page_nr]);
793 memcpy(page_data + page_ofs, buffer, page_cnt);
795 kunmap(buf->pages[page_nr]);
796 buffer += page_cnt;
797 offset += page_cnt;
798 count -= page_cnt;
801 buf->size = max_t(size_t, offset, buf->size);
802 out:
803 mutex_unlock(&fw_lock);
804 return retval;
807 static struct bin_attribute firmware_attr_data = {
808 .attr = { .name = "data", .mode = 0644 },
809 .size = 0,
810 .read = firmware_data_read,
811 .write = firmware_data_write,
814 static void firmware_class_timeout_work(struct work_struct *work)
816 struct firmware_priv *fw_priv = container_of(work,
817 struct firmware_priv, timeout_work.work);
819 mutex_lock(&fw_lock);
820 fw_load_abort(fw_priv);
821 mutex_unlock(&fw_lock);
824 static struct firmware_priv *
825 fw_create_instance(struct firmware *firmware, const char *fw_name,
826 struct device *device, bool uevent, bool nowait)
828 struct firmware_priv *fw_priv;
829 struct device *f_dev;
831 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
832 if (!fw_priv) {
833 dev_err(device, "%s: kmalloc failed\n", __func__);
834 fw_priv = ERR_PTR(-ENOMEM);
835 goto exit;
838 fw_priv->nowait = nowait;
839 fw_priv->fw = firmware;
840 INIT_DELAYED_WORK(&fw_priv->timeout_work,
841 firmware_class_timeout_work);
843 f_dev = &fw_priv->dev;
845 device_initialize(f_dev);
846 dev_set_name(f_dev, "%s", fw_name);
847 f_dev->parent = device;
848 f_dev->class = &firmware_class;
849 exit:
850 return fw_priv;
853 /* load a firmware via user helper */
854 static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
855 long timeout)
857 int retval = 0;
858 struct device *f_dev = &fw_priv->dev;
859 struct firmware_buf *buf = fw_priv->buf;
861 /* fall back on userspace loading */
862 buf->is_paged_buf = true;
864 dev_set_uevent_suppress(f_dev, true);
866 retval = device_add(f_dev);
867 if (retval) {
868 dev_err(f_dev, "%s: device_register failed\n", __func__);
869 goto err_put_dev;
872 retval = device_create_bin_file(f_dev, &firmware_attr_data);
873 if (retval) {
874 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
875 goto err_del_dev;
878 mutex_lock(&fw_lock);
879 list_add(&buf->pending_list, &pending_fw_head);
880 mutex_unlock(&fw_lock);
882 retval = device_create_file(f_dev, &dev_attr_loading);
883 if (retval) {
884 mutex_lock(&fw_lock);
885 list_del_init(&buf->pending_list);
886 mutex_unlock(&fw_lock);
887 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
888 goto err_del_bin_attr;
891 if (uevent) {
892 buf->need_uevent = true;
893 dev_set_uevent_suppress(f_dev, false);
894 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
895 if (timeout != MAX_SCHEDULE_TIMEOUT)
896 schedule_delayed_work(&fw_priv->timeout_work, timeout);
898 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
901 wait_for_completion(&buf->completion);
903 cancel_delayed_work_sync(&fw_priv->timeout_work);
905 device_remove_file(f_dev, &dev_attr_loading);
906 err_del_bin_attr:
907 device_remove_bin_file(f_dev, &firmware_attr_data);
908 err_del_dev:
909 device_del(f_dev);
910 err_put_dev:
911 put_device(f_dev);
912 return retval;
915 static int fw_load_from_user_helper(struct firmware *firmware,
916 const char *name, struct device *device,
917 bool uevent, bool nowait, long timeout)
919 struct firmware_priv *fw_priv;
921 fw_priv = fw_create_instance(firmware, name, device, uevent, nowait);
922 if (IS_ERR(fw_priv))
923 return PTR_ERR(fw_priv);
925 fw_priv->buf = firmware->priv;
926 return _request_firmware_load(fw_priv, uevent, timeout);
929 #ifdef CONFIG_PM_SLEEP
930 /* kill pending requests without uevent to avoid blocking suspend */
931 static void kill_requests_without_uevent(void)
933 struct firmware_buf *buf;
934 struct firmware_buf *next;
936 mutex_lock(&fw_lock);
937 list_for_each_entry_safe(buf, next, &pending_fw_head, pending_list) {
938 if (!buf->need_uevent)
939 __fw_load_abort(buf);
941 mutex_unlock(&fw_lock);
943 #endif
945 #else /* CONFIG_FW_LOADER_USER_HELPER */
946 static inline int
947 fw_load_from_user_helper(struct firmware *firmware, const char *name,
948 struct device *device, bool uevent, bool nowait,
949 long timeout)
951 return -ENOENT;
954 /* No abort during direct loading */
955 #define is_fw_load_aborted(buf) false
957 #ifdef CONFIG_PM_SLEEP
958 static inline void kill_requests_without_uevent(void) { }
959 #endif
961 #endif /* CONFIG_FW_LOADER_USER_HELPER */
964 /* wait until the shared firmware_buf becomes ready (or error) */
965 static int sync_cached_firmware_buf(struct firmware_buf *buf)
967 int ret = 0;
969 mutex_lock(&fw_lock);
970 while (!test_bit(FW_STATUS_DONE, &buf->status)) {
971 if (is_fw_load_aborted(buf)) {
972 ret = -ENOENT;
973 break;
975 mutex_unlock(&fw_lock);
976 wait_for_completion(&buf->completion);
977 mutex_lock(&fw_lock);
979 mutex_unlock(&fw_lock);
980 return ret;
983 /* prepare firmware and firmware_buf structs;
984 * return 0 if a firmware is already assigned, 1 if need to load one,
985 * or a negative error code
987 static int
988 _request_firmware_prepare(struct firmware **firmware_p, const char *name,
989 struct device *device)
991 struct firmware *firmware;
992 struct firmware_buf *buf;
993 int ret;
995 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
996 if (!firmware) {
997 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
998 __func__);
999 return -ENOMEM;
1002 if (fw_get_builtin_firmware(firmware, name)) {
1003 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
1004 return 0; /* assigned */
1007 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
1010 * bind with 'buf' now to avoid warning in failure path
1011 * of requesting firmware.
1013 firmware->priv = buf;
1015 if (ret > 0) {
1016 ret = sync_cached_firmware_buf(buf);
1017 if (!ret) {
1018 fw_set_page_data(buf, firmware);
1019 return 0; /* assigned */
1023 if (ret < 0)
1024 return ret;
1025 return 1; /* need to load */
1028 static int assign_firmware_buf(struct firmware *fw, struct device *device,
1029 bool skip_cache)
1031 struct firmware_buf *buf = fw->priv;
1033 mutex_lock(&fw_lock);
1034 if (!buf->size || is_fw_load_aborted(buf)) {
1035 mutex_unlock(&fw_lock);
1036 return -ENOENT;
1040 * add firmware name into devres list so that we can auto cache
1041 * and uncache firmware for device.
1043 * device may has been deleted already, but the problem
1044 * should be fixed in devres or driver core.
1046 if (device && !skip_cache)
1047 fw_add_devm_name(device, buf->fw_id);
1050 * After caching firmware image is started, let it piggyback
1051 * on request firmware.
1053 if (buf->fwc->state == FW_LOADER_START_CACHE) {
1054 if (fw_cache_piggyback_on_request(buf->fw_id))
1055 kref_get(&buf->ref);
1058 /* pass the pages buffer to driver at the last minute */
1059 fw_set_page_data(buf, fw);
1060 mutex_unlock(&fw_lock);
1061 return 0;
1064 /* called from request_firmware() and request_firmware_work_func() */
1065 static int
1066 _request_firmware(const struct firmware **firmware_p, const char *name,
1067 struct device *device, bool uevent, bool nowait)
1069 struct firmware *fw;
1070 long timeout;
1071 int ret;
1073 if (!firmware_p)
1074 return -EINVAL;
1076 if (!name || name[0] == '\0')
1077 return -EINVAL;
1079 ret = _request_firmware_prepare(&fw, name, device);
1080 if (ret <= 0) /* error or already assigned */
1081 goto out;
1083 ret = 0;
1084 timeout = firmware_loading_timeout();
1085 if (nowait) {
1086 timeout = usermodehelper_read_lock_wait(timeout);
1087 if (!timeout) {
1088 dev_dbg(device, "firmware: %s loading timed out\n",
1089 name);
1090 ret = -EBUSY;
1091 goto out;
1093 } else {
1094 ret = usermodehelper_read_trylock();
1095 if (WARN_ON(ret)) {
1096 dev_err(device, "firmware: %s will not be loaded\n",
1097 name);
1098 goto out;
1102 if (!fw_get_filesystem_firmware(device, fw->priv))
1103 ret = fw_load_from_user_helper(fw, name, device,
1104 uevent, nowait, timeout);
1106 /* don't cache firmware handled without uevent */
1107 if (!ret)
1108 ret = assign_firmware_buf(fw, device, !uevent);
1110 usermodehelper_read_unlock();
1112 out:
1113 if (ret < 0) {
1114 release_firmware(fw);
1115 fw = NULL;
1118 *firmware_p = fw;
1119 return ret;
1123 * request_firmware: - send firmware request and wait for it
1124 * @firmware_p: pointer to firmware image
1125 * @name: name of firmware file
1126 * @device: device for which firmware is being loaded
1128 * @firmware_p will be used to return a firmware image by the name
1129 * of @name for device @device.
1131 * Should be called from user context where sleeping is allowed.
1133 * @name will be used as $FIRMWARE in the uevent environment and
1134 * should be distinctive enough not to be confused with any other
1135 * firmware image for this or any other device.
1137 * Caller must hold the reference count of @device.
1139 * The function can be called safely inside device's suspend and
1140 * resume callback.
1143 request_firmware(const struct firmware **firmware_p, const char *name,
1144 struct device *device)
1146 int ret;
1148 /* Need to pin this module until return */
1149 __module_get(THIS_MODULE);
1150 ret = _request_firmware(firmware_p, name, device, true, false);
1151 module_put(THIS_MODULE);
1152 return ret;
1154 EXPORT_SYMBOL(request_firmware);
1157 * release_firmware: - release the resource associated with a firmware image
1158 * @fw: firmware resource to release
1160 void release_firmware(const struct firmware *fw)
1162 if (fw) {
1163 if (!fw_is_builtin_firmware(fw))
1164 firmware_free_data(fw);
1165 kfree(fw);
1168 EXPORT_SYMBOL(release_firmware);
1170 /* Async support */
1171 struct firmware_work {
1172 struct work_struct work;
1173 struct module *module;
1174 const char *name;
1175 struct device *device;
1176 void *context;
1177 void (*cont)(const struct firmware *fw, void *context);
1178 bool uevent;
1181 static void request_firmware_work_func(struct work_struct *work)
1183 struct firmware_work *fw_work;
1184 const struct firmware *fw;
1186 fw_work = container_of(work, struct firmware_work, work);
1188 _request_firmware(&fw, fw_work->name, fw_work->device,
1189 fw_work->uevent, true);
1190 fw_work->cont(fw, fw_work->context);
1191 put_device(fw_work->device); /* taken in request_firmware_nowait() */
1193 module_put(fw_work->module);
1194 kfree(fw_work);
1198 * request_firmware_nowait - asynchronous version of request_firmware
1199 * @module: module requesting the firmware
1200 * @uevent: sends uevent to copy the firmware image if this flag
1201 * is non-zero else the firmware copy must be done manually.
1202 * @name: name of firmware file
1203 * @device: device for which firmware is being loaded
1204 * @gfp: allocation flags
1205 * @context: will be passed over to @cont, and
1206 * @fw may be %NULL if firmware request fails.
1207 * @cont: function will be called asynchronously when the firmware
1208 * request is over.
1210 * Caller must hold the reference count of @device.
1212 * Asynchronous variant of request_firmware() for user contexts:
1213 * - sleep for as small periods as possible since it may
1214 * increase kernel boot time of built-in device drivers
1215 * requesting firmware in their ->probe() methods, if
1216 * @gfp is GFP_KERNEL.
1218 * - can't sleep at all if @gfp is GFP_ATOMIC.
1221 request_firmware_nowait(
1222 struct module *module, bool uevent,
1223 const char *name, struct device *device, gfp_t gfp, void *context,
1224 void (*cont)(const struct firmware *fw, void *context))
1226 struct firmware_work *fw_work;
1228 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
1229 if (!fw_work)
1230 return -ENOMEM;
1232 fw_work->module = module;
1233 fw_work->name = name;
1234 fw_work->device = device;
1235 fw_work->context = context;
1236 fw_work->cont = cont;
1237 fw_work->uevent = uevent;
1239 if (!try_module_get(module)) {
1240 kfree(fw_work);
1241 return -EFAULT;
1244 get_device(fw_work->device);
1245 INIT_WORK(&fw_work->work, request_firmware_work_func);
1246 schedule_work(&fw_work->work);
1247 return 0;
1249 EXPORT_SYMBOL(request_firmware_nowait);
1251 #ifdef CONFIG_PM_SLEEP
1252 static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1255 * cache_firmware - cache one firmware image in kernel memory space
1256 * @fw_name: the firmware image name
1258 * Cache firmware in kernel memory so that drivers can use it when
1259 * system isn't ready for them to request firmware image from userspace.
1260 * Once it returns successfully, driver can use request_firmware or its
1261 * nowait version to get the cached firmware without any interacting
1262 * with userspace
1264 * Return 0 if the firmware image has been cached successfully
1265 * Return !0 otherwise
1268 static int cache_firmware(const char *fw_name)
1270 int ret;
1271 const struct firmware *fw;
1273 pr_debug("%s: %s\n", __func__, fw_name);
1275 ret = request_firmware(&fw, fw_name, NULL);
1276 if (!ret)
1277 kfree(fw);
1279 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1281 return ret;
1284 static struct firmware_buf *fw_lookup_buf(const char *fw_name)
1286 struct firmware_buf *tmp;
1287 struct firmware_cache *fwc = &fw_cache;
1289 spin_lock(&fwc->lock);
1290 tmp = __fw_lookup_buf(fw_name);
1291 spin_unlock(&fwc->lock);
1293 return tmp;
1297 * uncache_firmware - remove one cached firmware image
1298 * @fw_name: the firmware image name
1300 * Uncache one firmware image which has been cached successfully
1301 * before.
1303 * Return 0 if the firmware cache has been removed successfully
1304 * Return !0 otherwise
1307 static int uncache_firmware(const char *fw_name)
1309 struct firmware_buf *buf;
1310 struct firmware fw;
1312 pr_debug("%s: %s\n", __func__, fw_name);
1314 if (fw_get_builtin_firmware(&fw, fw_name))
1315 return 0;
1317 buf = fw_lookup_buf(fw_name);
1318 if (buf) {
1319 fw_free_buf(buf);
1320 return 0;
1323 return -EINVAL;
1326 static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1328 struct fw_cache_entry *fce;
1330 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1331 if (!fce)
1332 goto exit;
1334 strcpy(fce->name, name);
1335 exit:
1336 return fce;
1339 static int __fw_entry_found(const char *name)
1341 struct firmware_cache *fwc = &fw_cache;
1342 struct fw_cache_entry *fce;
1344 list_for_each_entry(fce, &fwc->fw_names, list) {
1345 if (!strcmp(fce->name, name))
1346 return 1;
1348 return 0;
1351 static int fw_cache_piggyback_on_request(const char *name)
1353 struct firmware_cache *fwc = &fw_cache;
1354 struct fw_cache_entry *fce;
1355 int ret = 0;
1357 spin_lock(&fwc->name_lock);
1358 if (__fw_entry_found(name))
1359 goto found;
1361 fce = alloc_fw_cache_entry(name);
1362 if (fce) {
1363 ret = 1;
1364 list_add(&fce->list, &fwc->fw_names);
1365 pr_debug("%s: fw: %s\n", __func__, name);
1367 found:
1368 spin_unlock(&fwc->name_lock);
1369 return ret;
1372 static void free_fw_cache_entry(struct fw_cache_entry *fce)
1374 kfree(fce);
1377 static void __async_dev_cache_fw_image(void *fw_entry,
1378 async_cookie_t cookie)
1380 struct fw_cache_entry *fce = fw_entry;
1381 struct firmware_cache *fwc = &fw_cache;
1382 int ret;
1384 ret = cache_firmware(fce->name);
1385 if (ret) {
1386 spin_lock(&fwc->name_lock);
1387 list_del(&fce->list);
1388 spin_unlock(&fwc->name_lock);
1390 free_fw_cache_entry(fce);
1394 /* called with dev->devres_lock held */
1395 static void dev_create_fw_entry(struct device *dev, void *res,
1396 void *data)
1398 struct fw_name_devm *fwn = res;
1399 const char *fw_name = fwn->name;
1400 struct list_head *head = data;
1401 struct fw_cache_entry *fce;
1403 fce = alloc_fw_cache_entry(fw_name);
1404 if (fce)
1405 list_add(&fce->list, head);
1408 static int devm_name_match(struct device *dev, void *res,
1409 void *match_data)
1411 struct fw_name_devm *fwn = res;
1412 return (fwn->magic == (unsigned long)match_data);
1415 static void dev_cache_fw_image(struct device *dev, void *data)
1417 LIST_HEAD(todo);
1418 struct fw_cache_entry *fce;
1419 struct fw_cache_entry *fce_next;
1420 struct firmware_cache *fwc = &fw_cache;
1422 devres_for_each_res(dev, fw_name_devm_release,
1423 devm_name_match, &fw_cache,
1424 dev_create_fw_entry, &todo);
1426 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1427 list_del(&fce->list);
1429 spin_lock(&fwc->name_lock);
1430 /* only one cache entry for one firmware */
1431 if (!__fw_entry_found(fce->name)) {
1432 list_add(&fce->list, &fwc->fw_names);
1433 } else {
1434 free_fw_cache_entry(fce);
1435 fce = NULL;
1437 spin_unlock(&fwc->name_lock);
1439 if (fce)
1440 async_schedule_domain(__async_dev_cache_fw_image,
1441 (void *)fce,
1442 &fw_cache_domain);
1446 static void __device_uncache_fw_images(void)
1448 struct firmware_cache *fwc = &fw_cache;
1449 struct fw_cache_entry *fce;
1451 spin_lock(&fwc->name_lock);
1452 while (!list_empty(&fwc->fw_names)) {
1453 fce = list_entry(fwc->fw_names.next,
1454 struct fw_cache_entry, list);
1455 list_del(&fce->list);
1456 spin_unlock(&fwc->name_lock);
1458 uncache_firmware(fce->name);
1459 free_fw_cache_entry(fce);
1461 spin_lock(&fwc->name_lock);
1463 spin_unlock(&fwc->name_lock);
1467 * device_cache_fw_images - cache devices' firmware
1469 * If one device called request_firmware or its nowait version
1470 * successfully before, the firmware names are recored into the
1471 * device's devres link list, so device_cache_fw_images can call
1472 * cache_firmware() to cache these firmwares for the device,
1473 * then the device driver can load its firmwares easily at
1474 * time when system is not ready to complete loading firmware.
1476 static void device_cache_fw_images(void)
1478 struct firmware_cache *fwc = &fw_cache;
1479 int old_timeout;
1480 DEFINE_WAIT(wait);
1482 pr_debug("%s\n", __func__);
1484 /* cancel uncache work */
1485 cancel_delayed_work_sync(&fwc->work);
1488 * use small loading timeout for caching devices' firmware
1489 * because all these firmware images have been loaded
1490 * successfully at lease once, also system is ready for
1491 * completing firmware loading now. The maximum size of
1492 * firmware in current distributions is about 2M bytes,
1493 * so 10 secs should be enough.
1495 old_timeout = loading_timeout;
1496 loading_timeout = 10;
1498 mutex_lock(&fw_lock);
1499 fwc->state = FW_LOADER_START_CACHE;
1500 dpm_for_each_dev(NULL, dev_cache_fw_image);
1501 mutex_unlock(&fw_lock);
1503 /* wait for completion of caching firmware for all devices */
1504 async_synchronize_full_domain(&fw_cache_domain);
1506 loading_timeout = old_timeout;
1510 * device_uncache_fw_images - uncache devices' firmware
1512 * uncache all firmwares which have been cached successfully
1513 * by device_uncache_fw_images earlier
1515 static void device_uncache_fw_images(void)
1517 pr_debug("%s\n", __func__);
1518 __device_uncache_fw_images();
1521 static void device_uncache_fw_images_work(struct work_struct *work)
1523 device_uncache_fw_images();
1527 * device_uncache_fw_images_delay - uncache devices firmwares
1528 * @delay: number of milliseconds to delay uncache device firmwares
1530 * uncache all devices's firmwares which has been cached successfully
1531 * by device_cache_fw_images after @delay milliseconds.
1533 static void device_uncache_fw_images_delay(unsigned long delay)
1535 schedule_delayed_work(&fw_cache.work,
1536 msecs_to_jiffies(delay));
1539 static int fw_pm_notify(struct notifier_block *notify_block,
1540 unsigned long mode, void *unused)
1542 switch (mode) {
1543 case PM_HIBERNATION_PREPARE:
1544 case PM_SUSPEND_PREPARE:
1545 case PM_RESTORE_PREPARE:
1546 kill_requests_without_uevent();
1547 device_cache_fw_images();
1548 break;
1550 case PM_POST_SUSPEND:
1551 case PM_POST_HIBERNATION:
1552 case PM_POST_RESTORE:
1554 * In case that system sleep failed and syscore_suspend is
1555 * not called.
1557 mutex_lock(&fw_lock);
1558 fw_cache.state = FW_LOADER_NO_CACHE;
1559 mutex_unlock(&fw_lock);
1561 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1562 break;
1565 return 0;
1568 /* stop caching firmware once syscore_suspend is reached */
1569 static int fw_suspend(void)
1571 fw_cache.state = FW_LOADER_NO_CACHE;
1572 return 0;
1575 static struct syscore_ops fw_syscore_ops = {
1576 .suspend = fw_suspend,
1578 #else
1579 static int fw_cache_piggyback_on_request(const char *name)
1581 return 0;
1583 #endif
1585 static void __init fw_cache_init(void)
1587 spin_lock_init(&fw_cache.lock);
1588 INIT_LIST_HEAD(&fw_cache.head);
1589 fw_cache.state = FW_LOADER_NO_CACHE;
1591 #ifdef CONFIG_PM_SLEEP
1592 spin_lock_init(&fw_cache.name_lock);
1593 INIT_LIST_HEAD(&fw_cache.fw_names);
1595 INIT_DELAYED_WORK(&fw_cache.work,
1596 device_uncache_fw_images_work);
1598 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1599 register_pm_notifier(&fw_cache.pm_notify);
1601 register_syscore_ops(&fw_syscore_ops);
1602 #endif
1605 static int __init firmware_class_init(void)
1607 fw_cache_init();
1608 #ifdef CONFIG_FW_LOADER_USER_HELPER
1609 register_reboot_notifier(&fw_shutdown_nb);
1610 return class_register(&firmware_class);
1611 #else
1612 return 0;
1613 #endif
1616 static void __exit firmware_class_exit(void)
1618 #ifdef CONFIG_PM_SLEEP
1619 unregister_syscore_ops(&fw_syscore_ops);
1620 unregister_pm_notifier(&fw_cache.pm_notify);
1621 #endif
1622 #ifdef CONFIG_FW_LOADER_USER_HELPER
1623 unregister_reboot_notifier(&fw_shutdown_nb);
1624 class_unregister(&firmware_class);
1625 #endif
1628 fs_initcall(firmware_class_init);
1629 module_exit(firmware_class_exit);