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