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