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