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