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