Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * firmware_class.c - Multi purpose firmware loading support |
| 3 | * |
Markus Rechberger | 87d37a4 | 2007-06-04 18:45:44 +0200 | [diff] [blame] | 4 | * Copyright (c) 2003 Manuel Estrada Sainz |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * Please see Documentation/firmware_class/ for more information. |
| 7 | * |
| 8 | */ |
| 9 | |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 10 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 18 | #include <linux/mutex.h> |
Stephen Boyd | a36cf84 | 2012-03-28 23:31:00 +0200 | [diff] [blame] | 19 | #include <linux/workqueue.h> |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 20 | #include <linux/highmem.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/firmware.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Stephen Boyd | a36cf84 | 2012-03-28 23:31:00 +0200 | [diff] [blame] | 23 | #include <linux/sched.h> |
Linus Torvalds | abb139e | 2012-10-03 15:58:32 -0700 | [diff] [blame] | 24 | #include <linux/file.h> |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 25 | #include <linux/list.h> |
Mimi Zohar | e40ba6d | 2015-11-19 12:39:22 -0500 | [diff] [blame] | 26 | #include <linux/fs.h> |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 27 | #include <linux/async.h> |
| 28 | #include <linux/pm.h> |
Ming Lei | 07646d9 | 2012-08-04 12:01:29 +0800 | [diff] [blame] | 29 | #include <linux/suspend.h> |
Ming Lei | ac39b3e | 2012-08-20 19:04:16 +0800 | [diff] [blame] | 30 | #include <linux/syscore_ops.h> |
Takashi Iwai | fe30414 | 2013-05-22 18:28:38 +0200 | [diff] [blame] | 31 | #include <linux/reboot.h> |
Kees Cook | 6593d92 | 2014-02-25 13:06:00 -0800 | [diff] [blame] | 32 | #include <linux/security.h> |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 33 | |
Linus Torvalds | abb139e | 2012-10-03 15:58:32 -0700 | [diff] [blame] | 34 | #include <generated/utsrelease.h> |
| 35 | |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 36 | #include "base.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Markus Rechberger | 87d37a4 | 2007-06-04 18:45:44 +0200 | [diff] [blame] | 38 | MODULE_AUTHOR("Manuel Estrada Sainz"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | MODULE_DESCRIPTION("Multi purpose firmware loading support"); |
| 40 | MODULE_LICENSE("GPL"); |
| 41 | |
Dmitry Torokhov | bcb9bd1 | 2010-03-13 23:49:18 -0800 | [diff] [blame] | 42 | /* Builtin firmware support */ |
| 43 | |
| 44 | #ifdef CONFIG_FW_LOADER |
| 45 | |
| 46 | extern struct builtin_fw __start_builtin_fw[]; |
| 47 | extern struct builtin_fw __end_builtin_fw[]; |
| 48 | |
Stephen Boyd | a098ecd | 2016-08-02 14:04:28 -0700 | [diff] [blame] | 49 | static bool fw_get_builtin_firmware(struct firmware *fw, const char *name, |
| 50 | void *buf, size_t size) |
Dmitry Torokhov | bcb9bd1 | 2010-03-13 23:49:18 -0800 | [diff] [blame] | 51 | { |
| 52 | struct builtin_fw *b_fw; |
| 53 | |
| 54 | for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) { |
| 55 | if (strcmp(name, b_fw->name) == 0) { |
| 56 | fw->size = b_fw->size; |
| 57 | fw->data = b_fw->data; |
Stephen Boyd | a098ecd | 2016-08-02 14:04:28 -0700 | [diff] [blame] | 58 | |
| 59 | if (buf && fw->size <= size) |
| 60 | memcpy(buf, fw->data, fw->size); |
Dmitry Torokhov | bcb9bd1 | 2010-03-13 23:49:18 -0800 | [diff] [blame] | 61 | return true; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | static bool fw_is_builtin_firmware(const struct firmware *fw) |
| 69 | { |
| 70 | struct builtin_fw *b_fw; |
| 71 | |
| 72 | for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) |
| 73 | if (fw->data == b_fw->data) |
| 74 | return true; |
| 75 | |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | #else /* Module case - no builtin firmware support */ |
| 80 | |
Stephen Boyd | a098ecd | 2016-08-02 14:04:28 -0700 | [diff] [blame] | 81 | static inline bool fw_get_builtin_firmware(struct firmware *fw, |
| 82 | const char *name, void *buf, |
| 83 | size_t size) |
Dmitry Torokhov | bcb9bd1 | 2010-03-13 23:49:18 -0800 | [diff] [blame] | 84 | { |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | static inline bool fw_is_builtin_firmware(const struct firmware *fw) |
| 89 | { |
| 90 | return false; |
| 91 | } |
| 92 | #endif |
| 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | enum { |
| 95 | FW_STATUS_LOADING, |
| 96 | FW_STATUS_DONE, |
| 97 | FW_STATUS_ABORT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
Dave Jones | 2f65168 | 2007-01-25 15:56:15 -0500 | [diff] [blame] | 100 | static int loading_timeout = 60; /* In seconds */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
Rafael J. Wysocki | 9b78c1d | 2012-03-28 23:30:02 +0200 | [diff] [blame] | 102 | static inline long firmware_loading_timeout(void) |
| 103 | { |
Ming Lei | 68ff2a0 | 2015-01-13 00:02:01 +0800 | [diff] [blame] | 104 | return loading_timeout > 0 ? loading_timeout * HZ : MAX_JIFFY_OFFSET; |
Rafael J. Wysocki | 9b78c1d | 2012-03-28 23:30:02 +0200 | [diff] [blame] | 105 | } |
| 106 | |
Takashi Iwai | 14c4bae | 2013-12-02 15:38:18 +0100 | [diff] [blame] | 107 | /* firmware behavior options */ |
| 108 | #define FW_OPT_UEVENT (1U << 0) |
| 109 | #define FW_OPT_NOWAIT (1U << 1) |
Takashi Iwai | 68aeeaa | 2013-12-02 15:38:19 +0100 | [diff] [blame] | 110 | #ifdef CONFIG_FW_LOADER_USER_HELPER |
Takashi Iwai | 5a1379e | 2014-06-04 17:48:15 +0200 | [diff] [blame] | 111 | #define FW_OPT_USERHELPER (1U << 2) |
Takashi Iwai | 68aeeaa | 2013-12-02 15:38:19 +0100 | [diff] [blame] | 112 | #else |
Takashi Iwai | 5a1379e | 2014-06-04 17:48:15 +0200 | [diff] [blame] | 113 | #define FW_OPT_USERHELPER 0 |
| 114 | #endif |
| 115 | #ifdef CONFIG_FW_LOADER_USER_HELPER_FALLBACK |
| 116 | #define FW_OPT_FALLBACK FW_OPT_USERHELPER |
| 117 | #else |
| 118 | #define FW_OPT_FALLBACK 0 |
Takashi Iwai | 68aeeaa | 2013-12-02 15:38:19 +0100 | [diff] [blame] | 119 | #endif |
Luis R. Rodriguez | c868edf | 2014-07-02 09:55:05 -0700 | [diff] [blame] | 120 | #define FW_OPT_NO_WARN (1U << 3) |
Vikram Mulukutla | 0e742e9 | 2016-08-02 14:04:25 -0700 | [diff] [blame] | 121 | #define FW_OPT_NOCACHE (1U << 4) |
Takashi Iwai | 14c4bae | 2013-12-02 15:38:18 +0100 | [diff] [blame] | 122 | |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 123 | struct firmware_cache { |
| 124 | /* firmware_buf instance will be added into the below list */ |
| 125 | spinlock_t lock; |
| 126 | struct list_head head; |
Ming Lei | cfe016b | 2012-09-08 17:32:30 +0800 | [diff] [blame] | 127 | int state; |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 128 | |
Ming Lei | cfe016b | 2012-09-08 17:32:30 +0800 | [diff] [blame] | 129 | #ifdef CONFIG_PM_SLEEP |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 130 | /* |
| 131 | * Names of firmware images which have been cached successfully |
| 132 | * will be added into the below list so that device uncache |
| 133 | * helper can trace which firmware images have been cached |
| 134 | * before. |
| 135 | */ |
| 136 | spinlock_t name_lock; |
| 137 | struct list_head fw_names; |
| 138 | |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 139 | struct delayed_work work; |
Ming Lei | 07646d9 | 2012-08-04 12:01:29 +0800 | [diff] [blame] | 140 | |
| 141 | struct notifier_block pm_notify; |
Ming Lei | cfe016b | 2012-09-08 17:32:30 +0800 | [diff] [blame] | 142 | #endif |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 143 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 145 | struct firmware_buf { |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 146 | struct kref ref; |
| 147 | struct list_head list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | struct completion completion; |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 149 | struct firmware_cache *fwc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | unsigned long status; |
Ming Lei | 65710cb | 2012-08-04 12:01:16 +0800 | [diff] [blame] | 151 | void *data; |
| 152 | size_t size; |
Stephen Boyd | a098ecd | 2016-08-02 14:04:28 -0700 | [diff] [blame] | 153 | size_t allocated_size; |
Takashi Iwai | 7b1269f | 2013-01-31 11:13:55 +0100 | [diff] [blame] | 154 | #ifdef CONFIG_FW_LOADER_USER_HELPER |
Takashi Iwai | cd7239f | 2013-01-31 11:13:56 +0100 | [diff] [blame] | 155 | bool is_paged_buf; |
Ming Lei | af5bc11 | 2013-05-27 18:30:04 +0800 | [diff] [blame] | 156 | bool need_uevent; |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 157 | struct page **pages; |
| 158 | int nr_pages; |
| 159 | int page_array_size; |
Takashi Iwai | fe30414 | 2013-05-22 18:28:38 +0200 | [diff] [blame] | 160 | struct list_head pending_list; |
Takashi Iwai | 7b1269f | 2013-01-31 11:13:55 +0100 | [diff] [blame] | 161 | #endif |
Luis R. Rodriguez | e0fd9b1 | 2015-05-12 14:49:43 -0700 | [diff] [blame] | 162 | const char *fw_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | }; |
| 164 | |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 165 | struct fw_cache_entry { |
| 166 | struct list_head list; |
Luis R. Rodriguez | e0fd9b1 | 2015-05-12 14:49:43 -0700 | [diff] [blame] | 167 | const char *name; |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 168 | }; |
| 169 | |
Ming Lei | f531f05a | 2012-08-04 12:01:25 +0800 | [diff] [blame] | 170 | struct fw_name_devm { |
| 171 | unsigned long magic; |
Luis R. Rodriguez | e0fd9b1 | 2015-05-12 14:49:43 -0700 | [diff] [blame] | 172 | const char *name; |
Ming Lei | f531f05a | 2012-08-04 12:01:25 +0800 | [diff] [blame] | 173 | }; |
| 174 | |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 175 | #define to_fwbuf(d) container_of(d, struct firmware_buf, ref) |
| 176 | |
Ming Lei | ac39b3e | 2012-08-20 19:04:16 +0800 | [diff] [blame] | 177 | #define FW_LOADER_NO_CACHE 0 |
| 178 | #define FW_LOADER_START_CACHE 1 |
| 179 | |
| 180 | static int fw_cache_piggyback_on_request(const char *name); |
| 181 | |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 182 | /* fw_lock could be moved to 'struct firmware_priv' but since it is just |
| 183 | * guarding for corner cases a global lock should be OK */ |
| 184 | static DEFINE_MUTEX(fw_lock); |
| 185 | |
| 186 | static struct firmware_cache fw_cache; |
| 187 | |
| 188 | static struct firmware_buf *__allocate_fw_buf(const char *fw_name, |
Stephen Boyd | a098ecd | 2016-08-02 14:04:28 -0700 | [diff] [blame] | 189 | struct firmware_cache *fwc, |
| 190 | void *dbuf, size_t size) |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 191 | { |
| 192 | struct firmware_buf *buf; |
| 193 | |
Luis R. Rodriguez | e0fd9b1 | 2015-05-12 14:49:43 -0700 | [diff] [blame] | 194 | buf = kzalloc(sizeof(*buf), GFP_ATOMIC); |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 195 | if (!buf) |
Luis R. Rodriguez | e0fd9b1 | 2015-05-12 14:49:43 -0700 | [diff] [blame] | 196 | return NULL; |
| 197 | |
| 198 | buf->fw_id = kstrdup_const(fw_name, GFP_ATOMIC); |
| 199 | if (!buf->fw_id) { |
| 200 | kfree(buf); |
| 201 | return NULL; |
| 202 | } |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 203 | |
| 204 | kref_init(&buf->ref); |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 205 | buf->fwc = fwc; |
Stephen Boyd | a098ecd | 2016-08-02 14:04:28 -0700 | [diff] [blame] | 206 | buf->data = dbuf; |
| 207 | buf->allocated_size = size; |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 208 | init_completion(&buf->completion); |
Kyle Yan | c5336ef | 2017-05-24 17:18:15 -0700 | [diff] [blame] | 209 | INIT_LIST_HEAD(&buf->list); |
Takashi Iwai | fe30414 | 2013-05-22 18:28:38 +0200 | [diff] [blame] | 210 | #ifdef CONFIG_FW_LOADER_USER_HELPER |
| 211 | INIT_LIST_HEAD(&buf->pending_list); |
| 212 | #endif |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 213 | |
| 214 | pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf); |
| 215 | |
| 216 | return buf; |
| 217 | } |
| 218 | |
Ming Lei | 2887b39 | 2012-08-04 12:01:22 +0800 | [diff] [blame] | 219 | static struct firmware_buf *__fw_lookup_buf(const char *fw_name) |
| 220 | { |
| 221 | struct firmware_buf *tmp; |
| 222 | struct firmware_cache *fwc = &fw_cache; |
| 223 | |
| 224 | list_for_each_entry(tmp, &fwc->head, list) |
| 225 | if (!strcmp(tmp->fw_id, fw_name)) |
| 226 | return tmp; |
| 227 | return NULL; |
| 228 | } |
| 229 | |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 230 | static int fw_lookup_and_allocate_buf(const char *fw_name, |
| 231 | struct firmware_cache *fwc, |
Stephen Boyd | a098ecd | 2016-08-02 14:04:28 -0700 | [diff] [blame] | 232 | struct firmware_buf **buf, void *dbuf, |
Vikram Mulukutla | b19be88 | 2017-04-24 19:31:14 -0700 | [diff] [blame] | 233 | size_t size, unsigned int opt_flags) |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 234 | { |
| 235 | struct firmware_buf *tmp; |
| 236 | |
| 237 | spin_lock(&fwc->lock); |
Vikram Mulukutla | b19be88 | 2017-04-24 19:31:14 -0700 | [diff] [blame] | 238 | if (!(opt_flags & FW_OPT_NOCACHE)) { |
| 239 | tmp = __fw_lookup_buf(fw_name); |
| 240 | if (tmp) { |
| 241 | kref_get(&tmp->ref); |
| 242 | spin_unlock(&fwc->lock); |
| 243 | *buf = tmp; |
| 244 | return 1; |
| 245 | } |
Ming Lei | 2887b39 | 2012-08-04 12:01:22 +0800 | [diff] [blame] | 246 | } |
Stephen Boyd | a098ecd | 2016-08-02 14:04:28 -0700 | [diff] [blame] | 247 | tmp = __allocate_fw_buf(fw_name, fwc, dbuf, size); |
Vikram Mulukutla | b19be88 | 2017-04-24 19:31:14 -0700 | [diff] [blame] | 248 | if (tmp && !(opt_flags & FW_OPT_NOCACHE)) |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 249 | list_add(&tmp->list, &fwc->head); |
| 250 | spin_unlock(&fwc->lock); |
| 251 | |
| 252 | *buf = tmp; |
| 253 | |
| 254 | return tmp ? 0 : -ENOMEM; |
| 255 | } |
| 256 | |
| 257 | static void __fw_free_buf(struct kref *ref) |
Bart Van Assche | 98233b2 | 2014-01-04 14:20:36 +0100 | [diff] [blame] | 258 | __releases(&fwc->lock) |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 259 | { |
| 260 | struct firmware_buf *buf = to_fwbuf(ref); |
| 261 | struct firmware_cache *fwc = buf->fwc; |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 262 | |
| 263 | pr_debug("%s: fw-%s buf=%p data=%p size=%u\n", |
| 264 | __func__, buf->fw_id, buf, buf->data, |
| 265 | (unsigned int)buf->size); |
| 266 | |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 267 | list_del(&buf->list); |
| 268 | spin_unlock(&fwc->lock); |
| 269 | |
Takashi Iwai | 7b1269f | 2013-01-31 11:13:55 +0100 | [diff] [blame] | 270 | #ifdef CONFIG_FW_LOADER_USER_HELPER |
Takashi Iwai | cd7239f | 2013-01-31 11:13:56 +0100 | [diff] [blame] | 271 | if (buf->is_paged_buf) { |
Takashi Iwai | 7b1269f | 2013-01-31 11:13:55 +0100 | [diff] [blame] | 272 | int i; |
Ming Lei | 746058f | 2012-10-09 12:01:03 +0800 | [diff] [blame] | 273 | vunmap(buf->data); |
| 274 | for (i = 0; i < buf->nr_pages; i++) |
| 275 | __free_page(buf->pages[i]); |
Chen Feng | 10a3fbf | 2015-12-16 17:03:15 +0800 | [diff] [blame] | 276 | vfree(buf->pages); |
Ming Lei | 746058f | 2012-10-09 12:01:03 +0800 | [diff] [blame] | 277 | } else |
Takashi Iwai | 7b1269f | 2013-01-31 11:13:55 +0100 | [diff] [blame] | 278 | #endif |
Stephen Boyd | a098ecd | 2016-08-02 14:04:28 -0700 | [diff] [blame] | 279 | if (!buf->allocated_size) |
Ming Lei | 746058f | 2012-10-09 12:01:03 +0800 | [diff] [blame] | 280 | vfree(buf->data); |
Luis R. Rodriguez | e0fd9b1 | 2015-05-12 14:49:43 -0700 | [diff] [blame] | 281 | kfree_const(buf->fw_id); |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 282 | kfree(buf); |
| 283 | } |
| 284 | |
| 285 | static void fw_free_buf(struct firmware_buf *buf) |
| 286 | { |
Chuansheng Liu | bd9eb7f | 2012-11-10 01:27:22 +0800 | [diff] [blame] | 287 | struct firmware_cache *fwc = buf->fwc; |
| 288 | spin_lock(&fwc->lock); |
| 289 | if (!kref_put(&buf->ref, __fw_free_buf)) |
| 290 | spin_unlock(&fwc->lock); |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 291 | } |
| 292 | |
jinjiawu | 74d1d60 | 2020-05-14 18:02:17 +0800 | [diff] [blame] | 293 | //[FairPhone][Audio][jinjia]=2018.11.21=smart amp porting. -s |
Ming Lei | 746058f | 2012-10-09 12:01:03 +0800 | [diff] [blame] | 294 | /* direct firmware loading support */ |
Ming Lei | 2760284 | 2012-11-03 17:47:58 +0800 | [diff] [blame] | 295 | static char fw_path_para[256]; |
| 296 | static const char * const fw_path[] = { |
| 297 | fw_path_para, |
jinjiawu | 74d1d60 | 2020-05-14 18:02:17 +0800 | [diff] [blame] | 298 | "/system/vendor/firmware", |
Ming Lei | 746058f | 2012-10-09 12:01:03 +0800 | [diff] [blame] | 299 | "/lib/firmware/updates/" UTS_RELEASE, |
| 300 | "/lib/firmware/updates", |
| 301 | "/lib/firmware/" UTS_RELEASE, |
Satya Durga Srinivasu Prabhala | 036c9b2 | 2017-02-27 11:27:10 -0800 | [diff] [blame] | 302 | "/lib/firmware" |
Ming Lei | 746058f | 2012-10-09 12:01:03 +0800 | [diff] [blame] | 303 | }; |
jinjiawu | 74d1d60 | 2020-05-14 18:02:17 +0800 | [diff] [blame] | 304 | //[FairPhone][Audio][jinjia]=2018.11.21=smart amp porting. -e |
Ming Lei | 746058f | 2012-10-09 12:01:03 +0800 | [diff] [blame] | 305 | |
Ming Lei | 2760284 | 2012-11-03 17:47:58 +0800 | [diff] [blame] | 306 | /* |
| 307 | * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH' |
| 308 | * from kernel command line because firmware_class is generally built in |
| 309 | * kernel instead of module. |
| 310 | */ |
| 311 | module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644); |
| 312 | MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path"); |
| 313 | |
Luis R. Rodriguez | 5275d19 | 2015-07-30 15:48:57 -0700 | [diff] [blame] | 314 | static void fw_finish_direct_load(struct device *device, |
| 315 | struct firmware_buf *buf) |
| 316 | { |
| 317 | mutex_lock(&fw_lock); |
| 318 | set_bit(FW_STATUS_DONE, &buf->status); |
| 319 | complete_all(&buf->completion); |
| 320 | mutex_unlock(&fw_lock); |
| 321 | } |
| 322 | |
Stephen Boyd | a098ecd | 2016-08-02 14:04:28 -0700 | [diff] [blame] | 323 | static int |
| 324 | fw_get_filesystem_firmware(struct device *device, struct firmware_buf *buf) |
Ming Lei | 746058f | 2012-10-09 12:01:03 +0800 | [diff] [blame] | 325 | { |
Mimi Zohar | e40ba6d | 2015-11-19 12:39:22 -0500 | [diff] [blame] | 326 | loff_t size; |
Luis R. Rodriguez | 1ba4de1 | 2015-05-12 14:49:41 -0700 | [diff] [blame] | 327 | int i, len; |
Neil Horman | 3e358ac | 2013-09-06 15:36:08 -0400 | [diff] [blame] | 328 | int rc = -ENOENT; |
Luis R. Rodriguez | f5727b0 | 2015-05-12 14:49:40 -0700 | [diff] [blame] | 329 | char *path; |
Stephen Boyd | a098ecd | 2016-08-02 14:04:28 -0700 | [diff] [blame] | 330 | enum kernel_read_file_id id = READING_FIRMWARE; |
| 331 | size_t msize = INT_MAX; |
| 332 | |
| 333 | /* Already populated data member means we're loading into a buffer */ |
| 334 | if (buf->data) { |
| 335 | id = READING_FIRMWARE_PREALLOC_BUFFER; |
| 336 | msize = buf->allocated_size; |
| 337 | } |
Luis R. Rodriguez | f5727b0 | 2015-05-12 14:49:40 -0700 | [diff] [blame] | 338 | |
| 339 | path = __getname(); |
| 340 | if (!path) |
| 341 | return -ENOMEM; |
Ming Lei | 746058f | 2012-10-09 12:01:03 +0800 | [diff] [blame] | 342 | |
| 343 | for (i = 0; i < ARRAY_SIZE(fw_path); i++) { |
Ming Lei | 2760284 | 2012-11-03 17:47:58 +0800 | [diff] [blame] | 344 | /* skip the unset customized path */ |
| 345 | if (!fw_path[i][0]) |
| 346 | continue; |
| 347 | |
Luis R. Rodriguez | 1ba4de1 | 2015-05-12 14:49:41 -0700 | [diff] [blame] | 348 | len = snprintf(path, PATH_MAX, "%s/%s", |
| 349 | fw_path[i], buf->fw_id); |
| 350 | if (len >= PATH_MAX) { |
| 351 | rc = -ENAMETOOLONG; |
| 352 | break; |
| 353 | } |
Ming Lei | 746058f | 2012-10-09 12:01:03 +0800 | [diff] [blame] | 354 | |
Mimi Zohar | e40ba6d | 2015-11-19 12:39:22 -0500 | [diff] [blame] | 355 | buf->size = 0; |
Stephen Boyd | a098ecd | 2016-08-02 14:04:28 -0700 | [diff] [blame] | 356 | rc = kernel_read_file_from_path(path, &buf->data, &size, msize, |
| 357 | id); |
Kees Cook | 4b2530d | 2016-02-04 13:15:02 -0800 | [diff] [blame] | 358 | if (rc) { |
Luis R. Rodriguez | 8e516aa | 2016-02-28 21:57:55 +0100 | [diff] [blame] | 359 | if (rc == -ENOENT) |
| 360 | dev_dbg(device, "loading %s failed with error %d\n", |
| 361 | path, rc); |
| 362 | else |
| 363 | dev_warn(device, "loading %s failed with error %d\n", |
| 364 | path, rc); |
Kees Cook | 4b2530d | 2016-02-04 13:15:02 -0800 | [diff] [blame] | 365 | continue; |
| 366 | } |
Mimi Zohar | e40ba6d | 2015-11-19 12:39:22 -0500 | [diff] [blame] | 367 | dev_dbg(device, "direct-loading %s\n", buf->fw_id); |
| 368 | buf->size = size; |
Luis R. Rodriguez | 5275d19 | 2015-07-30 15:48:57 -0700 | [diff] [blame] | 369 | fw_finish_direct_load(device, buf); |
Kees Cook | 4b2530d | 2016-02-04 13:15:02 -0800 | [diff] [blame] | 370 | break; |
Takashi Iwai | 4e0c92d | 2013-01-31 11:13:54 +0100 | [diff] [blame] | 371 | } |
Kees Cook | 4b2530d | 2016-02-04 13:15:02 -0800 | [diff] [blame] | 372 | __putname(path); |
Takashi Iwai | 4e0c92d | 2013-01-31 11:13:54 +0100 | [diff] [blame] | 373 | |
Neil Horman | 3e358ac | 2013-09-06 15:36:08 -0400 | [diff] [blame] | 374 | return rc; |
Ming Lei | 746058f | 2012-10-09 12:01:03 +0800 | [diff] [blame] | 375 | } |
| 376 | |
Takashi Iwai | 7b1269f | 2013-01-31 11:13:55 +0100 | [diff] [blame] | 377 | /* firmware holds the ownership of pages */ |
| 378 | static void firmware_free_data(const struct firmware *fw) |
| 379 | { |
| 380 | /* Loaded directly? */ |
| 381 | if (!fw->priv) { |
| 382 | vfree(fw->data); |
| 383 | return; |
| 384 | } |
| 385 | fw_free_buf(fw->priv); |
| 386 | } |
| 387 | |
Takashi Iwai | cd7239f | 2013-01-31 11:13:56 +0100 | [diff] [blame] | 388 | /* store the pages buffer info firmware from buf */ |
| 389 | static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw) |
| 390 | { |
| 391 | fw->priv = buf; |
Takashi Iwai | 7b1269f | 2013-01-31 11:13:55 +0100 | [diff] [blame] | 392 | #ifdef CONFIG_FW_LOADER_USER_HELPER |
Takashi Iwai | cd7239f | 2013-01-31 11:13:56 +0100 | [diff] [blame] | 393 | fw->pages = buf->pages; |
| 394 | #endif |
| 395 | fw->size = buf->size; |
| 396 | fw->data = buf->data; |
| 397 | |
| 398 | pr_debug("%s: fw-%s buf=%p data=%p size=%u\n", |
| 399 | __func__, buf->fw_id, buf, buf->data, |
| 400 | (unsigned int)buf->size); |
| 401 | } |
| 402 | |
| 403 | #ifdef CONFIG_PM_SLEEP |
| 404 | static void fw_name_devm_release(struct device *dev, void *res) |
| 405 | { |
| 406 | struct fw_name_devm *fwn = res; |
| 407 | |
| 408 | if (fwn->magic == (unsigned long)&fw_cache) |
| 409 | pr_debug("%s: fw_name-%s devm-%p released\n", |
| 410 | __func__, fwn->name, res); |
Luis R. Rodriguez | e0fd9b1 | 2015-05-12 14:49:43 -0700 | [diff] [blame] | 411 | kfree_const(fwn->name); |
Takashi Iwai | cd7239f | 2013-01-31 11:13:56 +0100 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | static int fw_devm_match(struct device *dev, void *res, |
| 415 | void *match_data) |
| 416 | { |
| 417 | struct fw_name_devm *fwn = res; |
| 418 | |
| 419 | return (fwn->magic == (unsigned long)&fw_cache) && |
| 420 | !strcmp(fwn->name, match_data); |
| 421 | } |
| 422 | |
| 423 | static struct fw_name_devm *fw_find_devm_name(struct device *dev, |
| 424 | const char *name) |
| 425 | { |
| 426 | struct fw_name_devm *fwn; |
| 427 | |
| 428 | fwn = devres_find(dev, fw_name_devm_release, |
| 429 | fw_devm_match, (void *)name); |
| 430 | return fwn; |
| 431 | } |
| 432 | |
| 433 | /* add firmware name into devres list */ |
| 434 | static int fw_add_devm_name(struct device *dev, const char *name) |
| 435 | { |
| 436 | struct fw_name_devm *fwn; |
| 437 | |
| 438 | fwn = fw_find_devm_name(dev, name); |
| 439 | if (fwn) |
| 440 | return 1; |
| 441 | |
Luis R. Rodriguez | e0fd9b1 | 2015-05-12 14:49:43 -0700 | [diff] [blame] | 442 | fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm), |
| 443 | GFP_KERNEL); |
Takashi Iwai | cd7239f | 2013-01-31 11:13:56 +0100 | [diff] [blame] | 444 | if (!fwn) |
| 445 | return -ENOMEM; |
Luis R. Rodriguez | e0fd9b1 | 2015-05-12 14:49:43 -0700 | [diff] [blame] | 446 | fwn->name = kstrdup_const(name, GFP_KERNEL); |
| 447 | if (!fwn->name) { |
Vladimir Zapolskiy | a885de6 | 2015-07-29 23:26:28 +0300 | [diff] [blame] | 448 | devres_free(fwn); |
Luis R. Rodriguez | e0fd9b1 | 2015-05-12 14:49:43 -0700 | [diff] [blame] | 449 | return -ENOMEM; |
| 450 | } |
Takashi Iwai | cd7239f | 2013-01-31 11:13:56 +0100 | [diff] [blame] | 451 | |
| 452 | fwn->magic = (unsigned long)&fw_cache; |
Takashi Iwai | cd7239f | 2013-01-31 11:13:56 +0100 | [diff] [blame] | 453 | devres_add(dev, fwn); |
| 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | #else |
| 458 | static int fw_add_devm_name(struct device *dev, const char *name) |
| 459 | { |
| 460 | return 0; |
| 461 | } |
| 462 | #endif |
| 463 | |
| 464 | |
| 465 | /* |
| 466 | * user-mode helper code |
| 467 | */ |
| 468 | #ifdef CONFIG_FW_LOADER_USER_HELPER |
| 469 | struct firmware_priv { |
Takashi Iwai | cd7239f | 2013-01-31 11:13:56 +0100 | [diff] [blame] | 470 | bool nowait; |
| 471 | struct device dev; |
| 472 | struct firmware_buf *buf; |
| 473 | struct firmware *fw; |
| 474 | }; |
Takashi Iwai | 7b1269f | 2013-01-31 11:13:55 +0100 | [diff] [blame] | 475 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 476 | static struct firmware_priv *to_firmware_priv(struct device *dev) |
| 477 | { |
| 478 | return container_of(dev, struct firmware_priv, dev); |
| 479 | } |
| 480 | |
Greg Kroah-Hartman | 7068cb0 | 2013-06-19 20:26:43 -0700 | [diff] [blame] | 481 | static void __fw_load_abort(struct firmware_buf *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | { |
Ming Lei | 8759793 | 2013-06-15 16:36:38 +0800 | [diff] [blame] | 483 | /* |
| 484 | * There is a small window in which user can write to 'loading' |
| 485 | * between loading done and disappearance of 'loading' |
| 486 | */ |
| 487 | if (test_bit(FW_STATUS_DONE, &buf->status)) |
| 488 | return; |
| 489 | |
Takashi Iwai | fe30414 | 2013-05-22 18:28:38 +0200 | [diff] [blame] | 490 | list_del_init(&buf->pending_list); |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 491 | set_bit(FW_STATUS_ABORT, &buf->status); |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 492 | complete_all(&buf->completion); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Greg Kroah-Hartman | 7068cb0 | 2013-06-19 20:26:43 -0700 | [diff] [blame] | 495 | static void fw_load_abort(struct firmware_priv *fw_priv) |
| 496 | { |
| 497 | struct firmware_buf *buf = fw_priv->buf; |
| 498 | |
| 499 | __fw_load_abort(buf); |
Ming Lei | 8759793 | 2013-06-15 16:36:38 +0800 | [diff] [blame] | 500 | |
| 501 | /* avoid user action after loading abort */ |
| 502 | fw_priv->buf = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Takashi Iwai | 807be03 | 2013-01-31 11:13:57 +0100 | [diff] [blame] | 505 | #define is_fw_load_aborted(buf) \ |
| 506 | test_bit(FW_STATUS_ABORT, &(buf)->status) |
| 507 | |
Takashi Iwai | fe30414 | 2013-05-22 18:28:38 +0200 | [diff] [blame] | 508 | static LIST_HEAD(pending_fw_head); |
| 509 | |
| 510 | /* reboot notifier for avoid deadlock with usermode_lock */ |
| 511 | static int fw_shutdown_notify(struct notifier_block *unused1, |
| 512 | unsigned long unused2, void *unused3) |
| 513 | { |
| 514 | mutex_lock(&fw_lock); |
| 515 | while (!list_empty(&pending_fw_head)) |
Greg Kroah-Hartman | 7068cb0 | 2013-06-19 20:26:43 -0700 | [diff] [blame] | 516 | __fw_load_abort(list_first_entry(&pending_fw_head, |
Takashi Iwai | fe30414 | 2013-05-22 18:28:38 +0200 | [diff] [blame] | 517 | struct firmware_buf, |
| 518 | pending_list)); |
| 519 | mutex_unlock(&fw_lock); |
| 520 | return NOTIFY_DONE; |
| 521 | } |
| 522 | |
| 523 | static struct notifier_block fw_shutdown_nb = { |
| 524 | .notifier_call = fw_shutdown_notify, |
| 525 | }; |
| 526 | |
Greg Kroah-Hartman | 14adbe5 | 2013-08-23 17:08:48 -0700 | [diff] [blame] | 527 | static ssize_t timeout_show(struct class *class, struct class_attribute *attr, |
| 528 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | { |
| 530 | return sprintf(buf, "%d\n", loading_timeout); |
| 531 | } |
| 532 | |
| 533 | /** |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 534 | * firmware_timeout_store - set number of seconds to wait for firmware |
| 535 | * @class: device class pointer |
Randy Dunlap | e59817b | 2010-03-10 11:47:58 -0800 | [diff] [blame] | 536 | * @attr: device attribute pointer |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 537 | * @buf: buffer to scan for timeout value |
| 538 | * @count: number of bytes in @buf |
| 539 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | * Sets the number of seconds to wait for the firmware. Once |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 541 | * this expires an error will be returned to the driver and no |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | * firmware will be provided. |
| 543 | * |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 544 | * Note: zero means 'wait forever'. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | **/ |
Greg Kroah-Hartman | 14adbe5 | 2013-08-23 17:08:48 -0700 | [diff] [blame] | 546 | static ssize_t timeout_store(struct class *class, struct class_attribute *attr, |
| 547 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | { |
| 549 | loading_timeout = simple_strtol(buf, NULL, 10); |
Stanislaw W. Gruszka | b92eac0 | 2005-06-28 20:44:51 -0700 | [diff] [blame] | 550 | if (loading_timeout < 0) |
| 551 | loading_timeout = 0; |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | return count; |
| 554 | } |
| 555 | |
Dmitry Torokhov | 673fae9 | 2010-03-13 23:49:13 -0800 | [diff] [blame] | 556 | static struct class_attribute firmware_class_attrs[] = { |
Greg Kroah-Hartman | 14adbe5 | 2013-08-23 17:08:48 -0700 | [diff] [blame] | 557 | __ATTR_RW(timeout), |
Dmitry Torokhov | 673fae9 | 2010-03-13 23:49:13 -0800 | [diff] [blame] | 558 | __ATTR_NULL |
| 559 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | |
Dmitry Torokhov | 673fae9 | 2010-03-13 23:49:13 -0800 | [diff] [blame] | 561 | static void fw_dev_release(struct device *dev) |
| 562 | { |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 563 | struct firmware_priv *fw_priv = to_firmware_priv(dev); |
Ming Lei | 65710cb | 2012-08-04 12:01:16 +0800 | [diff] [blame] | 564 | |
Dmitry Torokhov | 673fae9 | 2010-03-13 23:49:13 -0800 | [diff] [blame] | 565 | kfree(fw_priv); |
Dmitry Torokhov | 673fae9 | 2010-03-13 23:49:13 -0800 | [diff] [blame] | 566 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | |
Linus Torvalds | 6f95772 | 2015-07-09 11:20:01 -0700 | [diff] [blame] | 568 | static int do_firmware_uevent(struct firmware_priv *fw_priv, struct kobj_uevent_env *env) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | { |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 570 | if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | return -ENOMEM; |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 572 | if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout)) |
kay.sievers@vrfy.org | 6897089 | 2005-04-18 21:57:31 -0700 | [diff] [blame] | 573 | return -ENOMEM; |
Johannes Berg | e9045f9 | 2010-03-29 17:57:20 +0200 | [diff] [blame] | 574 | if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait)) |
| 575 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
| 577 | return 0; |
| 578 | } |
| 579 | |
Linus Torvalds | 6f95772 | 2015-07-09 11:20:01 -0700 | [diff] [blame] | 580 | static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 581 | { |
| 582 | struct firmware_priv *fw_priv = to_firmware_priv(dev); |
| 583 | int err = 0; |
| 584 | |
| 585 | mutex_lock(&fw_lock); |
| 586 | if (fw_priv->buf) |
| 587 | err = do_firmware_uevent(fw_priv, env); |
| 588 | mutex_unlock(&fw_lock); |
| 589 | return err; |
| 590 | } |
| 591 | |
Adrian Bunk | 1b81d66 | 2006-05-20 15:00:16 -0700 | [diff] [blame] | 592 | static struct class firmware_class = { |
| 593 | .name = "firmware", |
Dmitry Torokhov | 673fae9 | 2010-03-13 23:49:13 -0800 | [diff] [blame] | 594 | .class_attrs = firmware_class_attrs, |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 595 | .dev_uevent = firmware_uevent, |
| 596 | .dev_release = fw_dev_release, |
Adrian Bunk | 1b81d66 | 2006-05-20 15:00:16 -0700 | [diff] [blame] | 597 | }; |
| 598 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 599 | static ssize_t firmware_loading_show(struct device *dev, |
| 600 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | { |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 602 | struct firmware_priv *fw_priv = to_firmware_priv(dev); |
Ming Lei | 8759793 | 2013-06-15 16:36:38 +0800 | [diff] [blame] | 603 | int loading = 0; |
| 604 | |
| 605 | mutex_lock(&fw_lock); |
| 606 | if (fw_priv->buf) |
| 607 | loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status); |
| 608 | mutex_unlock(&fw_lock); |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 609 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | return sprintf(buf, "%d\n", loading); |
| 611 | } |
| 612 | |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 613 | /* Some architectures don't have PAGE_KERNEL_RO */ |
| 614 | #ifndef PAGE_KERNEL_RO |
| 615 | #define PAGE_KERNEL_RO PAGE_KERNEL |
| 616 | #endif |
Ming Lei | 253c924 | 2012-10-09 12:01:02 +0800 | [diff] [blame] | 617 | |
| 618 | /* one pages buffer should be mapped/unmapped only once */ |
| 619 | static int fw_map_pages_buf(struct firmware_buf *buf) |
| 620 | { |
Takashi Iwai | cd7239f | 2013-01-31 11:13:56 +0100 | [diff] [blame] | 621 | if (!buf->is_paged_buf) |
Ming Lei | 746058f | 2012-10-09 12:01:03 +0800 | [diff] [blame] | 622 | return 0; |
| 623 | |
Markus Elfring | daa3d67 | 2014-11-19 11:38:38 +0100 | [diff] [blame] | 624 | vunmap(buf->data); |
Ming Lei | 253c924 | 2012-10-09 12:01:02 +0800 | [diff] [blame] | 625 | buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO); |
| 626 | if (!buf->data) |
| 627 | return -ENOMEM; |
| 628 | return 0; |
| 629 | } |
| 630 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | /** |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 632 | * firmware_loading_store - set value in the 'loading' control file |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 633 | * @dev: device pointer |
Randy Dunlap | af9997e | 2006-12-22 01:06:52 -0800 | [diff] [blame] | 634 | * @attr: device attribute pointer |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 635 | * @buf: buffer to scan for loading control value |
| 636 | * @count: number of bytes in @buf |
| 637 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | * The relevant values are: |
| 639 | * |
| 640 | * 1: Start a load, discarding any previous partial load. |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 641 | * 0: Conclude the load and hand the data to the driver code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | * -1: Conclude the load with an error and discard any written data. |
| 643 | **/ |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 644 | static ssize_t firmware_loading_store(struct device *dev, |
| 645 | struct device_attribute *attr, |
| 646 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | { |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 648 | struct firmware_priv *fw_priv = to_firmware_priv(dev); |
Ming Lei | 8759793 | 2013-06-15 16:36:38 +0800 | [diff] [blame] | 649 | struct firmware_buf *fw_buf; |
Kees Cook | 6593d92 | 2014-02-25 13:06:00 -0800 | [diff] [blame] | 650 | ssize_t written = count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | int loading = simple_strtol(buf, NULL, 10); |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 652 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | |
Neil Horman | eea915b | 2012-01-02 15:31:23 -0500 | [diff] [blame] | 654 | mutex_lock(&fw_lock); |
Ming Lei | 8759793 | 2013-06-15 16:36:38 +0800 | [diff] [blame] | 655 | fw_buf = fw_priv->buf; |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 656 | if (!fw_buf) |
Neil Horman | eea915b | 2012-01-02 15:31:23 -0500 | [diff] [blame] | 657 | goto out; |
| 658 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | switch (loading) { |
| 660 | case 1: |
Ming Lei | 65710cb | 2012-08-04 12:01:16 +0800 | [diff] [blame] | 661 | /* discarding any previous partial load */ |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 662 | if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) { |
| 663 | for (i = 0; i < fw_buf->nr_pages; i++) |
| 664 | __free_page(fw_buf->pages[i]); |
Chen Feng | 10a3fbf | 2015-12-16 17:03:15 +0800 | [diff] [blame] | 665 | vfree(fw_buf->pages); |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 666 | fw_buf->pages = NULL; |
| 667 | fw_buf->page_array_size = 0; |
| 668 | fw_buf->nr_pages = 0; |
| 669 | set_bit(FW_STATUS_LOADING, &fw_buf->status); |
Ming Lei | 28eefa7 | 2012-08-04 12:01:17 +0800 | [diff] [blame] | 670 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | break; |
| 672 | case 0: |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 673 | if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) { |
Kees Cook | 6593d92 | 2014-02-25 13:06:00 -0800 | [diff] [blame] | 674 | int rc; |
| 675 | |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 676 | set_bit(FW_STATUS_DONE, &fw_buf->status); |
| 677 | clear_bit(FW_STATUS_LOADING, &fw_buf->status); |
Ming Lei | 253c924 | 2012-10-09 12:01:02 +0800 | [diff] [blame] | 678 | |
| 679 | /* |
| 680 | * Several loading requests may be pending on |
| 681 | * one same firmware buf, so let all requests |
| 682 | * see the mapped 'buf->data' once the loading |
| 683 | * is completed. |
| 684 | * */ |
Kees Cook | 6593d92 | 2014-02-25 13:06:00 -0800 | [diff] [blame] | 685 | rc = fw_map_pages_buf(fw_buf); |
| 686 | if (rc) |
zhang jun | 2b1278c | 2014-02-13 15:18:47 +0800 | [diff] [blame] | 687 | dev_err(dev, "%s: map pages failed\n", |
| 688 | __func__); |
Kees Cook | 6593d92 | 2014-02-25 13:06:00 -0800 | [diff] [blame] | 689 | else |
Mimi Zohar | e40ba6d | 2015-11-19 12:39:22 -0500 | [diff] [blame] | 690 | rc = security_kernel_post_read_file(NULL, |
| 691 | fw_buf->data, fw_buf->size, |
| 692 | READING_FIRMWARE); |
Kees Cook | 6593d92 | 2014-02-25 13:06:00 -0800 | [diff] [blame] | 693 | |
| 694 | /* |
| 695 | * Same logic as fw_load_abort, only the DONE bit |
| 696 | * is ignored and we set ABORT only on failure. |
| 697 | */ |
Takashi Iwai | fe30414 | 2013-05-22 18:28:38 +0200 | [diff] [blame] | 698 | list_del_init(&fw_buf->pending_list); |
Kees Cook | 6593d92 | 2014-02-25 13:06:00 -0800 | [diff] [blame] | 699 | if (rc) { |
| 700 | set_bit(FW_STATUS_ABORT, &fw_buf->status); |
| 701 | written = rc; |
| 702 | } |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 703 | complete_all(&fw_buf->completion); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | break; |
| 705 | } |
| 706 | /* fallthrough */ |
| 707 | default: |
Bjorn Helgaas | 266a813 | 2008-10-15 22:04:20 -0700 | [diff] [blame] | 708 | dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | /* fallthrough */ |
| 710 | case -1: |
| 711 | fw_load_abort(fw_priv); |
| 712 | break; |
| 713 | } |
Neil Horman | eea915b | 2012-01-02 15:31:23 -0500 | [diff] [blame] | 714 | out: |
| 715 | mutex_unlock(&fw_lock); |
Kees Cook | 6593d92 | 2014-02-25 13:06:00 -0800 | [diff] [blame] | 716 | return written; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 719 | static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | |
Stephen Boyd | a098ecd | 2016-08-02 14:04:28 -0700 | [diff] [blame] | 721 | static void firmware_rw_buf(struct firmware_buf *buf, char *buffer, |
| 722 | loff_t offset, size_t count, bool read) |
| 723 | { |
| 724 | if (read) |
| 725 | memcpy(buffer, buf->data + offset, count); |
| 726 | else |
| 727 | memcpy(buf->data + offset, buffer, count); |
| 728 | } |
| 729 | |
Stephen Boyd | 9ccf981 | 2016-08-02 14:04:22 -0700 | [diff] [blame] | 730 | static void firmware_rw(struct firmware_buf *buf, char *buffer, |
| 731 | loff_t offset, size_t count, bool read) |
| 732 | { |
| 733 | while (count) { |
| 734 | void *page_data; |
| 735 | int page_nr = offset >> PAGE_SHIFT; |
| 736 | int page_ofs = offset & (PAGE_SIZE-1); |
| 737 | int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count); |
| 738 | |
| 739 | page_data = kmap(buf->pages[page_nr]); |
| 740 | |
| 741 | if (read) |
| 742 | memcpy(buffer, page_data + page_ofs, page_cnt); |
| 743 | else |
| 744 | memcpy(page_data + page_ofs, buffer, page_cnt); |
| 745 | |
| 746 | kunmap(buf->pages[page_nr]); |
| 747 | buffer += page_cnt; |
| 748 | offset += page_cnt; |
| 749 | count -= page_cnt; |
| 750 | } |
| 751 | } |
| 752 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 753 | static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj, |
| 754 | struct bin_attribute *bin_attr, |
| 755 | char *buffer, loff_t offset, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | { |
Lars-Peter Clausen | b0d1f80 | 2012-07-03 18:49:36 +0200 | [diff] [blame] | 757 | struct device *dev = kobj_to_dev(kobj); |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 758 | struct firmware_priv *fw_priv = to_firmware_priv(dev); |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 759 | struct firmware_buf *buf; |
Akinobu Mita | f37e661 | 2008-07-25 01:48:23 -0700 | [diff] [blame] | 760 | ssize_t ret_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 762 | mutex_lock(&fw_lock); |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 763 | buf = fw_priv->buf; |
| 764 | if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | ret_count = -ENODEV; |
| 766 | goto out; |
| 767 | } |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 768 | if (offset > buf->size) { |
Jiri Slaby | 308975f | 2009-06-21 23:57:31 +0200 | [diff] [blame] | 769 | ret_count = 0; |
| 770 | goto out; |
| 771 | } |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 772 | if (count > buf->size - offset) |
| 773 | count = buf->size - offset; |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 774 | |
| 775 | ret_count = count; |
| 776 | |
Stephen Boyd | a098ecd | 2016-08-02 14:04:28 -0700 | [diff] [blame] | 777 | if (buf->data) |
| 778 | firmware_rw_buf(buf, buffer, offset, count, true); |
| 779 | else |
| 780 | firmware_rw(buf, buffer, offset, count, true); |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 781 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | out: |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 783 | mutex_unlock(&fw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | return ret_count; |
| 785 | } |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 786 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 787 | static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | { |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 789 | struct firmware_buf *buf = fw_priv->buf; |
Fabian Frederick | a76040d | 2014-07-01 21:13:30 +0200 | [diff] [blame] | 790 | int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 792 | /* If the array of pages is too small, grow it... */ |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 793 | if (buf->page_array_size < pages_needed) { |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 794 | int new_array_size = max(pages_needed, |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 795 | buf->page_array_size * 2); |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 796 | struct page **new_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | |
Chen Feng | 10a3fbf | 2015-12-16 17:03:15 +0800 | [diff] [blame] | 798 | new_pages = vmalloc(new_array_size * sizeof(void *)); |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 799 | if (!new_pages) { |
| 800 | fw_load_abort(fw_priv); |
| 801 | return -ENOMEM; |
| 802 | } |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 803 | memcpy(new_pages, buf->pages, |
| 804 | buf->page_array_size * sizeof(void *)); |
| 805 | memset(&new_pages[buf->page_array_size], 0, sizeof(void *) * |
| 806 | (new_array_size - buf->page_array_size)); |
Chen Feng | 10a3fbf | 2015-12-16 17:03:15 +0800 | [diff] [blame] | 807 | vfree(buf->pages); |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 808 | buf->pages = new_pages; |
| 809 | buf->page_array_size = new_array_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | } |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 811 | |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 812 | while (buf->nr_pages < pages_needed) { |
| 813 | buf->pages[buf->nr_pages] = |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 814 | alloc_page(GFP_KERNEL | __GFP_HIGHMEM); |
| 815 | |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 816 | if (!buf->pages[buf->nr_pages]) { |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 817 | fw_load_abort(fw_priv); |
| 818 | return -ENOMEM; |
| 819 | } |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 820 | buf->nr_pages++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | return 0; |
| 823 | } |
| 824 | |
| 825 | /** |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 826 | * firmware_data_write - write method for firmware |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 827 | * @filp: open sysfs file |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 828 | * @kobj: kobject for the device |
Randy Dunlap | 42e61f4 | 2007-07-23 21:42:11 -0700 | [diff] [blame] | 829 | * @bin_attr: bin_attr structure |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 830 | * @buffer: buffer being written |
| 831 | * @offset: buffer offset for write in total data store area |
| 832 | * @count: buffer size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | * |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 834 | * Data written to the 'data' attribute will be later handed to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | * the driver as a firmware image. |
| 836 | **/ |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 837 | static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj, |
| 838 | struct bin_attribute *bin_attr, |
| 839 | char *buffer, loff_t offset, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | { |
Lars-Peter Clausen | b0d1f80 | 2012-07-03 18:49:36 +0200 | [diff] [blame] | 841 | struct device *dev = kobj_to_dev(kobj); |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 842 | struct firmware_priv *fw_priv = to_firmware_priv(dev); |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 843 | struct firmware_buf *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | ssize_t retval; |
| 845 | |
| 846 | if (!capable(CAP_SYS_RAWIO)) |
| 847 | return -EPERM; |
Stanislaw W. Gruszka | b92eac0 | 2005-06-28 20:44:51 -0700 | [diff] [blame] | 848 | |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 849 | mutex_lock(&fw_lock); |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 850 | buf = fw_priv->buf; |
| 851 | if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | retval = -ENODEV; |
| 853 | goto out; |
| 854 | } |
Ming Lei | 65710cb | 2012-08-04 12:01:16 +0800 | [diff] [blame] | 855 | |
Stephen Boyd | a098ecd | 2016-08-02 14:04:28 -0700 | [diff] [blame] | 856 | if (buf->data) { |
| 857 | if (offset + count > buf->allocated_size) { |
| 858 | retval = -ENOMEM; |
| 859 | goto out; |
| 860 | } |
| 861 | firmware_rw_buf(buf, buffer, offset, count, false); |
| 862 | retval = count; |
| 863 | } else { |
| 864 | retval = fw_realloc_buffer(fw_priv, offset + count); |
| 865 | if (retval) |
| 866 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | |
Stephen Boyd | a098ecd | 2016-08-02 14:04:28 -0700 | [diff] [blame] | 868 | retval = count; |
| 869 | firmware_rw(buf, buffer, offset, count, false); |
| 870 | } |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 871 | |
Stephen Boyd | 9ccf981 | 2016-08-02 14:04:22 -0700 | [diff] [blame] | 872 | buf->size = max_t(size_t, offset + count, buf->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | out: |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 874 | mutex_unlock(&fw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | return retval; |
| 876 | } |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 877 | |
Dmitry Torokhov | 0983ca2 | 2010-06-04 00:54:37 -0700 | [diff] [blame] | 878 | static struct bin_attribute firmware_attr_data = { |
| 879 | .attr = { .name = "data", .mode = 0644 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | .size = 0, |
| 881 | .read = firmware_data_read, |
| 882 | .write = firmware_data_write, |
| 883 | }; |
| 884 | |
Takashi Iwai | 4623990 | 2015-02-04 15:18:07 +0100 | [diff] [blame] | 885 | static struct attribute *fw_dev_attrs[] = { |
| 886 | &dev_attr_loading.attr, |
| 887 | NULL |
| 888 | }; |
| 889 | |
| 890 | static struct bin_attribute *fw_dev_bin_attrs[] = { |
| 891 | &firmware_attr_data, |
| 892 | NULL |
| 893 | }; |
| 894 | |
| 895 | static const struct attribute_group fw_dev_attr_group = { |
| 896 | .attrs = fw_dev_attrs, |
| 897 | .bin_attrs = fw_dev_bin_attrs, |
| 898 | }; |
| 899 | |
| 900 | static const struct attribute_group *fw_dev_attr_groups[] = { |
| 901 | &fw_dev_attr_group, |
| 902 | NULL |
| 903 | }; |
| 904 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 905 | static struct firmware_priv * |
Stephen Boyd | dddb554 | 2012-03-28 23:30:43 +0200 | [diff] [blame] | 906 | fw_create_instance(struct firmware *firmware, const char *fw_name, |
Takashi Iwai | 14c4bae | 2013-12-02 15:38:18 +0100 | [diff] [blame] | 907 | struct device *device, unsigned int opt_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | { |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 909 | struct firmware_priv *fw_priv; |
| 910 | struct device *f_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 912 | fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL); |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 913 | if (!fw_priv) { |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 914 | fw_priv = ERR_PTR(-ENOMEM); |
| 915 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | |
Takashi Iwai | 14c4bae | 2013-12-02 15:38:18 +0100 | [diff] [blame] | 918 | fw_priv->nowait = !!(opt_flags & FW_OPT_NOWAIT); |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 919 | fw_priv->fw = firmware; |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 920 | f_dev = &fw_priv->dev; |
| 921 | |
| 922 | device_initialize(f_dev); |
Ming Lei | 99c2aa7 | 2012-08-04 12:01:19 +0800 | [diff] [blame] | 923 | dev_set_name(f_dev, "%s", fw_name); |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 924 | f_dev->parent = device; |
| 925 | f_dev->class = &firmware_class; |
Takashi Iwai | 4623990 | 2015-02-04 15:18:07 +0100 | [diff] [blame] | 926 | f_dev->groups = fw_dev_attr_groups; |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 927 | exit: |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 928 | return fw_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | } |
Takashi Iwai | cd7239f | 2013-01-31 11:13:56 +0100 | [diff] [blame] | 930 | |
| 931 | /* load a firmware via user helper */ |
Takashi Iwai | 14c4bae | 2013-12-02 15:38:18 +0100 | [diff] [blame] | 932 | static int _request_firmware_load(struct firmware_priv *fw_priv, |
| 933 | unsigned int opt_flags, long timeout) |
Takashi Iwai | cd7239f | 2013-01-31 11:13:56 +0100 | [diff] [blame] | 934 | { |
| 935 | int retval = 0; |
| 936 | struct device *f_dev = &fw_priv->dev; |
| 937 | struct firmware_buf *buf = fw_priv->buf; |
| 938 | |
| 939 | /* fall back on userspace loading */ |
Stephen Boyd | a098ecd | 2016-08-02 14:04:28 -0700 | [diff] [blame] | 940 | if (!buf->data) |
| 941 | buf->is_paged_buf = true; |
Takashi Iwai | cd7239f | 2013-01-31 11:13:56 +0100 | [diff] [blame] | 942 | |
| 943 | dev_set_uevent_suppress(f_dev, true); |
| 944 | |
Takashi Iwai | cd7239f | 2013-01-31 11:13:56 +0100 | [diff] [blame] | 945 | retval = device_add(f_dev); |
| 946 | if (retval) { |
| 947 | dev_err(f_dev, "%s: device_register failed\n", __func__); |
| 948 | goto err_put_dev; |
| 949 | } |
| 950 | |
Maxime Bizon | 1eeeef1 | 2013-08-29 20:28:13 +0200 | [diff] [blame] | 951 | mutex_lock(&fw_lock); |
| 952 | list_add(&buf->pending_list, &pending_fw_head); |
| 953 | mutex_unlock(&fw_lock); |
| 954 | |
Takashi Iwai | 14c4bae | 2013-12-02 15:38:18 +0100 | [diff] [blame] | 955 | if (opt_flags & FW_OPT_UEVENT) { |
Ming Lei | af5bc11 | 2013-05-27 18:30:04 +0800 | [diff] [blame] | 956 | buf->need_uevent = true; |
Takashi Iwai | cd7239f | 2013-01-31 11:13:56 +0100 | [diff] [blame] | 957 | dev_set_uevent_suppress(f_dev, false); |
| 958 | dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id); |
Takashi Iwai | cd7239f | 2013-01-31 11:13:56 +0100 | [diff] [blame] | 959 | kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD); |
Ming Lei | 68ff2a0 | 2015-01-13 00:02:01 +0800 | [diff] [blame] | 960 | } else { |
| 961 | timeout = MAX_JIFFY_OFFSET; |
Takashi Iwai | cd7239f | 2013-01-31 11:13:56 +0100 | [diff] [blame] | 962 | } |
| 963 | |
Kyle Yan | 5f4db9a | 2017-09-20 17:18:35 -0700 | [diff] [blame] | 964 | timeout = wait_for_completion_killable_timeout(&buf->completion, |
Ming Lei | 68ff2a0 | 2015-01-13 00:02:01 +0800 | [diff] [blame] | 965 | timeout); |
Yves-Alexis Perez | b3854ce | 2016-11-11 11:28:40 -0800 | [diff] [blame] | 966 | if (timeout == -ERESTARTSYS || !timeout) { |
| 967 | retval = timeout; |
Ming Lei | 0cb6424 | 2015-01-13 00:01:35 +0800 | [diff] [blame] | 968 | mutex_lock(&fw_lock); |
| 969 | fw_load_abort(fw_priv); |
| 970 | mutex_unlock(&fw_lock); |
Yves-Alexis Perez | b3854ce | 2016-11-11 11:28:40 -0800 | [diff] [blame] | 971 | } else if (timeout > 0) { |
Zahari Doychev | ef518cc | 2015-03-10 10:45:40 +0100 | [diff] [blame] | 972 | retval = 0; |
Ming Lei | 0cb6424 | 2015-01-13 00:01:35 +0800 | [diff] [blame] | 973 | } |
Takashi Iwai | cd7239f | 2013-01-31 11:13:56 +0100 | [diff] [blame] | 974 | |
Shuah Khan | 0542ad8 | 2014-07-22 18:10:38 -0600 | [diff] [blame] | 975 | if (is_fw_load_aborted(buf)) |
| 976 | retval = -EAGAIN; |
Stephen Boyd | a098ecd | 2016-08-02 14:04:28 -0700 | [diff] [blame] | 977 | else if (buf->is_paged_buf && !buf->data) |
zhang jun | 2b1278c | 2014-02-13 15:18:47 +0800 | [diff] [blame] | 978 | retval = -ENOMEM; |
Takashi Iwai | cd7239f | 2013-01-31 11:13:56 +0100 | [diff] [blame] | 979 | |
Takashi Iwai | cd7239f | 2013-01-31 11:13:56 +0100 | [diff] [blame] | 980 | device_del(f_dev); |
| 981 | err_put_dev: |
| 982 | put_device(f_dev); |
| 983 | return retval; |
| 984 | } |
| 985 | |
| 986 | static int fw_load_from_user_helper(struct firmware *firmware, |
| 987 | const char *name, struct device *device, |
Takashi Iwai | 14c4bae | 2013-12-02 15:38:18 +0100 | [diff] [blame] | 988 | unsigned int opt_flags, long timeout) |
Takashi Iwai | cd7239f | 2013-01-31 11:13:56 +0100 | [diff] [blame] | 989 | { |
| 990 | struct firmware_priv *fw_priv; |
| 991 | |
Takashi Iwai | 14c4bae | 2013-12-02 15:38:18 +0100 | [diff] [blame] | 992 | fw_priv = fw_create_instance(firmware, name, device, opt_flags); |
Takashi Iwai | cd7239f | 2013-01-31 11:13:56 +0100 | [diff] [blame] | 993 | if (IS_ERR(fw_priv)) |
| 994 | return PTR_ERR(fw_priv); |
| 995 | |
| 996 | fw_priv->buf = firmware->priv; |
Takashi Iwai | 14c4bae | 2013-12-02 15:38:18 +0100 | [diff] [blame] | 997 | return _request_firmware_load(fw_priv, opt_flags, timeout); |
Takashi Iwai | cd7239f | 2013-01-31 11:13:56 +0100 | [diff] [blame] | 998 | } |
Ming Lei | ddf1f06 | 2013-06-04 10:01:13 +0800 | [diff] [blame] | 999 | |
Tim Murray | 57c7ee1 | 2016-10-20 13:53:58 -0700 | [diff] [blame] | 1000 | #ifdef CONFIG_FW_CACHE |
Ming Lei | ddf1f06 | 2013-06-04 10:01:13 +0800 | [diff] [blame] | 1001 | /* kill pending requests without uevent to avoid blocking suspend */ |
| 1002 | static void kill_requests_without_uevent(void) |
| 1003 | { |
| 1004 | struct firmware_buf *buf; |
| 1005 | struct firmware_buf *next; |
| 1006 | |
| 1007 | mutex_lock(&fw_lock); |
| 1008 | list_for_each_entry_safe(buf, next, &pending_fw_head, pending_list) { |
| 1009 | if (!buf->need_uevent) |
Greg Kroah-Hartman | 7068cb0 | 2013-06-19 20:26:43 -0700 | [diff] [blame] | 1010 | __fw_load_abort(buf); |
Ming Lei | ddf1f06 | 2013-06-04 10:01:13 +0800 | [diff] [blame] | 1011 | } |
| 1012 | mutex_unlock(&fw_lock); |
| 1013 | } |
Ming Lei | 5b7cb7a | 2013-06-06 19:52:54 +0800 | [diff] [blame] | 1014 | #endif |
Ming Lei | ddf1f06 | 2013-06-04 10:01:13 +0800 | [diff] [blame] | 1015 | |
Takashi Iwai | cd7239f | 2013-01-31 11:13:56 +0100 | [diff] [blame] | 1016 | #else /* CONFIG_FW_LOADER_USER_HELPER */ |
| 1017 | static inline int |
| 1018 | fw_load_from_user_helper(struct firmware *firmware, const char *name, |
Takashi Iwai | 14c4bae | 2013-12-02 15:38:18 +0100 | [diff] [blame] | 1019 | struct device *device, unsigned int opt_flags, |
Takashi Iwai | cd7239f | 2013-01-31 11:13:56 +0100 | [diff] [blame] | 1020 | long timeout) |
| 1021 | { |
| 1022 | return -ENOENT; |
| 1023 | } |
Takashi Iwai | 807be03 | 2013-01-31 11:13:57 +0100 | [diff] [blame] | 1024 | |
| 1025 | /* No abort during direct loading */ |
| 1026 | #define is_fw_load_aborted(buf) false |
| 1027 | |
Ming Lei | 5b7cb7a | 2013-06-06 19:52:54 +0800 | [diff] [blame] | 1028 | #ifdef CONFIG_PM_SLEEP |
Ming Lei | ddf1f06 | 2013-06-04 10:01:13 +0800 | [diff] [blame] | 1029 | static inline void kill_requests_without_uevent(void) { } |
Ming Lei | 5b7cb7a | 2013-06-06 19:52:54 +0800 | [diff] [blame] | 1030 | #endif |
Ming Lei | ddf1f06 | 2013-06-04 10:01:13 +0800 | [diff] [blame] | 1031 | |
Takashi Iwai | 7b1269f | 2013-01-31 11:13:55 +0100 | [diff] [blame] | 1032 | #endif /* CONFIG_FW_LOADER_USER_HELPER */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | |
Ming Lei | f531f05a | 2012-08-04 12:01:25 +0800 | [diff] [blame] | 1034 | |
Takashi Iwai | 4e0c92d | 2013-01-31 11:13:54 +0100 | [diff] [blame] | 1035 | /* wait until the shared firmware_buf becomes ready (or error) */ |
| 1036 | static int sync_cached_firmware_buf(struct firmware_buf *buf) |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 1037 | { |
Takashi Iwai | 4e0c92d | 2013-01-31 11:13:54 +0100 | [diff] [blame] | 1038 | int ret = 0; |
| 1039 | |
| 1040 | mutex_lock(&fw_lock); |
| 1041 | while (!test_bit(FW_STATUS_DONE, &buf->status)) { |
Takashi Iwai | 807be03 | 2013-01-31 11:13:57 +0100 | [diff] [blame] | 1042 | if (is_fw_load_aborted(buf)) { |
Takashi Iwai | 4e0c92d | 2013-01-31 11:13:54 +0100 | [diff] [blame] | 1043 | ret = -ENOENT; |
| 1044 | break; |
| 1045 | } |
| 1046 | mutex_unlock(&fw_lock); |
Kweh, Hock Leong | 000deba | 2014-11-18 10:56:59 +0800 | [diff] [blame] | 1047 | ret = wait_for_completion_interruptible(&buf->completion); |
Takashi Iwai | 4e0c92d | 2013-01-31 11:13:54 +0100 | [diff] [blame] | 1048 | mutex_lock(&fw_lock); |
| 1049 | } |
| 1050 | mutex_unlock(&fw_lock); |
| 1051 | return ret; |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 1052 | } |
| 1053 | |
Takashi Iwai | 4e0c92d | 2013-01-31 11:13:54 +0100 | [diff] [blame] | 1054 | /* prepare firmware and firmware_buf structs; |
| 1055 | * return 0 if a firmware is already assigned, 1 if need to load one, |
| 1056 | * or a negative error code |
| 1057 | */ |
| 1058 | static int |
| 1059 | _request_firmware_prepare(struct firmware **firmware_p, const char *name, |
Vikram Mulukutla | b19be88 | 2017-04-24 19:31:14 -0700 | [diff] [blame] | 1060 | struct device *device, void *dbuf, size_t size, |
| 1061 | unsigned int opt_flags) |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 1062 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | struct firmware *firmware; |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 1064 | struct firmware_buf *buf; |
| 1065 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | |
Jiri Slaby | 4aed064 | 2005-09-13 01:25:01 -0700 | [diff] [blame] | 1067 | *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | if (!firmware) { |
Bjorn Helgaas | 266a813 | 2008-10-15 22:04:20 -0700 | [diff] [blame] | 1069 | dev_err(device, "%s: kmalloc(struct firmware) failed\n", |
| 1070 | __func__); |
Takashi Iwai | 4e0c92d | 2013-01-31 11:13:54 +0100 | [diff] [blame] | 1071 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | |
Stephen Boyd | a098ecd | 2016-08-02 14:04:28 -0700 | [diff] [blame] | 1074 | if (fw_get_builtin_firmware(firmware, name, dbuf, size)) { |
Luis R. Rodriguez | ed04630 | 2015-04-29 16:30:43 -0700 | [diff] [blame] | 1075 | dev_dbg(device, "using built-in %s\n", name); |
Takashi Iwai | 4e0c92d | 2013-01-31 11:13:54 +0100 | [diff] [blame] | 1076 | return 0; /* assigned */ |
David Woodhouse | 5658c76 | 2008-05-23 13:52:42 +0100 | [diff] [blame] | 1077 | } |
| 1078 | |
Vikram Mulukutla | b19be88 | 2017-04-24 19:31:14 -0700 | [diff] [blame] | 1079 | ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf, dbuf, size, |
| 1080 | opt_flags); |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 1081 | |
Takashi Iwai | 4e0c92d | 2013-01-31 11:13:54 +0100 | [diff] [blame] | 1082 | /* |
| 1083 | * bind with 'buf' now to avoid warning in failure path |
| 1084 | * of requesting firmware. |
| 1085 | */ |
| 1086 | firmware->priv = buf; |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 1087 | |
Takashi Iwai | 4e0c92d | 2013-01-31 11:13:54 +0100 | [diff] [blame] | 1088 | if (ret > 0) { |
| 1089 | ret = sync_cached_firmware_buf(buf); |
| 1090 | if (!ret) { |
| 1091 | fw_set_page_data(buf, firmware); |
| 1092 | return 0; /* assigned */ |
| 1093 | } |
Stephen Boyd | dddb554 | 2012-03-28 23:30:43 +0200 | [diff] [blame] | 1094 | } |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 1095 | |
Takashi Iwai | 4e0c92d | 2013-01-31 11:13:54 +0100 | [diff] [blame] | 1096 | if (ret < 0) |
| 1097 | return ret; |
| 1098 | return 1; /* need to load */ |
Rafael J. Wysocki | 811fa40 | 2012-03-28 23:29:55 +0200 | [diff] [blame] | 1099 | } |
| 1100 | |
Ming Lei | e771d1a | 2013-05-27 18:30:03 +0800 | [diff] [blame] | 1101 | static int assign_firmware_buf(struct firmware *fw, struct device *device, |
Takashi Iwai | 14c4bae | 2013-12-02 15:38:18 +0100 | [diff] [blame] | 1102 | unsigned int opt_flags) |
Takashi Iwai | 4e0c92d | 2013-01-31 11:13:54 +0100 | [diff] [blame] | 1103 | { |
| 1104 | struct firmware_buf *buf = fw->priv; |
| 1105 | |
| 1106 | mutex_lock(&fw_lock); |
Takashi Iwai | 807be03 | 2013-01-31 11:13:57 +0100 | [diff] [blame] | 1107 | if (!buf->size || is_fw_load_aborted(buf)) { |
Takashi Iwai | 4e0c92d | 2013-01-31 11:13:54 +0100 | [diff] [blame] | 1108 | mutex_unlock(&fw_lock); |
| 1109 | return -ENOENT; |
| 1110 | } |
| 1111 | |
| 1112 | /* |
| 1113 | * add firmware name into devres list so that we can auto cache |
| 1114 | * and uncache firmware for device. |
| 1115 | * |
| 1116 | * device may has been deleted already, but the problem |
| 1117 | * should be fixed in devres or driver core. |
| 1118 | */ |
Takashi Iwai | 14c4bae | 2013-12-02 15:38:18 +0100 | [diff] [blame] | 1119 | /* don't cache firmware handled without uevent */ |
Vikram Mulukutla | 0e742e9 | 2016-08-02 14:04:25 -0700 | [diff] [blame] | 1120 | if (device && (opt_flags & FW_OPT_UEVENT) && |
| 1121 | !(opt_flags & FW_OPT_NOCACHE)) |
Takashi Iwai | 4e0c92d | 2013-01-31 11:13:54 +0100 | [diff] [blame] | 1122 | fw_add_devm_name(device, buf->fw_id); |
| 1123 | |
| 1124 | /* |
| 1125 | * After caching firmware image is started, let it piggyback |
| 1126 | * on request firmware. |
| 1127 | */ |
Vikram Mulukutla | 0e742e9 | 2016-08-02 14:04:25 -0700 | [diff] [blame] | 1128 | if (!(opt_flags & FW_OPT_NOCACHE) && |
| 1129 | buf->fwc->state == FW_LOADER_START_CACHE) { |
Takashi Iwai | 4e0c92d | 2013-01-31 11:13:54 +0100 | [diff] [blame] | 1130 | if (fw_cache_piggyback_on_request(buf->fw_id)) |
| 1131 | kref_get(&buf->ref); |
| 1132 | } |
| 1133 | |
| 1134 | /* pass the pages buffer to driver at the last minute */ |
| 1135 | fw_set_page_data(buf, fw); |
| 1136 | mutex_unlock(&fw_lock); |
| 1137 | return 0; |
| 1138 | } |
| 1139 | |
Takashi Iwai | 4e0c92d | 2013-01-31 11:13:54 +0100 | [diff] [blame] | 1140 | /* called from request_firmware() and request_firmware_work_func() */ |
| 1141 | static int |
| 1142 | _request_firmware(const struct firmware **firmware_p, const char *name, |
Stephen Boyd | a098ecd | 2016-08-02 14:04:28 -0700 | [diff] [blame] | 1143 | struct device *device, void *buf, size_t size, |
| 1144 | unsigned int opt_flags) |
Takashi Iwai | 4e0c92d | 2013-01-31 11:13:54 +0100 | [diff] [blame] | 1145 | { |
Brian Norris | 715780a | 2015-12-09 14:50:28 -0800 | [diff] [blame] | 1146 | struct firmware *fw = NULL; |
Takashi Iwai | 4e0c92d | 2013-01-31 11:13:54 +0100 | [diff] [blame] | 1147 | long timeout; |
| 1148 | int ret; |
| 1149 | |
| 1150 | if (!firmware_p) |
| 1151 | return -EINVAL; |
| 1152 | |
Brian Norris | 715780a | 2015-12-09 14:50:28 -0800 | [diff] [blame] | 1153 | if (!name || name[0] == '\0') { |
| 1154 | ret = -EINVAL; |
| 1155 | goto out; |
| 1156 | } |
Kees Cook | 471b095 | 2014-09-18 11:25:37 -0700 | [diff] [blame] | 1157 | |
Vikram Mulukutla | b19be88 | 2017-04-24 19:31:14 -0700 | [diff] [blame] | 1158 | ret = _request_firmware_prepare(&fw, name, device, buf, size, |
| 1159 | opt_flags); |
Takashi Iwai | 4e0c92d | 2013-01-31 11:13:54 +0100 | [diff] [blame] | 1160 | if (ret <= 0) /* error or already assigned */ |
| 1161 | goto out; |
| 1162 | |
| 1163 | ret = 0; |
| 1164 | timeout = firmware_loading_timeout(); |
Takashi Iwai | 14c4bae | 2013-12-02 15:38:18 +0100 | [diff] [blame] | 1165 | if (opt_flags & FW_OPT_NOWAIT) { |
Takashi Iwai | 4e0c92d | 2013-01-31 11:13:54 +0100 | [diff] [blame] | 1166 | timeout = usermodehelper_read_lock_wait(timeout); |
| 1167 | if (!timeout) { |
| 1168 | dev_dbg(device, "firmware: %s loading timed out\n", |
| 1169 | name); |
| 1170 | ret = -EBUSY; |
| 1171 | goto out; |
| 1172 | } |
| 1173 | } else { |
| 1174 | ret = usermodehelper_read_trylock(); |
| 1175 | if (WARN_ON(ret)) { |
| 1176 | dev_err(device, "firmware: %s will not be loaded\n", |
| 1177 | name); |
| 1178 | goto out; |
| 1179 | } |
| 1180 | } |
| 1181 | |
Neil Horman | 3e358ac | 2013-09-06 15:36:08 -0400 | [diff] [blame] | 1182 | ret = fw_get_filesystem_firmware(device, fw->priv); |
| 1183 | if (ret) { |
Luis R. Rodriguez | c868edf | 2014-07-02 09:55:05 -0700 | [diff] [blame] | 1184 | if (!(opt_flags & FW_OPT_NO_WARN)) |
Kyle Yan | 7e2a08d | 2017-05-09 15:40:17 -0700 | [diff] [blame] | 1185 | dev_dbg(device, |
| 1186 | "Firmware %s was not found in kernel paths. rc:%d\n", |
Luis R. Rodriguez | c868edf | 2014-07-02 09:55:05 -0700 | [diff] [blame] | 1187 | name, ret); |
| 1188 | if (opt_flags & FW_OPT_USERHELPER) { |
Kyle Yan | 7e2a08d | 2017-05-09 15:40:17 -0700 | [diff] [blame] | 1189 | dev_dbg(device, "Falling back to user helper\n"); |
Takashi Iwai | bba3a87 | 2013-12-02 15:38:16 +0100 | [diff] [blame] | 1190 | ret = fw_load_from_user_helper(fw, name, device, |
Takashi Iwai | 14c4bae | 2013-12-02 15:38:18 +0100 | [diff] [blame] | 1191 | opt_flags, timeout); |
Takashi Iwai | bba3a87 | 2013-12-02 15:38:16 +0100 | [diff] [blame] | 1192 | } |
Neil Horman | 3e358ac | 2013-09-06 15:36:08 -0400 | [diff] [blame] | 1193 | } |
Ming Lei | e771d1a | 2013-05-27 18:30:03 +0800 | [diff] [blame] | 1194 | |
Takashi Iwai | 4e0c92d | 2013-01-31 11:13:54 +0100 | [diff] [blame] | 1195 | if (!ret) |
Takashi Iwai | 14c4bae | 2013-12-02 15:38:18 +0100 | [diff] [blame] | 1196 | ret = assign_firmware_buf(fw, device, opt_flags); |
Takashi Iwai | 4e0c92d | 2013-01-31 11:13:54 +0100 | [diff] [blame] | 1197 | |
| 1198 | usermodehelper_read_unlock(); |
| 1199 | |
| 1200 | out: |
| 1201 | if (ret < 0) { |
| 1202 | release_firmware(fw); |
| 1203 | fw = NULL; |
| 1204 | } |
| 1205 | |
| 1206 | *firmware_p = fw; |
| 1207 | return ret; |
| 1208 | } |
| 1209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | /** |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 1211 | * request_firmware: - send firmware request and wait for it |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 1212 | * @firmware_p: pointer to firmware image |
| 1213 | * @name: name of firmware file |
| 1214 | * @device: device for which firmware is being loaded |
| 1215 | * |
| 1216 | * @firmware_p will be used to return a firmware image by the name |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 1217 | * of @name for device @device. |
| 1218 | * |
| 1219 | * Should be called from user context where sleeping is allowed. |
| 1220 | * |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 1221 | * @name will be used as $FIRMWARE in the uevent environment and |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 1222 | * should be distinctive enough not to be confused with any other |
| 1223 | * firmware image for this or any other device. |
Ming Lei | 0cfc1e1 | 2012-08-04 12:01:23 +0800 | [diff] [blame] | 1224 | * |
| 1225 | * Caller must hold the reference count of @device. |
Ming Lei | 6a92785 | 2012-11-03 17:48:16 +0800 | [diff] [blame] | 1226 | * |
| 1227 | * The function can be called safely inside device's suspend and |
| 1228 | * resume callback. |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 1229 | **/ |
| 1230 | int |
| 1231 | request_firmware(const struct firmware **firmware_p, const char *name, |
Andrei Oprea | ea31003 | 2015-03-08 12:41:15 +0200 | [diff] [blame] | 1232 | struct device *device) |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 1233 | { |
Ming Lei | d6c8aa3 | 2013-06-06 20:01:48 +0800 | [diff] [blame] | 1234 | int ret; |
| 1235 | |
| 1236 | /* Need to pin this module until return */ |
| 1237 | __module_get(THIS_MODULE); |
Stephen Boyd | a098ecd | 2016-08-02 14:04:28 -0700 | [diff] [blame] | 1238 | ret = _request_firmware(firmware_p, name, device, NULL, 0, |
Takashi Iwai | 14c4bae | 2013-12-02 15:38:18 +0100 | [diff] [blame] | 1239 | FW_OPT_UEVENT | FW_OPT_FALLBACK); |
Ming Lei | d6c8aa3 | 2013-06-06 20:01:48 +0800 | [diff] [blame] | 1240 | module_put(THIS_MODULE); |
| 1241 | return ret; |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 1242 | } |
Daniel Mack | f494513 | 2013-05-23 22:17:18 +0200 | [diff] [blame] | 1243 | EXPORT_SYMBOL(request_firmware); |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 1244 | |
Takashi Iwai | bba3a87 | 2013-12-02 15:38:16 +0100 | [diff] [blame] | 1245 | /** |
Borislav Petkov | 3c1556b | 2014-12-03 22:46:37 +0100 | [diff] [blame] | 1246 | * request_firmware_direct: - load firmware directly without usermode helper |
Takashi Iwai | bba3a87 | 2013-12-02 15:38:16 +0100 | [diff] [blame] | 1247 | * @firmware_p: pointer to firmware image |
| 1248 | * @name: name of firmware file |
| 1249 | * @device: device for which firmware is being loaded |
| 1250 | * |
| 1251 | * This function works pretty much like request_firmware(), but this doesn't |
| 1252 | * fall back to usermode helper even if the firmware couldn't be loaded |
| 1253 | * directly from fs. Hence it's useful for loading optional firmwares, which |
| 1254 | * aren't always present, without extra long timeouts of udev. |
| 1255 | **/ |
| 1256 | int request_firmware_direct(const struct firmware **firmware_p, |
| 1257 | const char *name, struct device *device) |
| 1258 | { |
| 1259 | int ret; |
Andrei Oprea | ea31003 | 2015-03-08 12:41:15 +0200 | [diff] [blame] | 1260 | |
Takashi Iwai | bba3a87 | 2013-12-02 15:38:16 +0100 | [diff] [blame] | 1261 | __module_get(THIS_MODULE); |
Stephen Boyd | a098ecd | 2016-08-02 14:04:28 -0700 | [diff] [blame] | 1262 | ret = _request_firmware(firmware_p, name, device, NULL, 0, |
Luis R. Rodriguez | c868edf | 2014-07-02 09:55:05 -0700 | [diff] [blame] | 1263 | FW_OPT_UEVENT | FW_OPT_NO_WARN); |
Takashi Iwai | bba3a87 | 2013-12-02 15:38:16 +0100 | [diff] [blame] | 1264 | module_put(THIS_MODULE); |
| 1265 | return ret; |
| 1266 | } |
| 1267 | EXPORT_SYMBOL_GPL(request_firmware_direct); |
Takashi Iwai | bba3a87 | 2013-12-02 15:38:16 +0100 | [diff] [blame] | 1268 | |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 1269 | /** |
Stephen Boyd | a098ecd | 2016-08-02 14:04:28 -0700 | [diff] [blame] | 1270 | * request_firmware_into_buf - load firmware into a previously allocated buffer |
| 1271 | * @firmware_p: pointer to firmware image |
| 1272 | * @name: name of firmware file |
| 1273 | * @device: device for which firmware is being loaded and DMA region allocated |
| 1274 | * @buf: address of buffer to load firmware into |
| 1275 | * @size: size of buffer |
| 1276 | * |
| 1277 | * This function works pretty much like request_firmware(), but it doesn't |
| 1278 | * allocate a buffer to hold the firmware data. Instead, the firmware |
| 1279 | * is loaded directly into the buffer pointed to by @buf and the @firmware_p |
| 1280 | * data member is pointed at @buf. |
| 1281 | * |
| 1282 | * This function doesn't cache firmware either. |
| 1283 | */ |
| 1284 | int |
| 1285 | request_firmware_into_buf(const struct firmware **firmware_p, const char *name, |
| 1286 | struct device *device, void *buf, size_t size) |
| 1287 | { |
| 1288 | int ret; |
| 1289 | |
| 1290 | __module_get(THIS_MODULE); |
| 1291 | ret = _request_firmware(firmware_p, name, device, buf, size, |
| 1292 | FW_OPT_UEVENT | FW_OPT_FALLBACK | |
| 1293 | FW_OPT_NOCACHE); |
| 1294 | module_put(THIS_MODULE); |
| 1295 | return ret; |
| 1296 | } |
| 1297 | EXPORT_SYMBOL(request_firmware_into_buf); |
| 1298 | |
| 1299 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | * release_firmware: - release the resource associated with a firmware image |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 1301 | * @fw: firmware resource to release |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | **/ |
Dmitry Torokhov | bcb9bd1 | 2010-03-13 23:49:18 -0800 | [diff] [blame] | 1303 | void release_firmware(const struct firmware *fw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | { |
| 1305 | if (fw) { |
Dmitry Torokhov | bcb9bd1 | 2010-03-13 23:49:18 -0800 | [diff] [blame] | 1306 | if (!fw_is_builtin_firmware(fw)) |
| 1307 | firmware_free_data(fw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | kfree(fw); |
| 1309 | } |
| 1310 | } |
Daniel Mack | f494513 | 2013-05-23 22:17:18 +0200 | [diff] [blame] | 1311 | EXPORT_SYMBOL(release_firmware); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | /* Async support */ |
| 1314 | struct firmware_work { |
| 1315 | struct work_struct work; |
| 1316 | struct module *module; |
| 1317 | const char *name; |
| 1318 | struct device *device; |
| 1319 | void *context; |
| 1320 | void (*cont)(const struct firmware *fw, void *context); |
Takashi Iwai | 14c4bae | 2013-12-02 15:38:18 +0100 | [diff] [blame] | 1321 | unsigned int opt_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | }; |
| 1323 | |
Stephen Boyd | a36cf84 | 2012-03-28 23:31:00 +0200 | [diff] [blame] | 1324 | static void request_firmware_work_func(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | { |
Stephen Boyd | a36cf84 | 2012-03-28 23:31:00 +0200 | [diff] [blame] | 1326 | struct firmware_work *fw_work; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | const struct firmware *fw; |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 1328 | |
Stephen Boyd | a36cf84 | 2012-03-28 23:31:00 +0200 | [diff] [blame] | 1329 | fw_work = container_of(work, struct firmware_work, work); |
Rafael J. Wysocki | 811fa40 | 2012-03-28 23:29:55 +0200 | [diff] [blame] | 1330 | |
Stephen Boyd | a098ecd | 2016-08-02 14:04:28 -0700 | [diff] [blame] | 1331 | _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0, |
Takashi Iwai | 14c4bae | 2013-12-02 15:38:18 +0100 | [diff] [blame] | 1332 | fw_work->opt_flags); |
Johannes Berg | 9ebfbd4 | 2009-10-29 12:36:02 +0100 | [diff] [blame] | 1333 | fw_work->cont(fw, fw_work->context); |
Takashi Iwai | 4e0c92d | 2013-01-31 11:13:54 +0100 | [diff] [blame] | 1334 | put_device(fw_work->device); /* taken in request_firmware_nowait() */ |
Johannes Berg | 9ebfbd4 | 2009-10-29 12:36:02 +0100 | [diff] [blame] | 1335 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | module_put(fw_work->module); |
Luis R. Rodriguez | f9692b2 | 2015-05-12 14:49:42 -0700 | [diff] [blame] | 1337 | kfree_const(fw_work->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | kfree(fw_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | /** |
Ben Hutchings | 3c31f07 | 2010-02-14 14:18:53 +0000 | [diff] [blame] | 1342 | * request_firmware_nowait - asynchronous version of request_firmware |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 1343 | * @module: module requesting the firmware |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 1344 | * @uevent: sends uevent to copy the firmware image if this flag |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 1345 | * is non-zero else the firmware copy must be done manually. |
| 1346 | * @name: name of firmware file |
| 1347 | * @device: device for which firmware is being loaded |
Johannes Berg | 9ebfbd4 | 2009-10-29 12:36:02 +0100 | [diff] [blame] | 1348 | * @gfp: allocation flags |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 1349 | * @context: will be passed over to @cont, and |
| 1350 | * @fw may be %NULL if firmware request fails. |
| 1351 | * @cont: function will be called asynchronously when the firmware |
| 1352 | * request is over. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | * |
Ming Lei | 0cfc1e1 | 2012-08-04 12:01:23 +0800 | [diff] [blame] | 1354 | * Caller must hold the reference count of @device. |
| 1355 | * |
Ming Lei | 6f21a62 | 2012-08-04 12:01:24 +0800 | [diff] [blame] | 1356 | * Asynchronous variant of request_firmware() for user contexts: |
| 1357 | * - sleep for as small periods as possible since it may |
| 1358 | * increase kernel boot time of built-in device drivers |
| 1359 | * requesting firmware in their ->probe() methods, if |
| 1360 | * @gfp is GFP_KERNEL. |
| 1361 | * |
| 1362 | * - can't sleep at all if @gfp is GFP_ATOMIC. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | **/ |
| 1364 | int |
| 1365 | request_firmware_nowait( |
Bob Liu | 072fc8f | 2011-01-26 18:33:32 +0800 | [diff] [blame] | 1366 | struct module *module, bool uevent, |
Johannes Berg | 9ebfbd4 | 2009-10-29 12:36:02 +0100 | [diff] [blame] | 1367 | const char *name, struct device *device, gfp_t gfp, void *context, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | void (*cont)(const struct firmware *fw, void *context)) |
| 1369 | { |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 1370 | struct firmware_work *fw_work; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | |
Andrei Oprea | ea31003 | 2015-03-08 12:41:15 +0200 | [diff] [blame] | 1372 | fw_work = kzalloc(sizeof(struct firmware_work), gfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | if (!fw_work) |
| 1374 | return -ENOMEM; |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 1375 | |
| 1376 | fw_work->module = module; |
Luis R. Rodriguez | f9692b2 | 2015-05-12 14:49:42 -0700 | [diff] [blame] | 1377 | fw_work->name = kstrdup_const(name, gfp); |
Luis R. Rodriguez | 303cda0 | 2015-05-28 17:46:42 -0700 | [diff] [blame] | 1378 | if (!fw_work->name) { |
| 1379 | kfree(fw_work); |
Luis R. Rodriguez | f9692b2 | 2015-05-12 14:49:42 -0700 | [diff] [blame] | 1380 | return -ENOMEM; |
Luis R. Rodriguez | 303cda0 | 2015-05-28 17:46:42 -0700 | [diff] [blame] | 1381 | } |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 1382 | fw_work->device = device; |
| 1383 | fw_work->context = context; |
| 1384 | fw_work->cont = cont; |
Takashi Iwai | 14c4bae | 2013-12-02 15:38:18 +0100 | [diff] [blame] | 1385 | fw_work->opt_flags = FW_OPT_NOWAIT | FW_OPT_FALLBACK | |
Takashi Iwai | 5a1379e | 2014-06-04 17:48:15 +0200 | [diff] [blame] | 1386 | (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER); |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 1387 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | if (!try_module_get(module)) { |
Luis R. Rodriguez | f9692b2 | 2015-05-12 14:49:42 -0700 | [diff] [blame] | 1389 | kfree_const(fw_work->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | kfree(fw_work); |
| 1391 | return -EFAULT; |
| 1392 | } |
| 1393 | |
Ming Lei | 0cfc1e1 | 2012-08-04 12:01:23 +0800 | [diff] [blame] | 1394 | get_device(fw_work->device); |
Stephen Boyd | a36cf84 | 2012-03-28 23:31:00 +0200 | [diff] [blame] | 1395 | INIT_WORK(&fw_work->work, request_firmware_work_func); |
| 1396 | schedule_work(&fw_work->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | return 0; |
| 1398 | } |
Daniel Mack | f494513 | 2013-05-23 22:17:18 +0200 | [diff] [blame] | 1399 | EXPORT_SYMBOL(request_firmware_nowait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | |
Tim Murray | 57c7ee1 | 2016-10-20 13:53:58 -0700 | [diff] [blame] | 1401 | #ifdef CONFIG_FW_CACHE |
Ming Lei | 90f89081 | 2013-06-20 12:30:16 +0800 | [diff] [blame] | 1402 | static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain); |
| 1403 | |
Ming Lei | 2887b39 | 2012-08-04 12:01:22 +0800 | [diff] [blame] | 1404 | /** |
| 1405 | * cache_firmware - cache one firmware image in kernel memory space |
| 1406 | * @fw_name: the firmware image name |
| 1407 | * |
| 1408 | * Cache firmware in kernel memory so that drivers can use it when |
| 1409 | * system isn't ready for them to request firmware image from userspace. |
| 1410 | * Once it returns successfully, driver can use request_firmware or its |
| 1411 | * nowait version to get the cached firmware without any interacting |
| 1412 | * with userspace |
| 1413 | * |
| 1414 | * Return 0 if the firmware image has been cached successfully |
| 1415 | * Return !0 otherwise |
| 1416 | * |
| 1417 | */ |
Ming Lei | 93232e4 | 2013-06-06 20:01:47 +0800 | [diff] [blame] | 1418 | static int cache_firmware(const char *fw_name) |
Ming Lei | 2887b39 | 2012-08-04 12:01:22 +0800 | [diff] [blame] | 1419 | { |
| 1420 | int ret; |
| 1421 | const struct firmware *fw; |
| 1422 | |
| 1423 | pr_debug("%s: %s\n", __func__, fw_name); |
| 1424 | |
| 1425 | ret = request_firmware(&fw, fw_name, NULL); |
| 1426 | if (!ret) |
| 1427 | kfree(fw); |
| 1428 | |
| 1429 | pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret); |
| 1430 | |
| 1431 | return ret; |
| 1432 | } |
| 1433 | |
Ming Lei | 6a2c123 | 2013-06-26 09:28:17 +0800 | [diff] [blame] | 1434 | static struct firmware_buf *fw_lookup_buf(const char *fw_name) |
| 1435 | { |
| 1436 | struct firmware_buf *tmp; |
| 1437 | struct firmware_cache *fwc = &fw_cache; |
| 1438 | |
| 1439 | spin_lock(&fwc->lock); |
| 1440 | tmp = __fw_lookup_buf(fw_name); |
| 1441 | spin_unlock(&fwc->lock); |
| 1442 | |
| 1443 | return tmp; |
| 1444 | } |
| 1445 | |
Ming Lei | 2887b39 | 2012-08-04 12:01:22 +0800 | [diff] [blame] | 1446 | /** |
| 1447 | * uncache_firmware - remove one cached firmware image |
| 1448 | * @fw_name: the firmware image name |
| 1449 | * |
| 1450 | * Uncache one firmware image which has been cached successfully |
| 1451 | * before. |
| 1452 | * |
| 1453 | * Return 0 if the firmware cache has been removed successfully |
| 1454 | * Return !0 otherwise |
| 1455 | * |
| 1456 | */ |
Ming Lei | 93232e4 | 2013-06-06 20:01:47 +0800 | [diff] [blame] | 1457 | static int uncache_firmware(const char *fw_name) |
Ming Lei | 2887b39 | 2012-08-04 12:01:22 +0800 | [diff] [blame] | 1458 | { |
| 1459 | struct firmware_buf *buf; |
| 1460 | struct firmware fw; |
| 1461 | |
| 1462 | pr_debug("%s: %s\n", __func__, fw_name); |
| 1463 | |
Stephen Boyd | a098ecd | 2016-08-02 14:04:28 -0700 | [diff] [blame] | 1464 | if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0)) |
Ming Lei | 2887b39 | 2012-08-04 12:01:22 +0800 | [diff] [blame] | 1465 | return 0; |
| 1466 | |
| 1467 | buf = fw_lookup_buf(fw_name); |
| 1468 | if (buf) { |
| 1469 | fw_free_buf(buf); |
| 1470 | return 0; |
| 1471 | } |
| 1472 | |
| 1473 | return -EINVAL; |
| 1474 | } |
| 1475 | |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1476 | static struct fw_cache_entry *alloc_fw_cache_entry(const char *name) |
| 1477 | { |
| 1478 | struct fw_cache_entry *fce; |
| 1479 | |
Luis R. Rodriguez | e0fd9b1 | 2015-05-12 14:49:43 -0700 | [diff] [blame] | 1480 | fce = kzalloc(sizeof(*fce), GFP_ATOMIC); |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1481 | if (!fce) |
| 1482 | goto exit; |
| 1483 | |
Luis R. Rodriguez | e0fd9b1 | 2015-05-12 14:49:43 -0700 | [diff] [blame] | 1484 | fce->name = kstrdup_const(name, GFP_ATOMIC); |
| 1485 | if (!fce->name) { |
| 1486 | kfree(fce); |
| 1487 | fce = NULL; |
| 1488 | goto exit; |
| 1489 | } |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1490 | exit: |
| 1491 | return fce; |
| 1492 | } |
| 1493 | |
Ming Lei | 373304f | 2012-10-09 12:01:01 +0800 | [diff] [blame] | 1494 | static int __fw_entry_found(const char *name) |
| 1495 | { |
| 1496 | struct firmware_cache *fwc = &fw_cache; |
| 1497 | struct fw_cache_entry *fce; |
| 1498 | |
| 1499 | list_for_each_entry(fce, &fwc->fw_names, list) { |
| 1500 | if (!strcmp(fce->name, name)) |
| 1501 | return 1; |
| 1502 | } |
| 1503 | return 0; |
| 1504 | } |
| 1505 | |
Ming Lei | ac39b3e | 2012-08-20 19:04:16 +0800 | [diff] [blame] | 1506 | static int fw_cache_piggyback_on_request(const char *name) |
| 1507 | { |
| 1508 | struct firmware_cache *fwc = &fw_cache; |
| 1509 | struct fw_cache_entry *fce; |
| 1510 | int ret = 0; |
| 1511 | |
| 1512 | spin_lock(&fwc->name_lock); |
Ming Lei | 373304f | 2012-10-09 12:01:01 +0800 | [diff] [blame] | 1513 | if (__fw_entry_found(name)) |
| 1514 | goto found; |
Ming Lei | ac39b3e | 2012-08-20 19:04:16 +0800 | [diff] [blame] | 1515 | |
| 1516 | fce = alloc_fw_cache_entry(name); |
| 1517 | if (fce) { |
| 1518 | ret = 1; |
| 1519 | list_add(&fce->list, &fwc->fw_names); |
| 1520 | pr_debug("%s: fw: %s\n", __func__, name); |
| 1521 | } |
| 1522 | found: |
| 1523 | spin_unlock(&fwc->name_lock); |
| 1524 | return ret; |
| 1525 | } |
| 1526 | |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1527 | static void free_fw_cache_entry(struct fw_cache_entry *fce) |
| 1528 | { |
Luis R. Rodriguez | e0fd9b1 | 2015-05-12 14:49:43 -0700 | [diff] [blame] | 1529 | kfree_const(fce->name); |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1530 | kfree(fce); |
| 1531 | } |
| 1532 | |
| 1533 | static void __async_dev_cache_fw_image(void *fw_entry, |
| 1534 | async_cookie_t cookie) |
| 1535 | { |
| 1536 | struct fw_cache_entry *fce = fw_entry; |
| 1537 | struct firmware_cache *fwc = &fw_cache; |
| 1538 | int ret; |
| 1539 | |
| 1540 | ret = cache_firmware(fce->name); |
Ming Lei | ac39b3e | 2012-08-20 19:04:16 +0800 | [diff] [blame] | 1541 | if (ret) { |
| 1542 | spin_lock(&fwc->name_lock); |
| 1543 | list_del(&fce->list); |
| 1544 | spin_unlock(&fwc->name_lock); |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1545 | |
Ming Lei | ac39b3e | 2012-08-20 19:04:16 +0800 | [diff] [blame] | 1546 | free_fw_cache_entry(fce); |
| 1547 | } |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | /* called with dev->devres_lock held */ |
| 1551 | static void dev_create_fw_entry(struct device *dev, void *res, |
| 1552 | void *data) |
| 1553 | { |
| 1554 | struct fw_name_devm *fwn = res; |
| 1555 | const char *fw_name = fwn->name; |
| 1556 | struct list_head *head = data; |
| 1557 | struct fw_cache_entry *fce; |
| 1558 | |
| 1559 | fce = alloc_fw_cache_entry(fw_name); |
| 1560 | if (fce) |
| 1561 | list_add(&fce->list, head); |
| 1562 | } |
| 1563 | |
| 1564 | static int devm_name_match(struct device *dev, void *res, |
| 1565 | void *match_data) |
| 1566 | { |
| 1567 | struct fw_name_devm *fwn = res; |
| 1568 | return (fwn->magic == (unsigned long)match_data); |
| 1569 | } |
| 1570 | |
Ming Lei | ab6dd8e | 2012-08-17 22:07:00 +0800 | [diff] [blame] | 1571 | static void dev_cache_fw_image(struct device *dev, void *data) |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1572 | { |
| 1573 | LIST_HEAD(todo); |
| 1574 | struct fw_cache_entry *fce; |
| 1575 | struct fw_cache_entry *fce_next; |
| 1576 | struct firmware_cache *fwc = &fw_cache; |
| 1577 | |
| 1578 | devres_for_each_res(dev, fw_name_devm_release, |
| 1579 | devm_name_match, &fw_cache, |
| 1580 | dev_create_fw_entry, &todo); |
| 1581 | |
| 1582 | list_for_each_entry_safe(fce, fce_next, &todo, list) { |
| 1583 | list_del(&fce->list); |
| 1584 | |
| 1585 | spin_lock(&fwc->name_lock); |
Ming Lei | 373304f | 2012-10-09 12:01:01 +0800 | [diff] [blame] | 1586 | /* only one cache entry for one firmware */ |
| 1587 | if (!__fw_entry_found(fce->name)) { |
Ming Lei | 373304f | 2012-10-09 12:01:01 +0800 | [diff] [blame] | 1588 | list_add(&fce->list, &fwc->fw_names); |
| 1589 | } else { |
| 1590 | free_fw_cache_entry(fce); |
| 1591 | fce = NULL; |
| 1592 | } |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1593 | spin_unlock(&fwc->name_lock); |
| 1594 | |
Ming Lei | 373304f | 2012-10-09 12:01:01 +0800 | [diff] [blame] | 1595 | if (fce) |
Ming Lei | d28d388 | 2012-10-09 12:01:04 +0800 | [diff] [blame] | 1596 | async_schedule_domain(__async_dev_cache_fw_image, |
| 1597 | (void *)fce, |
| 1598 | &fw_cache_domain); |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1599 | } |
| 1600 | } |
| 1601 | |
| 1602 | static void __device_uncache_fw_images(void) |
| 1603 | { |
| 1604 | struct firmware_cache *fwc = &fw_cache; |
| 1605 | struct fw_cache_entry *fce; |
| 1606 | |
| 1607 | spin_lock(&fwc->name_lock); |
| 1608 | while (!list_empty(&fwc->fw_names)) { |
| 1609 | fce = list_entry(fwc->fw_names.next, |
| 1610 | struct fw_cache_entry, list); |
| 1611 | list_del(&fce->list); |
| 1612 | spin_unlock(&fwc->name_lock); |
| 1613 | |
| 1614 | uncache_firmware(fce->name); |
| 1615 | free_fw_cache_entry(fce); |
| 1616 | |
| 1617 | spin_lock(&fwc->name_lock); |
| 1618 | } |
| 1619 | spin_unlock(&fwc->name_lock); |
| 1620 | } |
| 1621 | |
| 1622 | /** |
| 1623 | * device_cache_fw_images - cache devices' firmware |
| 1624 | * |
| 1625 | * If one device called request_firmware or its nowait version |
| 1626 | * successfully before, the firmware names are recored into the |
| 1627 | * device's devres link list, so device_cache_fw_images can call |
| 1628 | * cache_firmware() to cache these firmwares for the device, |
| 1629 | * then the device driver can load its firmwares easily at |
| 1630 | * time when system is not ready to complete loading firmware. |
| 1631 | */ |
| 1632 | static void device_cache_fw_images(void) |
| 1633 | { |
| 1634 | struct firmware_cache *fwc = &fw_cache; |
Ming Lei | ffe53f6 | 2012-08-04 12:01:28 +0800 | [diff] [blame] | 1635 | int old_timeout; |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1636 | DEFINE_WAIT(wait); |
| 1637 | |
| 1638 | pr_debug("%s\n", __func__); |
| 1639 | |
Ming Lei | 373304f | 2012-10-09 12:01:01 +0800 | [diff] [blame] | 1640 | /* cancel uncache work */ |
| 1641 | cancel_delayed_work_sync(&fwc->work); |
| 1642 | |
Ming Lei | ffe53f6 | 2012-08-04 12:01:28 +0800 | [diff] [blame] | 1643 | /* |
| 1644 | * use small loading timeout for caching devices' firmware |
| 1645 | * because all these firmware images have been loaded |
| 1646 | * successfully at lease once, also system is ready for |
| 1647 | * completing firmware loading now. The maximum size of |
| 1648 | * firmware in current distributions is about 2M bytes, |
| 1649 | * so 10 secs should be enough. |
| 1650 | */ |
| 1651 | old_timeout = loading_timeout; |
| 1652 | loading_timeout = 10; |
| 1653 | |
Ming Lei | ac39b3e | 2012-08-20 19:04:16 +0800 | [diff] [blame] | 1654 | mutex_lock(&fw_lock); |
| 1655 | fwc->state = FW_LOADER_START_CACHE; |
Ming Lei | ab6dd8e | 2012-08-17 22:07:00 +0800 | [diff] [blame] | 1656 | dpm_for_each_dev(NULL, dev_cache_fw_image); |
Ming Lei | ac39b3e | 2012-08-20 19:04:16 +0800 | [diff] [blame] | 1657 | mutex_unlock(&fw_lock); |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1658 | |
| 1659 | /* wait for completion of caching firmware for all devices */ |
Ming Lei | d28d388 | 2012-10-09 12:01:04 +0800 | [diff] [blame] | 1660 | async_synchronize_full_domain(&fw_cache_domain); |
Ming Lei | ffe53f6 | 2012-08-04 12:01:28 +0800 | [diff] [blame] | 1661 | |
| 1662 | loading_timeout = old_timeout; |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1663 | } |
| 1664 | |
| 1665 | /** |
| 1666 | * device_uncache_fw_images - uncache devices' firmware |
| 1667 | * |
| 1668 | * uncache all firmwares which have been cached successfully |
| 1669 | * by device_uncache_fw_images earlier |
| 1670 | */ |
| 1671 | static void device_uncache_fw_images(void) |
| 1672 | { |
| 1673 | pr_debug("%s\n", __func__); |
| 1674 | __device_uncache_fw_images(); |
| 1675 | } |
| 1676 | |
| 1677 | static void device_uncache_fw_images_work(struct work_struct *work) |
| 1678 | { |
| 1679 | device_uncache_fw_images(); |
| 1680 | } |
| 1681 | |
| 1682 | /** |
| 1683 | * device_uncache_fw_images_delay - uncache devices firmwares |
| 1684 | * @delay: number of milliseconds to delay uncache device firmwares |
| 1685 | * |
| 1686 | * uncache all devices's firmwares which has been cached successfully |
| 1687 | * by device_cache_fw_images after @delay milliseconds. |
| 1688 | */ |
| 1689 | static void device_uncache_fw_images_delay(unsigned long delay) |
| 1690 | { |
Shaibal Dutta | bce6618 | 2014-01-31 15:44:58 -0800 | [diff] [blame] | 1691 | queue_delayed_work(system_power_efficient_wq, &fw_cache.work, |
| 1692 | msecs_to_jiffies(delay)); |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1693 | } |
| 1694 | |
Ming Lei | 07646d9 | 2012-08-04 12:01:29 +0800 | [diff] [blame] | 1695 | static int fw_pm_notify(struct notifier_block *notify_block, |
| 1696 | unsigned long mode, void *unused) |
| 1697 | { |
| 1698 | switch (mode) { |
| 1699 | case PM_HIBERNATION_PREPARE: |
| 1700 | case PM_SUSPEND_PREPARE: |
Sebastian Capella | f8d5b9e | 2014-02-18 17:52:08 -0800 | [diff] [blame] | 1701 | case PM_RESTORE_PREPARE: |
Ming Lei | af5bc11 | 2013-05-27 18:30:04 +0800 | [diff] [blame] | 1702 | kill_requests_without_uevent(); |
Ming Lei | 07646d9 | 2012-08-04 12:01:29 +0800 | [diff] [blame] | 1703 | device_cache_fw_images(); |
| 1704 | break; |
| 1705 | |
| 1706 | case PM_POST_SUSPEND: |
| 1707 | case PM_POST_HIBERNATION: |
| 1708 | case PM_POST_RESTORE: |
Ming Lei | ac39b3e | 2012-08-20 19:04:16 +0800 | [diff] [blame] | 1709 | /* |
| 1710 | * In case that system sleep failed and syscore_suspend is |
| 1711 | * not called. |
| 1712 | */ |
| 1713 | mutex_lock(&fw_lock); |
| 1714 | fw_cache.state = FW_LOADER_NO_CACHE; |
| 1715 | mutex_unlock(&fw_lock); |
| 1716 | |
Ming Lei | 07646d9 | 2012-08-04 12:01:29 +0800 | [diff] [blame] | 1717 | device_uncache_fw_images_delay(10 * MSEC_PER_SEC); |
| 1718 | break; |
| 1719 | } |
| 1720 | |
| 1721 | return 0; |
| 1722 | } |
Ming Lei | 07646d9 | 2012-08-04 12:01:29 +0800 | [diff] [blame] | 1723 | |
Ming Lei | ac39b3e | 2012-08-20 19:04:16 +0800 | [diff] [blame] | 1724 | /* stop caching firmware once syscore_suspend is reached */ |
| 1725 | static int fw_suspend(void) |
| 1726 | { |
| 1727 | fw_cache.state = FW_LOADER_NO_CACHE; |
| 1728 | return 0; |
| 1729 | } |
| 1730 | |
| 1731 | static struct syscore_ops fw_syscore_ops = { |
| 1732 | .suspend = fw_suspend, |
| 1733 | }; |
Ming Lei | cfe016b | 2012-09-08 17:32:30 +0800 | [diff] [blame] | 1734 | #else |
| 1735 | static int fw_cache_piggyback_on_request(const char *name) |
| 1736 | { |
| 1737 | return 0; |
| 1738 | } |
| 1739 | #endif |
Ming Lei | ac39b3e | 2012-08-20 19:04:16 +0800 | [diff] [blame] | 1740 | |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1741 | static void __init fw_cache_init(void) |
| 1742 | { |
| 1743 | spin_lock_init(&fw_cache.lock); |
| 1744 | INIT_LIST_HEAD(&fw_cache.head); |
Ming Lei | cfe016b | 2012-09-08 17:32:30 +0800 | [diff] [blame] | 1745 | fw_cache.state = FW_LOADER_NO_CACHE; |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1746 | |
Tim Murray | 57c7ee1 | 2016-10-20 13:53:58 -0700 | [diff] [blame] | 1747 | #ifdef CONFIG_FW_CACHE |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1748 | spin_lock_init(&fw_cache.name_lock); |
| 1749 | INIT_LIST_HEAD(&fw_cache.fw_names); |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1750 | |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1751 | INIT_DELAYED_WORK(&fw_cache.work, |
| 1752 | device_uncache_fw_images_work); |
Ming Lei | 07646d9 | 2012-08-04 12:01:29 +0800 | [diff] [blame] | 1753 | |
| 1754 | fw_cache.pm_notify.notifier_call = fw_pm_notify; |
| 1755 | register_pm_notifier(&fw_cache.pm_notify); |
Ming Lei | ac39b3e | 2012-08-20 19:04:16 +0800 | [diff] [blame] | 1756 | |
| 1757 | register_syscore_ops(&fw_syscore_ops); |
Ming Lei | cfe016b | 2012-09-08 17:32:30 +0800 | [diff] [blame] | 1758 | #endif |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1759 | } |
| 1760 | |
Dmitry Torokhov | 673fae9 | 2010-03-13 23:49:13 -0800 | [diff] [blame] | 1761 | static int __init firmware_class_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | { |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 1763 | fw_cache_init(); |
Takashi Iwai | 7b1269f | 2013-01-31 11:13:55 +0100 | [diff] [blame] | 1764 | #ifdef CONFIG_FW_LOADER_USER_HELPER |
Takashi Iwai | fe30414 | 2013-05-22 18:28:38 +0200 | [diff] [blame] | 1765 | register_reboot_notifier(&fw_shutdown_nb); |
Dmitry Torokhov | 673fae9 | 2010-03-13 23:49:13 -0800 | [diff] [blame] | 1766 | return class_register(&firmware_class); |
Takashi Iwai | 7b1269f | 2013-01-31 11:13:55 +0100 | [diff] [blame] | 1767 | #else |
| 1768 | return 0; |
| 1769 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | } |
Dmitry Torokhov | 673fae9 | 2010-03-13 23:49:13 -0800 | [diff] [blame] | 1771 | |
| 1772 | static void __exit firmware_class_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | { |
Tim Murray | 57c7ee1 | 2016-10-20 13:53:58 -0700 | [diff] [blame] | 1774 | #ifdef CONFIG_FW_CACHE |
Ming Lei | ac39b3e | 2012-08-20 19:04:16 +0800 | [diff] [blame] | 1775 | unregister_syscore_ops(&fw_syscore_ops); |
Ming Lei | 07646d9 | 2012-08-04 12:01:29 +0800 | [diff] [blame] | 1776 | unregister_pm_notifier(&fw_cache.pm_notify); |
Ming Lei | cfe016b | 2012-09-08 17:32:30 +0800 | [diff] [blame] | 1777 | #endif |
Takashi Iwai | 7b1269f | 2013-01-31 11:13:55 +0100 | [diff] [blame] | 1778 | #ifdef CONFIG_FW_LOADER_USER_HELPER |
Takashi Iwai | fe30414 | 2013-05-22 18:28:38 +0200 | [diff] [blame] | 1779 | unregister_reboot_notifier(&fw_shutdown_nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | class_unregister(&firmware_class); |
Takashi Iwai | 7b1269f | 2013-01-31 11:13:55 +0100 | [diff] [blame] | 1781 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | } |
| 1783 | |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 1784 | fs_initcall(firmware_class_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | module_exit(firmware_class_exit); |