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