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