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