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