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