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