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