10 #include <linux/capability.h>
11 #include <linux/device.h>
12 #include <linux/module.h>
17 #include <linux/bitops.h>
22 #include <linux/slab.h>
23 #include <linux/sched.h>
25 #include <linux/list.h>
31 #include <generated/utsrelease.h>
41 #ifdef CONFIG_FW_LOADER
46 static bool fw_get_builtin_firmware(
struct firmware *
fw,
const char *
name)
50 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
61 static bool fw_is_builtin_firmware(
const struct firmware *fw)
65 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
74 static inline bool fw_get_builtin_firmware(
struct firmware *fw,
const char *name)
79 static inline bool fw_is_builtin_firmware(
const struct firmware *fw)
96 static int loading_timeout = 60;
98 static inline long firmware_loading_timeout(
void)
109 #ifdef CONFIG_PM_SLEEP
158 #define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
160 #define FW_LOADER_NO_CACHE 0
161 #define FW_LOADER_START_CACHE 1
163 static int fw_cache_piggyback_on_request(
const char *name);
181 kref_init(&buf->
ref);
187 pr_debug(
"%s: fw-%s buf=%p\n", __func__, fw_name, buf);
192 static struct firmware_buf *__fw_lookup_buf(
const char *fw_name)
203 static
int fw_lookup_and_allocate_buf(
const char *fw_name,
209 spin_lock(&fwc->lock);
210 tmp = __fw_lookup_buf(fw_name);
213 spin_unlock(&fwc->lock);
217 tmp = __allocate_fw_buf(fw_name, fwc);
219 list_add(&tmp->
list, &fwc->head);
220 spin_unlock(&fwc->lock);
227 static struct firmware_buf *fw_lookup_buf(
const char *fw_name)
232 spin_lock(&fwc->
lock);
233 tmp = __fw_lookup_buf(fw_name);
234 spin_unlock(&fwc->
lock);
239 static void __fw_free_buf(
struct kref *ref)
245 pr_debug(
"%s: fw-%s buf=%p data=%p size=%u\n",
247 (
unsigned int)buf->
size);
249 spin_lock(&fwc->
lock);
251 spin_unlock(&fwc->
lock);
266 kref_put(&buf->
ref, __fw_free_buf);
270 static const char *fw_path[] = {
271 "/lib/firmware/updates/" UTS_RELEASE,
272 "/lib/firmware/updates",
273 "/lib/firmware/" UTS_RELEASE,
285 if (
st.size != (
long)
st.size)
290 static bool fw_read_file_contents(
struct file *file,
struct firmware_buf *fw_buf)
295 size = fw_file_size(file);
310 static bool fw_get_filesystem_firmware(
struct firmware_buf *buf)
313 bool success =
false;
323 success = fw_read_file_contents(file, buf);
345 static ssize_t firmware_timeout_show(
struct class *
class,
349 return sprintf(buf,
"%d\n", loading_timeout);
365 static ssize_t firmware_timeout_store(
struct class *
class,
367 const char *buf,
size_t count)
370 if (loading_timeout < 0)
378 firmware_timeout_show, firmware_timeout_store),
382 static void fw_dev_release(
struct device *
dev)
405 static struct class firmware_class = {
407 .class_attrs = firmware_class_attrs,
408 .dev_uevent = firmware_uevent,
409 .dev_release = fw_dev_release,
418 return sprintf(buf,
"%d\n", loading);
422 static void firmware_free_data(
const struct firmware *fw)
429 fw_free_buf(fw->
priv);
433 #ifndef PAGE_KERNEL_RO
434 #define PAGE_KERNEL_RO PAGE_KERNEL
466 const char *buf,
size_t count)
482 for (i = 0; i < fw_buf->
nr_pages; i++)
502 fw_map_pages_buf(fw_buf);
508 dev_err(dev,
"%s: unexpected value (%d)\n", __func__, loading);
511 fw_load_abort(fw_priv);
519 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
521 static ssize_t firmware_data_read(
struct file *filp,
struct kobject *kobj,
525 struct device *dev = kobj_to_dev(kobj);
536 if (offset > buf->
size) {
540 if (count > buf->
size - offset)
553 memcpy(buffer, page_data + page_ofs, page_cnt);
565 static int fw_realloc_buffer(
struct firmware_priv *fw_priv,
int min_size)
572 int new_array_size =
max(pages_needed,
574 struct page **new_pages;
576 new_pages =
kmalloc(new_array_size *
sizeof(
void *),
579 fw_load_abort(fw_priv);
587 buf->
pages = new_pages;
591 while (buf->
nr_pages < pages_needed) {
596 fw_load_abort(fw_priv);
616 static ssize_t firmware_data_write(
struct file *filp,
struct kobject *kobj,
618 char *buffer, loff_t offset,
size_t count)
620 struct device *dev = kobj_to_dev(kobj);
635 retval = fw_realloc_buffer(fw_priv, offset + count);
649 memcpy(page_data + page_ofs, buffer, page_cnt);
664 .attr = { .name =
"data", .mode = 0644 },
666 .read = firmware_data_read,
667 .write = firmware_data_write,
670 static void firmware_class_timeout(
u_long data)
674 fw_load_abort(fw_priv);
682 struct device *f_dev;
684 fw_priv = kzalloc(
sizeof(*fw_priv),
GFP_KERNEL);
686 dev_err(device,
"%s: kmalloc failed\n", __func__);
687 fw_priv = ERR_PTR(-
ENOMEM);
692 fw_priv->
fw = firmware;
694 firmware_class_timeout, (
u_long) fw_priv);
696 f_dev = &fw_priv->
dev;
701 f_dev->
class = &firmware_class;
707 static void fw_set_page_data(
struct firmware_buf *buf,
struct firmware *fw)
714 pr_debug(
"%s: fw-%s buf=%p data=%p size=%u\n",
716 (
unsigned int)buf->
size);
719 #ifdef CONFIG_PM_SLEEP
720 static void fw_name_devm_release(
struct device *dev,
void *
res)
724 if (fwn->
magic == (
unsigned long)&fw_cache)
725 pr_debug(
"%s: fw_name-%s devm-%p released\n",
726 __func__, fwn->
name, res);
729 static int fw_devm_match(
struct device *dev,
void *res,
734 return (fwn->
magic == (
unsigned long)&fw_cache) &&
738 static struct fw_name_devm *fw_find_devm_name(
struct device *dev,
744 fw_devm_match, (
void *)name);
749 static int fw_add_devm_name(
struct device *dev,
const char *name)
753 fwn = fw_find_devm_name(dev, name);
769 static int fw_add_devm_name(
struct device *dev,
const char *name)
775 static void _request_firmware_cleanup(
const struct firmware **firmware_p)
782 _request_firmware_prepare(
const struct firmware **firmware_p,
const char *name,
783 struct device *device,
bool uevent,
bool nowait)
785 struct firmware *firmware;
793 *firmware_p = firmware = kzalloc(
sizeof(*firmware),
GFP_KERNEL);
795 dev_err(device,
"%s: kmalloc(struct firmware) failed\n",
800 if (fw_get_builtin_firmware(firmware, name)) {
801 dev_dbg(device,
"firmware: using built-in firmware %s\n", name);
805 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
807 fw_priv = fw_create_instance(firmware, name, device,
810 if (IS_ERR(fw_priv) || ret < 0) {
814 }
else if (fw_priv) {
829 fw_priv = ERR_PTR(-
ENOENT);
831 _request_firmware_cleanup(firmware_p);
835 fw_set_page_data(buf, firmware);
847 static int _request_firmware_load(
struct firmware_priv *fw_priv,
bool uevent,
851 struct device *f_dev = &fw_priv->
dev;
857 if (fw_get_filesystem_firmware(buf)) {
859 " firmware %s\n", buf->
fw_id);
870 dev_set_uevent_suppress(f_dev,
true);
877 dev_err(f_dev,
"%s: device_register failed\n", __func__);
883 dev_err(f_dev,
"%s: sysfs_create_bin_file failed\n", __func__);
889 dev_err(f_dev,
"%s: device_create_file failed\n", __func__);
890 goto err_del_bin_attr;
894 dev_set_uevent_suppress(f_dev,
false);
895 dev_dbg(f_dev,
"firmware: requesting %s\n", buf->
fw_id);
919 if (!retval && f_dev->
parent)
927 if (fw_cache_piggyback_on_request(buf->
fw_id))
932 fw_set_page_data(buf, fw_priv->
fw);
969 struct device *device)
974 fw_priv = _request_firmware_prepare(firmware_p, name, device,
true,
976 if (IS_ERR_OR_NULL(fw_priv))
977 return PTR_RET(fw_priv);
981 dev_err(device,
"firmware: %s will not be loaded\n", name);
983 ret = _request_firmware_load(fw_priv,
true,
984 firmware_loading_timeout());
988 _request_firmware_cleanup(firmware_p);
1000 if (!fw_is_builtin_firmware(fw))
1001 firmware_free_data(fw);
1020 const struct firmware *
fw;
1026 fw_priv = _request_firmware_prepare(&fw, fw_work->
name, fw_work->
device,
1028 if (IS_ERR_OR_NULL(fw_priv)) {
1029 ret = PTR_RET(fw_priv);
1035 ret = _request_firmware_load(fw_priv, fw_work->
uevent, timeout);
1038 dev_dbg(fw_work->
device,
"firmware: %s loading timed out\n",
1043 _request_firmware_cleanup(&fw);
1049 module_put(fw_work->
module);
1079 const char *name,
struct device *device,
gfp_t gfp,
void *
context,
1080 void (*
cont)(
const struct firmware *fw,
void *context))
1095 if (!try_module_get(module)) {
1123 const struct firmware *
fw;
1125 pr_debug(
"%s: %s\n", __func__, fw_name);
1131 pr_debug(
"%s: %s ret=%d\n", __func__, fw_name, ret);
1152 pr_debug(
"%s: %s\n", __func__, fw_name);
1154 if (fw_get_builtin_firmware(&fw, fw_name))
1157 buf = fw_lookup_buf(fw_name);
1166 #ifdef CONFIG_PM_SLEEP
1169 static struct fw_cache_entry *alloc_fw_cache_entry(
const char *name)
1182 static int __fw_entry_found(
const char *name)
1194 static int fw_cache_piggyback_on_request(
const char *name)
1200 spin_lock(&fwc->name_lock);
1201 if (__fw_entry_found(name))
1204 fce = alloc_fw_cache_entry(name);
1207 list_add(&fce->
list, &fwc->fw_names);
1208 pr_debug(
"%s: fw: %s\n", __func__, name);
1211 spin_unlock(&fwc->name_lock);
1220 static void __async_dev_cache_fw_image(
void *fw_entry,
1229 spin_lock(&fwc->name_lock);
1231 spin_unlock(&fwc->name_lock);
1233 free_fw_cache_entry(fce);
1238 static void dev_create_fw_entry(
struct device *dev,
void *res,
1242 const char *fw_name = fwn->
name;
1246 fce = alloc_fw_cache_entry(fw_name);
1248 list_add(&fce->
list, head);
1251 static int devm_name_match(
struct device *dev,
void *res,
1255 return (fwn->
magic == (
unsigned long)match_data);
1258 static void dev_cache_fw_image(
struct device *dev,
void *data)
1266 devm_name_match, &fw_cache,
1267 dev_create_fw_entry, &
todo);
1272 spin_lock(&fwc->name_lock);
1274 if (!__fw_entry_found(fce->
name)) {
1275 list_add(&fce->
list, &fwc->fw_names);
1277 free_fw_cache_entry(fce);
1280 spin_unlock(&fwc->name_lock);
1289 static void __device_uncache_fw_images(
void)
1294 spin_lock(&fwc->name_lock);
1295 while (!list_empty(&fwc->fw_names)) {
1299 spin_unlock(&fwc->name_lock);
1302 free_fw_cache_entry(fce);
1304 spin_lock(&fwc->name_lock);
1306 spin_unlock(&fwc->name_lock);
1319 static void device_cache_fw_images(
void)
1338 old_timeout = loading_timeout;
1339 loading_timeout = 10;
1349 loading_timeout = old_timeout;
1358 static void device_uncache_fw_images(
void)
1361 __device_uncache_fw_images();
1364 static void device_uncache_fw_images_work(
struct work_struct *work)
1366 device_uncache_fw_images();
1376 static void device_uncache_fw_images_delay(
unsigned long delay)
1388 device_cache_fw_images();
1410 static int fw_suspend(
void)
1420 static int fw_cache_piggyback_on_request(
const char *name)
1426 static void __init fw_cache_init(
void)
1429 INIT_LIST_HEAD(&fw_cache.head);
1432 #ifdef CONFIG_PM_SLEEP
1434 INIT_LIST_HEAD(&fw_cache.fw_names);
1437 device_uncache_fw_images_work);
1439 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1440 register_pm_notifier(&fw_cache.pm_notify);
1446 static int __init firmware_class_init(
void)
1452 static void __exit firmware_class_exit(
void)
1454 #ifdef CONFIG_PM_SLEEP
1456 unregister_pm_notifier(&fw_cache.pm_notify);