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