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