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