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