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