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