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> |
563d0757 | 19 | #include <linux/kthread.h> |
6e03a201 | 20 | #include <linux/highmem.h> |
1da177e4 LT |
21 | #include <linux/firmware.h> |
22 | #include "base.h" | |
23 | ||
e55c8790 GKH |
24 | #define to_dev(obj) container_of(obj, struct device, kobj) |
25 | ||
87d37a4f | 26 | MODULE_AUTHOR("Manuel Estrada Sainz"); |
1da177e4 LT |
27 | MODULE_DESCRIPTION("Multi purpose firmware loading support"); |
28 | MODULE_LICENSE("GPL"); | |
29 | ||
30 | enum { | |
31 | FW_STATUS_LOADING, | |
32 | FW_STATUS_DONE, | |
33 | FW_STATUS_ABORT, | |
1da177e4 LT |
34 | }; |
35 | ||
2f65168d | 36 | static int loading_timeout = 60; /* In seconds */ |
1da177e4 LT |
37 | |
38 | /* fw_lock could be moved to 'struct firmware_priv' but since it is just | |
39 | * guarding for corner cases a global lock should be OK */ | |
cad1e55d | 40 | static DEFINE_MUTEX(fw_lock); |
1da177e4 LT |
41 | |
42 | struct firmware_priv { | |
976821d7 | 43 | char *fw_id; |
1da177e4 LT |
44 | struct completion completion; |
45 | struct bin_attribute attr_data; | |
46 | struct firmware *fw; | |
47 | unsigned long status; | |
6e03a201 DW |
48 | struct page **pages; |
49 | int nr_pages; | |
50 | int page_array_size; | |
51 | const char *vdata; | |
1da177e4 LT |
52 | struct timer_list timeout; |
53 | }; | |
54 | ||
5658c769 DW |
55 | #ifdef CONFIG_FW_LOADER |
56 | extern struct builtin_fw __start_builtin_fw[]; | |
57 | extern struct builtin_fw __end_builtin_fw[]; | |
58 | #else /* Module case. Avoid ifdefs later; it'll all optimise out */ | |
59 | static struct builtin_fw *__start_builtin_fw; | |
60 | static struct builtin_fw *__end_builtin_fw; | |
61 | #endif | |
62 | ||
858119e1 | 63 | static void |
1da177e4 LT |
64 | fw_load_abort(struct firmware_priv *fw_priv) |
65 | { | |
66 | set_bit(FW_STATUS_ABORT, &fw_priv->status); | |
67 | wmb(); | |
68 | complete(&fw_priv->completion); | |
69 | } | |
70 | ||
71 | static ssize_t | |
72 | firmware_timeout_show(struct class *class, char *buf) | |
73 | { | |
74 | return sprintf(buf, "%d\n", loading_timeout); | |
75 | } | |
76 | ||
77 | /** | |
eb8e3179 RD |
78 | * firmware_timeout_store - set number of seconds to wait for firmware |
79 | * @class: device class pointer | |
80 | * @buf: buffer to scan for timeout value | |
81 | * @count: number of bytes in @buf | |
82 | * | |
1da177e4 | 83 | * Sets the number of seconds to wait for the firmware. Once |
eb8e3179 | 84 | * this expires an error will be returned to the driver and no |
1da177e4 LT |
85 | * firmware will be provided. |
86 | * | |
eb8e3179 | 87 | * Note: zero means 'wait forever'. |
1da177e4 LT |
88 | **/ |
89 | static ssize_t | |
90 | firmware_timeout_store(struct class *class, const char *buf, size_t count) | |
91 | { | |
92 | loading_timeout = simple_strtol(buf, NULL, 10); | |
b92eac01 SG |
93 | if (loading_timeout < 0) |
94 | loading_timeout = 0; | |
1da177e4 LT |
95 | return count; |
96 | } | |
97 | ||
98 | static CLASS_ATTR(timeout, 0644, firmware_timeout_show, firmware_timeout_store); | |
99 | ||
e55c8790 | 100 | static void fw_dev_release(struct device *dev); |
1da177e4 | 101 | |
7eff2e7a | 102 | static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env) |
1da177e4 | 103 | { |
e55c8790 | 104 | struct firmware_priv *fw_priv = dev_get_drvdata(dev); |
1da177e4 | 105 | |
7eff2e7a | 106 | if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id)) |
1da177e4 | 107 | return -ENOMEM; |
7eff2e7a | 108 | if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout)) |
6897089c | 109 | return -ENOMEM; |
1da177e4 LT |
110 | |
111 | return 0; | |
112 | } | |
113 | ||
1b81d663 AB |
114 | static struct class firmware_class = { |
115 | .name = "firmware", | |
e55c8790 GKH |
116 | .dev_uevent = firmware_uevent, |
117 | .dev_release = fw_dev_release, | |
1b81d663 AB |
118 | }; |
119 | ||
e55c8790 GKH |
120 | static ssize_t firmware_loading_show(struct device *dev, |
121 | struct device_attribute *attr, char *buf) | |
1da177e4 | 122 | { |
e55c8790 | 123 | struct firmware_priv *fw_priv = dev_get_drvdata(dev); |
1da177e4 LT |
124 | int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status); |
125 | return sprintf(buf, "%d\n", loading); | |
126 | } | |
127 | ||
6e03a201 DW |
128 | /* Some architectures don't have PAGE_KERNEL_RO */ |
129 | #ifndef PAGE_KERNEL_RO | |
130 | #define PAGE_KERNEL_RO PAGE_KERNEL | |
131 | #endif | |
1da177e4 | 132 | /** |
eb8e3179 | 133 | * firmware_loading_store - set value in the 'loading' control file |
e55c8790 | 134 | * @dev: device pointer |
af9997e4 | 135 | * @attr: device attribute pointer |
eb8e3179 RD |
136 | * @buf: buffer to scan for loading control value |
137 | * @count: number of bytes in @buf | |
138 | * | |
1da177e4 LT |
139 | * The relevant values are: |
140 | * | |
141 | * 1: Start a load, discarding any previous partial load. | |
eb8e3179 | 142 | * 0: Conclude the load and hand the data to the driver code. |
1da177e4 LT |
143 | * -1: Conclude the load with an error and discard any written data. |
144 | **/ | |
e55c8790 GKH |
145 | static ssize_t firmware_loading_store(struct device *dev, |
146 | struct device_attribute *attr, | |
147 | const char *buf, size_t count) | |
1da177e4 | 148 | { |
e55c8790 | 149 | struct firmware_priv *fw_priv = dev_get_drvdata(dev); |
1da177e4 | 150 | int loading = simple_strtol(buf, NULL, 10); |
6e03a201 | 151 | int i; |
1da177e4 LT |
152 | |
153 | switch (loading) { | |
154 | case 1: | |
cad1e55d | 155 | mutex_lock(&fw_lock); |
b92eac01 | 156 | if (!fw_priv->fw) { |
cad1e55d | 157 | mutex_unlock(&fw_lock); |
b92eac01 SG |
158 | break; |
159 | } | |
1da177e4 LT |
160 | vfree(fw_priv->fw->data); |
161 | fw_priv->fw->data = NULL; | |
6e03a201 DW |
162 | for (i = 0; i < fw_priv->nr_pages; i++) |
163 | __free_page(fw_priv->pages[i]); | |
164 | kfree(fw_priv->pages); | |
165 | fw_priv->pages = NULL; | |
166 | fw_priv->page_array_size = 0; | |
167 | fw_priv->nr_pages = 0; | |
1da177e4 | 168 | fw_priv->fw->size = 0; |
1da177e4 | 169 | set_bit(FW_STATUS_LOADING, &fw_priv->status); |
cad1e55d | 170 | mutex_unlock(&fw_lock); |
1da177e4 LT |
171 | break; |
172 | case 0: | |
173 | if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) { | |
6e03a201 DW |
174 | vfree(fw_priv->fw->data); |
175 | fw_priv->fw->data = vmap(fw_priv->pages, | |
176 | fw_priv->nr_pages, | |
177 | 0, PAGE_KERNEL_RO); | |
178 | if (!fw_priv->fw->data) { | |
179 | dev_err(dev, "%s: vmap() failed\n", __func__); | |
180 | goto err; | |
181 | } | |
182 | /* Pages will be freed by vfree() */ | |
183 | fw_priv->pages = NULL; | |
184 | fw_priv->page_array_size = 0; | |
185 | fw_priv->nr_pages = 0; | |
1da177e4 LT |
186 | complete(&fw_priv->completion); |
187 | clear_bit(FW_STATUS_LOADING, &fw_priv->status); | |
188 | break; | |
189 | } | |
190 | /* fallthrough */ | |
191 | default: | |
266a813c | 192 | dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading); |
1da177e4 LT |
193 | /* fallthrough */ |
194 | case -1: | |
6e03a201 | 195 | err: |
1da177e4 LT |
196 | fw_load_abort(fw_priv); |
197 | break; | |
198 | } | |
199 | ||
200 | return count; | |
201 | } | |
202 | ||
e55c8790 | 203 | static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store); |
1da177e4 LT |
204 | |
205 | static ssize_t | |
91a69029 | 206 | firmware_data_read(struct kobject *kobj, struct bin_attribute *bin_attr, |
1da177e4 LT |
207 | char *buffer, loff_t offset, size_t count) |
208 | { | |
e55c8790 GKH |
209 | struct device *dev = to_dev(kobj); |
210 | struct firmware_priv *fw_priv = dev_get_drvdata(dev); | |
1da177e4 | 211 | struct firmware *fw; |
f37e6617 | 212 | ssize_t ret_count; |
1da177e4 | 213 | |
cad1e55d | 214 | mutex_lock(&fw_lock); |
1da177e4 | 215 | fw = fw_priv->fw; |
b92eac01 | 216 | if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) { |
1da177e4 LT |
217 | ret_count = -ENODEV; |
218 | goto out; | |
219 | } | |
308975fa JS |
220 | if (offset > fw->size) { |
221 | ret_count = 0; | |
222 | goto out; | |
223 | } | |
6e03a201 DW |
224 | if (count > fw->size - offset) |
225 | count = fw->size - offset; | |
226 | ||
227 | ret_count = count; | |
228 | ||
229 | while (count) { | |
230 | void *page_data; | |
231 | int page_nr = offset >> PAGE_SHIFT; | |
232 | int page_ofs = offset & (PAGE_SIZE-1); | |
233 | int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count); | |
234 | ||
235 | page_data = kmap(fw_priv->pages[page_nr]); | |
236 | ||
237 | memcpy(buffer, page_data + page_ofs, page_cnt); | |
238 | ||
239 | kunmap(fw_priv->pages[page_nr]); | |
240 | buffer += page_cnt; | |
241 | offset += page_cnt; | |
242 | count -= page_cnt; | |
243 | } | |
1da177e4 | 244 | out: |
cad1e55d | 245 | mutex_unlock(&fw_lock); |
1da177e4 LT |
246 | return ret_count; |
247 | } | |
eb8e3179 | 248 | |
1da177e4 LT |
249 | static int |
250 | fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size) | |
251 | { | |
6e03a201 DW |
252 | int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT; |
253 | ||
254 | /* If the array of pages is too small, grow it... */ | |
255 | if (fw_priv->page_array_size < pages_needed) { | |
256 | int new_array_size = max(pages_needed, | |
257 | fw_priv->page_array_size * 2); | |
258 | struct page **new_pages; | |
259 | ||
260 | new_pages = kmalloc(new_array_size * sizeof(void *), | |
261 | GFP_KERNEL); | |
262 | if (!new_pages) { | |
263 | fw_load_abort(fw_priv); | |
264 | return -ENOMEM; | |
265 | } | |
266 | memcpy(new_pages, fw_priv->pages, | |
267 | fw_priv->page_array_size * sizeof(void *)); | |
268 | memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) * | |
269 | (new_array_size - fw_priv->page_array_size)); | |
270 | kfree(fw_priv->pages); | |
271 | fw_priv->pages = new_pages; | |
272 | fw_priv->page_array_size = new_array_size; | |
273 | } | |
1da177e4 | 274 | |
6e03a201 DW |
275 | while (fw_priv->nr_pages < pages_needed) { |
276 | fw_priv->pages[fw_priv->nr_pages] = | |
277 | alloc_page(GFP_KERNEL | __GFP_HIGHMEM); | |
1da177e4 | 278 | |
6e03a201 DW |
279 | if (!fw_priv->pages[fw_priv->nr_pages]) { |
280 | fw_load_abort(fw_priv); | |
281 | return -ENOMEM; | |
282 | } | |
283 | fw_priv->nr_pages++; | |
1da177e4 | 284 | } |
1da177e4 LT |
285 | return 0; |
286 | } | |
287 | ||
288 | /** | |
eb8e3179 | 289 | * firmware_data_write - write method for firmware |
e55c8790 | 290 | * @kobj: kobject for the device |
42e61f4a | 291 | * @bin_attr: bin_attr structure |
eb8e3179 RD |
292 | * @buffer: buffer being written |
293 | * @offset: buffer offset for write in total data store area | |
294 | * @count: buffer size | |
1da177e4 | 295 | * |
eb8e3179 | 296 | * Data written to the 'data' attribute will be later handed to |
1da177e4 LT |
297 | * the driver as a firmware image. |
298 | **/ | |
299 | static ssize_t | |
91a69029 | 300 | firmware_data_write(struct kobject *kobj, struct bin_attribute *bin_attr, |
1da177e4 LT |
301 | char *buffer, loff_t offset, size_t count) |
302 | { | |
e55c8790 GKH |
303 | struct device *dev = to_dev(kobj); |
304 | struct firmware_priv *fw_priv = dev_get_drvdata(dev); | |
1da177e4 LT |
305 | struct firmware *fw; |
306 | ssize_t retval; | |
307 | ||
308 | if (!capable(CAP_SYS_RAWIO)) | |
309 | return -EPERM; | |
b92eac01 | 310 | |
cad1e55d | 311 | mutex_lock(&fw_lock); |
1da177e4 | 312 | fw = fw_priv->fw; |
b92eac01 | 313 | if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) { |
1da177e4 LT |
314 | retval = -ENODEV; |
315 | goto out; | |
316 | } | |
317 | retval = fw_realloc_buffer(fw_priv, offset + count); | |
318 | if (retval) | |
319 | goto out; | |
320 | ||
1da177e4 | 321 | retval = count; |
6e03a201 DW |
322 | |
323 | while (count) { | |
324 | void *page_data; | |
325 | int page_nr = offset >> PAGE_SHIFT; | |
326 | int page_ofs = offset & (PAGE_SIZE - 1); | |
327 | int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count); | |
328 | ||
329 | page_data = kmap(fw_priv->pages[page_nr]); | |
330 | ||
331 | memcpy(page_data + page_ofs, buffer, page_cnt); | |
332 | ||
333 | kunmap(fw_priv->pages[page_nr]); | |
334 | buffer += page_cnt; | |
335 | offset += page_cnt; | |
336 | count -= page_cnt; | |
337 | } | |
338 | ||
339 | fw->size = max_t(size_t, offset, fw->size); | |
1da177e4 | 340 | out: |
cad1e55d | 341 | mutex_unlock(&fw_lock); |
1da177e4 LT |
342 | return retval; |
343 | } | |
eb8e3179 | 344 | |
1da177e4 | 345 | static struct bin_attribute firmware_attr_data_tmpl = { |
7b595756 | 346 | .attr = {.name = "data", .mode = 0644}, |
1da177e4 LT |
347 | .size = 0, |
348 | .read = firmware_data_read, | |
349 | .write = firmware_data_write, | |
350 | }; | |
351 | ||
e55c8790 | 352 | static void fw_dev_release(struct device *dev) |
1da177e4 | 353 | { |
e55c8790 | 354 | struct firmware_priv *fw_priv = dev_get_drvdata(dev); |
6e03a201 | 355 | int i; |
1da177e4 | 356 | |
6e03a201 DW |
357 | for (i = 0; i < fw_priv->nr_pages; i++) |
358 | __free_page(fw_priv->pages[i]); | |
359 | kfree(fw_priv->pages); | |
976821d7 | 360 | kfree(fw_priv->fw_id); |
1da177e4 | 361 | kfree(fw_priv); |
0f2f2221 | 362 | kfree(dev); |
1da177e4 LT |
363 | |
364 | module_put(THIS_MODULE); | |
365 | } | |
366 | ||
367 | static void | |
368 | firmware_class_timeout(u_long data) | |
369 | { | |
370 | struct firmware_priv *fw_priv = (struct firmware_priv *) data; | |
371 | fw_load_abort(fw_priv); | |
372 | } | |
373 | ||
e55c8790 GKH |
374 | static int fw_register_device(struct device **dev_p, const char *fw_name, |
375 | struct device *device) | |
1da177e4 LT |
376 | { |
377 | int retval; | |
4aed0644 | 378 | struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv), |
1da177e4 | 379 | GFP_KERNEL); |
e55c8790 | 380 | struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL); |
1da177e4 | 381 | |
e55c8790 | 382 | *dev_p = NULL; |
1da177e4 | 383 | |
e55c8790 | 384 | if (!fw_priv || !f_dev) { |
266a813c | 385 | dev_err(device, "%s: kmalloc failed\n", __func__); |
1da177e4 LT |
386 | retval = -ENOMEM; |
387 | goto error_kfree; | |
388 | } | |
1da177e4 LT |
389 | |
390 | init_completion(&fw_priv->completion); | |
391 | fw_priv->attr_data = firmware_attr_data_tmpl; | |
976821d7 SO |
392 | fw_priv->fw_id = kstrdup(fw_name, GFP_KERNEL); |
393 | if (!fw_priv->fw_id) { | |
394 | dev_err(device, "%s: Firmware name allocation failed\n", | |
395 | __func__); | |
396 | retval = -ENOMEM; | |
397 | goto error_kfree; | |
398 | } | |
1da177e4 LT |
399 | |
400 | fw_priv->timeout.function = firmware_class_timeout; | |
401 | fw_priv->timeout.data = (u_long) fw_priv; | |
402 | init_timer(&fw_priv->timeout); | |
403 | ||
acc0e90f | 404 | dev_set_name(f_dev, "%s", dev_name(device)); |
e55c8790 GKH |
405 | f_dev->parent = device; |
406 | f_dev->class = &firmware_class; | |
407 | dev_set_drvdata(f_dev, fw_priv); | |
f67f129e | 408 | dev_set_uevent_suppress(f_dev, 1); |
e55c8790 | 409 | retval = device_register(f_dev); |
1da177e4 | 410 | if (retval) { |
266a813c | 411 | dev_err(device, "%s: device_register failed\n", __func__); |
6acf70f0 | 412 | put_device(f_dev); |
0f2f2221 | 413 | return retval; |
1da177e4 | 414 | } |
e55c8790 | 415 | *dev_p = f_dev; |
1da177e4 LT |
416 | return 0; |
417 | ||
418 | error_kfree: | |
e55c8790 | 419 | kfree(f_dev); |
6acf70f0 | 420 | kfree(fw_priv); |
1da177e4 LT |
421 | return retval; |
422 | } | |
423 | ||
e55c8790 GKH |
424 | static int fw_setup_device(struct firmware *fw, struct device **dev_p, |
425 | const char *fw_name, struct device *device, | |
426 | int uevent) | |
1da177e4 | 427 | { |
e55c8790 | 428 | struct device *f_dev; |
1da177e4 LT |
429 | struct firmware_priv *fw_priv; |
430 | int retval; | |
431 | ||
e55c8790 GKH |
432 | *dev_p = NULL; |
433 | retval = fw_register_device(&f_dev, fw_name, device); | |
1da177e4 LT |
434 | if (retval) |
435 | goto out; | |
436 | ||
437 | /* Need to pin this module until class device is destroyed */ | |
438 | __module_get(THIS_MODULE); | |
439 | ||
e55c8790 | 440 | fw_priv = dev_get_drvdata(f_dev); |
1da177e4 LT |
441 | |
442 | fw_priv->fw = fw; | |
e55c8790 | 443 | retval = sysfs_create_bin_file(&f_dev->kobj, &fw_priv->attr_data); |
1da177e4 | 444 | if (retval) { |
266a813c | 445 | dev_err(device, "%s: sysfs_create_bin_file failed\n", __func__); |
1da177e4 LT |
446 | goto error_unreg; |
447 | } | |
448 | ||
e55c8790 | 449 | retval = device_create_file(f_dev, &dev_attr_loading); |
1da177e4 | 450 | if (retval) { |
266a813c | 451 | dev_err(device, "%s: device_create_file failed\n", __func__); |
1da177e4 LT |
452 | goto error_unreg; |
453 | } | |
454 | ||
312c004d | 455 | if (uevent) |
f67f129e | 456 | dev_set_uevent_suppress(f_dev, 0); |
e55c8790 | 457 | *dev_p = f_dev; |
1da177e4 LT |
458 | goto out; |
459 | ||
460 | error_unreg: | |
e55c8790 | 461 | device_unregister(f_dev); |
1da177e4 LT |
462 | out: |
463 | return retval; | |
464 | } | |
465 | ||
6e3eaab0 AS |
466 | static int |
467 | _request_firmware(const struct firmware **firmware_p, const char *name, | |
312c004d | 468 | struct device *device, int uevent) |
1da177e4 | 469 | { |
e55c8790 | 470 | struct device *f_dev; |
1da177e4 LT |
471 | struct firmware_priv *fw_priv; |
472 | struct firmware *firmware; | |
5658c769 | 473 | struct builtin_fw *builtin; |
1da177e4 LT |
474 | int retval; |
475 | ||
476 | if (!firmware_p) | |
477 | return -EINVAL; | |
478 | ||
4aed0644 | 479 | *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL); |
1da177e4 | 480 | if (!firmware) { |
266a813c BH |
481 | dev_err(device, "%s: kmalloc(struct firmware) failed\n", |
482 | __func__); | |
1da177e4 LT |
483 | retval = -ENOMEM; |
484 | goto out; | |
485 | } | |
1da177e4 | 486 | |
5658c769 DW |
487 | for (builtin = __start_builtin_fw; builtin != __end_builtin_fw; |
488 | builtin++) { | |
489 | if (strcmp(name, builtin->name)) | |
490 | continue; | |
266a813c BH |
491 | dev_info(device, "firmware: using built-in firmware %s\n", |
492 | name); | |
5658c769 DW |
493 | firmware->size = builtin->size; |
494 | firmware->data = builtin->data; | |
495 | return 0; | |
496 | } | |
497 | ||
498 | if (uevent) | |
266a813c | 499 | dev_info(device, "firmware: requesting %s\n", name); |
5658c769 | 500 | |
e55c8790 | 501 | retval = fw_setup_device(firmware, &f_dev, name, device, uevent); |
1da177e4 LT |
502 | if (retval) |
503 | goto error_kfree_fw; | |
504 | ||
e55c8790 | 505 | fw_priv = dev_get_drvdata(f_dev); |
1da177e4 | 506 | |
312c004d | 507 | if (uevent) { |
6e3eaab0 AS |
508 | if (loading_timeout > 0) { |
509 | fw_priv->timeout.expires = jiffies + loading_timeout * HZ; | |
510 | add_timer(&fw_priv->timeout); | |
511 | } | |
1da177e4 | 512 | |
e55c8790 | 513 | kobject_uevent(&f_dev->kobj, KOBJ_ADD); |
6e3eaab0 AS |
514 | wait_for_completion(&fw_priv->completion); |
515 | set_bit(FW_STATUS_DONE, &fw_priv->status); | |
516 | del_timer_sync(&fw_priv->timeout); | |
517 | } else | |
518 | wait_for_completion(&fw_priv->completion); | |
1da177e4 | 519 | |
cad1e55d | 520 | mutex_lock(&fw_lock); |
1da177e4 LT |
521 | if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) { |
522 | retval = -ENOENT; | |
523 | release_firmware(fw_priv->fw); | |
524 | *firmware_p = NULL; | |
525 | } | |
526 | fw_priv->fw = NULL; | |
cad1e55d | 527 | mutex_unlock(&fw_lock); |
e55c8790 | 528 | device_unregister(f_dev); |
1da177e4 LT |
529 | goto out; |
530 | ||
531 | error_kfree_fw: | |
532 | kfree(firmware); | |
533 | *firmware_p = NULL; | |
534 | out: | |
535 | return retval; | |
536 | } | |
537 | ||
6e3eaab0 | 538 | /** |
312c004d | 539 | * request_firmware: - send firmware request and wait for it |
eb8e3179 RD |
540 | * @firmware_p: pointer to firmware image |
541 | * @name: name of firmware file | |
542 | * @device: device for which firmware is being loaded | |
543 | * | |
544 | * @firmware_p will be used to return a firmware image by the name | |
6e3eaab0 AS |
545 | * of @name for device @device. |
546 | * | |
547 | * Should be called from user context where sleeping is allowed. | |
548 | * | |
312c004d | 549 | * @name will be used as $FIRMWARE in the uevent environment and |
6e3eaab0 AS |
550 | * should be distinctive enough not to be confused with any other |
551 | * firmware image for this or any other device. | |
552 | **/ | |
553 | int | |
554 | request_firmware(const struct firmware **firmware_p, const char *name, | |
555 | struct device *device) | |
556 | { | |
312c004d KS |
557 | int uevent = 1; |
558 | return _request_firmware(firmware_p, name, device, uevent); | |
6e3eaab0 AS |
559 | } |
560 | ||
1da177e4 LT |
561 | /** |
562 | * release_firmware: - release the resource associated with a firmware image | |
eb8e3179 | 563 | * @fw: firmware resource to release |
1da177e4 LT |
564 | **/ |
565 | void | |
566 | release_firmware(const struct firmware *fw) | |
567 | { | |
5658c769 DW |
568 | struct builtin_fw *builtin; |
569 | ||
1da177e4 | 570 | if (fw) { |
5658c769 DW |
571 | for (builtin = __start_builtin_fw; builtin != __end_builtin_fw; |
572 | builtin++) { | |
573 | if (fw->data == builtin->data) | |
574 | goto free_fw; | |
575 | } | |
1da177e4 | 576 | vfree(fw->data); |
5658c769 | 577 | free_fw: |
1da177e4 LT |
578 | kfree(fw); |
579 | } | |
580 | } | |
581 | ||
1da177e4 LT |
582 | /* Async support */ |
583 | struct firmware_work { | |
584 | struct work_struct work; | |
585 | struct module *module; | |
586 | const char *name; | |
587 | struct device *device; | |
588 | void *context; | |
589 | void (*cont)(const struct firmware *fw, void *context); | |
312c004d | 590 | int uevent; |
1da177e4 LT |
591 | }; |
592 | ||
593 | static int | |
594 | request_firmware_work_func(void *arg) | |
595 | { | |
596 | struct firmware_work *fw_work = arg; | |
597 | const struct firmware *fw; | |
113fab13 | 598 | int ret; |
1da177e4 LT |
599 | if (!arg) { |
600 | WARN_ON(1); | |
601 | return 0; | |
602 | } | |
113fab13 | 603 | ret = _request_firmware(&fw, fw_work->name, fw_work->device, |
312c004d | 604 | fw_work->uevent); |
113fab13 MC |
605 | if (ret < 0) |
606 | fw_work->cont(NULL, fw_work->context); | |
607 | else { | |
608 | fw_work->cont(fw, fw_work->context); | |
609 | release_firmware(fw); | |
610 | } | |
1da177e4 LT |
611 | module_put(fw_work->module); |
612 | kfree(fw_work); | |
113fab13 | 613 | return ret; |
1da177e4 LT |
614 | } |
615 | ||
616 | /** | |
eb8e3179 RD |
617 | * request_firmware_nowait: asynchronous version of request_firmware |
618 | * @module: module requesting the firmware | |
312c004d | 619 | * @uevent: sends uevent to copy the firmware image if this flag |
eb8e3179 RD |
620 | * is non-zero else the firmware copy must be done manually. |
621 | * @name: name of firmware file | |
622 | * @device: device for which firmware is being loaded | |
623 | * @context: will be passed over to @cont, and | |
624 | * @fw may be %NULL if firmware request fails. | |
625 | * @cont: function will be called asynchronously when the firmware | |
626 | * request is over. | |
1da177e4 | 627 | * |
7fcab099 ML |
628 | * Asynchronous variant of request_firmware() for user contexts where |
629 | * it is not possible to sleep for long time. It can't be called | |
630 | * in atomic contexts. | |
1da177e4 LT |
631 | **/ |
632 | int | |
633 | request_firmware_nowait( | |
312c004d | 634 | struct module *module, int uevent, |
1da177e4 LT |
635 | const char *name, struct device *device, void *context, |
636 | void (*cont)(const struct firmware *fw, void *context)) | |
637 | { | |
563d0757 | 638 | struct task_struct *task; |
1da177e4 LT |
639 | struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work), |
640 | GFP_ATOMIC); | |
1da177e4 LT |
641 | |
642 | if (!fw_work) | |
643 | return -ENOMEM; | |
644 | if (!try_module_get(module)) { | |
645 | kfree(fw_work); | |
646 | return -EFAULT; | |
647 | } | |
648 | ||
649 | *fw_work = (struct firmware_work) { | |
650 | .module = module, | |
651 | .name = name, | |
652 | .device = device, | |
653 | .context = context, | |
654 | .cont = cont, | |
312c004d | 655 | .uevent = uevent, |
1da177e4 LT |
656 | }; |
657 | ||
563d0757 SB |
658 | task = kthread_run(request_firmware_work_func, fw_work, |
659 | "firmware/%s", name); | |
1da177e4 | 660 | |
563d0757 | 661 | if (IS_ERR(task)) { |
1da177e4 | 662 | fw_work->cont(NULL, fw_work->context); |
113fab13 MC |
663 | module_put(fw_work->module); |
664 | kfree(fw_work); | |
563d0757 | 665 | return PTR_ERR(task); |
1da177e4 LT |
666 | } |
667 | return 0; | |
668 | } | |
669 | ||
670 | static int __init | |
671 | firmware_class_init(void) | |
672 | { | |
673 | int error; | |
674 | error = class_register(&firmware_class); | |
675 | if (error) { | |
2b3a302a | 676 | printk(KERN_ERR "%s: class_register failed\n", __func__); |
1da177e4 LT |
677 | return error; |
678 | } | |
679 | error = class_create_file(&firmware_class, &class_attr_timeout); | |
680 | if (error) { | |
681 | printk(KERN_ERR "%s: class_create_file failed\n", | |
2b3a302a | 682 | __func__); |
1da177e4 LT |
683 | class_unregister(&firmware_class); |
684 | } | |
685 | return error; | |
686 | ||
687 | } | |
688 | static void __exit | |
689 | firmware_class_exit(void) | |
690 | { | |
691 | class_unregister(&firmware_class); | |
692 | } | |
693 | ||
a30a6a2c | 694 | fs_initcall(firmware_class_init); |
1da177e4 LT |
695 | module_exit(firmware_class_exit); |
696 | ||
697 | EXPORT_SYMBOL(release_firmware); | |
698 | EXPORT_SYMBOL(request_firmware); | |
699 | EXPORT_SYMBOL(request_firmware_nowait); |