blob: d7872b96019d22db197554bfa9ae2102a96f1ba4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * firmware_class.c - Multi purpose firmware loading support
3 *
Markus Rechberger87d37a42007-06-04 18:45:44 +02004 * Copyright (c) 2003 Manuel Estrada Sainz
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Please see Documentation/firmware_class/ for more information.
7 *
8 */
9
Randy.Dunlapc59ede72006-01-11 12:17:46 -080010#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#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>
Laura Garciacad1e552006-05-23 23:22:38 +020018#include <linux/mutex.h>
Stephen Boyda36cf842012-03-28 23:31:00 +020019#include <linux/workqueue.h>
David Woodhouse6e03a202009-04-09 22:04:07 -070020#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/firmware.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Stephen Boyda36cf842012-03-28 23:31:00 +020023#include <linux/sched.h>
Linus Torvaldsabb139e2012-10-03 15:58:32 -070024#include <linux/file.h>
Ming Lei1f2b7952012-08-04 12:01:21 +080025#include <linux/list.h>
Ming Lei37276a52012-08-04 12:01:27 +080026#include <linux/async.h>
27#include <linux/pm.h>
Ming Lei07646d92012-08-04 12:01:29 +080028#include <linux/suspend.h>
Ming Leiac39b3e2012-08-20 19:04:16 +080029#include <linux/syscore_ops.h>
Takashi Iwai28cb7112013-05-22 18:28:38 +020030#include <linux/reboot.h>
Ming Lei37276a52012-08-04 12:01:27 +080031
Linus Torvaldsabb139e2012-10-03 15:58:32 -070032#include <generated/utsrelease.h>
33
Ming Lei37276a52012-08-04 12:01:27 +080034#include "base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Markus Rechberger87d37a42007-06-04 18:45:44 +020036MODULE_AUTHOR("Manuel Estrada Sainz");
Linus Torvalds1da177e2005-04-16 15:20:36 -070037MODULE_DESCRIPTION("Multi purpose firmware loading support");
38MODULE_LICENSE("GPL");
39
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080040/* Builtin firmware support */
41
42#ifdef CONFIG_FW_LOADER
43
44extern struct builtin_fw __start_builtin_fw[];
45extern struct builtin_fw __end_builtin_fw[];
46
47static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
48{
49 struct builtin_fw *b_fw;
50
51 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
52 if (strcmp(name, b_fw->name) == 0) {
53 fw->size = b_fw->size;
54 fw->data = b_fw->data;
55 return true;
56 }
57 }
58
59 return false;
60}
61
62static bool fw_is_builtin_firmware(const struct firmware *fw)
63{
64 struct builtin_fw *b_fw;
65
66 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
67 if (fw->data == b_fw->data)
68 return true;
69
70 return false;
71}
72
73#else /* Module case - no builtin firmware support */
74
75static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
76{
77 return false;
78}
79
80static inline bool fw_is_builtin_firmware(const struct firmware *fw)
81{
82 return false;
83}
84#endif
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086enum {
87 FW_STATUS_LOADING,
88 FW_STATUS_DONE,
89 FW_STATUS_ABORT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090};
91
Dave Jones2f651682007-01-25 15:56:15 -050092static int loading_timeout = 60; /* In seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +020094static inline long firmware_loading_timeout(void)
95{
96 return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
97}
98
Ming Lei1f2b7952012-08-04 12:01:21 +080099struct firmware_cache {
100 /* firmware_buf instance will be added into the below list */
101 spinlock_t lock;
102 struct list_head head;
Ming Leicfe016b2012-09-08 17:32:30 +0800103 int state;
Ming Lei37276a52012-08-04 12:01:27 +0800104
Ming Leicfe016b2012-09-08 17:32:30 +0800105#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +0800106 /*
107 * Names of firmware images which have been cached successfully
108 * will be added into the below list so that device uncache
109 * helper can trace which firmware images have been cached
110 * before.
111 */
112 spinlock_t name_lock;
113 struct list_head fw_names;
114
Ming Lei37276a52012-08-04 12:01:27 +0800115 struct delayed_work work;
Ming Lei07646d92012-08-04 12:01:29 +0800116
117 struct notifier_block pm_notify;
Ming Leicfe016b2012-09-08 17:32:30 +0800118#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800119};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Ming Lei12446912012-08-04 12:01:20 +0800121struct firmware_buf {
Ming Lei1f2b7952012-08-04 12:01:21 +0800122 struct kref ref;
123 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 struct completion completion;
Ming Lei1f2b7952012-08-04 12:01:21 +0800125 struct firmware_cache *fwc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 unsigned long status;
Ming Lei65710cb2012-08-04 12:01:16 +0800127 void *data;
128 size_t size;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100129#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100130 bool is_paged_buf;
David Woodhouse6e03a202009-04-09 22:04:07 -0700131 struct page **pages;
132 int nr_pages;
133 int page_array_size;
Takashi Iwai28cb7112013-05-22 18:28:38 +0200134 struct list_head pending_list;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100135#endif
Dmitry Torokhove1771232010-03-13 23:49:23 -0800136 char fw_id[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137};
138
Ming Lei37276a52012-08-04 12:01:27 +0800139struct fw_cache_entry {
140 struct list_head list;
141 char name[];
142};
143
Ming Leif531f05a2012-08-04 12:01:25 +0800144struct fw_name_devm {
145 unsigned long magic;
146 char name[];
147};
148
Ming Lei1f2b7952012-08-04 12:01:21 +0800149#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
150
Ming Leiac39b3e2012-08-20 19:04:16 +0800151#define FW_LOADER_NO_CACHE 0
152#define FW_LOADER_START_CACHE 1
153
154static int fw_cache_piggyback_on_request(const char *name);
155
Ming Lei1f2b7952012-08-04 12:01:21 +0800156/* fw_lock could be moved to 'struct firmware_priv' but since it is just
157 * guarding for corner cases a global lock should be OK */
158static DEFINE_MUTEX(fw_lock);
159
160static struct firmware_cache fw_cache;
161
162static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
163 struct firmware_cache *fwc)
164{
165 struct firmware_buf *buf;
166
167 buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
168
169 if (!buf)
170 return buf;
171
172 kref_init(&buf->ref);
173 strcpy(buf->fw_id, fw_name);
174 buf->fwc = fwc;
175 init_completion(&buf->completion);
Takashi Iwai28cb7112013-05-22 18:28:38 +0200176#ifdef CONFIG_FW_LOADER_USER_HELPER
177 INIT_LIST_HEAD(&buf->pending_list);
178#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800179
180 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
181
182 return buf;
183}
184
Ming Lei2887b392012-08-04 12:01:22 +0800185static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
186{
187 struct firmware_buf *tmp;
188 struct firmware_cache *fwc = &fw_cache;
189
190 list_for_each_entry(tmp, &fwc->head, list)
191 if (!strcmp(tmp->fw_id, fw_name))
192 return tmp;
193 return NULL;
194}
195
Ming Lei1f2b7952012-08-04 12:01:21 +0800196static int fw_lookup_and_allocate_buf(const char *fw_name,
197 struct firmware_cache *fwc,
198 struct firmware_buf **buf)
199{
200 struct firmware_buf *tmp;
201
202 spin_lock(&fwc->lock);
Ming Lei2887b392012-08-04 12:01:22 +0800203 tmp = __fw_lookup_buf(fw_name);
204 if (tmp) {
205 kref_get(&tmp->ref);
206 spin_unlock(&fwc->lock);
207 *buf = tmp;
208 return 1;
209 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800210 tmp = __allocate_fw_buf(fw_name, fwc);
211 if (tmp)
212 list_add(&tmp->list, &fwc->head);
213 spin_unlock(&fwc->lock);
214
215 *buf = tmp;
216
217 return tmp ? 0 : -ENOMEM;
218}
219
Ming Lei2887b392012-08-04 12:01:22 +0800220static struct firmware_buf *fw_lookup_buf(const char *fw_name)
221{
222 struct firmware_buf *tmp;
223 struct firmware_cache *fwc = &fw_cache;
224
225 spin_lock(&fwc->lock);
226 tmp = __fw_lookup_buf(fw_name);
227 spin_unlock(&fwc->lock);
228
229 return tmp;
230}
231
Ming Lei1f2b7952012-08-04 12:01:21 +0800232static void __fw_free_buf(struct kref *ref)
233{
234 struct firmware_buf *buf = to_fwbuf(ref);
235 struct firmware_cache *fwc = buf->fwc;
Ming Lei1f2b7952012-08-04 12:01:21 +0800236
237 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
238 __func__, buf->fw_id, buf, buf->data,
239 (unsigned int)buf->size);
240
Ming Lei1f2b7952012-08-04 12:01:21 +0800241 list_del(&buf->list);
242 spin_unlock(&fwc->lock);
243
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100244#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100245 if (buf->is_paged_buf) {
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100246 int i;
Ming Lei746058f2012-10-09 12:01:03 +0800247 vunmap(buf->data);
248 for (i = 0; i < buf->nr_pages; i++)
249 __free_page(buf->pages[i]);
250 kfree(buf->pages);
251 } else
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100252#endif
Ming Lei746058f2012-10-09 12:01:03 +0800253 vfree(buf->data);
Ming Lei1f2b7952012-08-04 12:01:21 +0800254 kfree(buf);
255}
256
257static void fw_free_buf(struct firmware_buf *buf)
258{
Chuansheng Liubd9eb7f2012-11-10 01:27:22 +0800259 struct firmware_cache *fwc = buf->fwc;
260 spin_lock(&fwc->lock);
261 if (!kref_put(&buf->ref, __fw_free_buf))
262 spin_unlock(&fwc->lock);
Ming Lei1f2b7952012-08-04 12:01:21 +0800263}
264
Ming Lei746058f2012-10-09 12:01:03 +0800265/* direct firmware loading support */
Ming Lei27602842012-11-03 17:47:58 +0800266static char fw_path_para[256];
267static const char * const fw_path[] = {
268 fw_path_para,
Ming Lei746058f2012-10-09 12:01:03 +0800269 "/lib/firmware/updates/" UTS_RELEASE,
270 "/lib/firmware/updates",
271 "/lib/firmware/" UTS_RELEASE,
272 "/lib/firmware"
273};
274
Ming Lei27602842012-11-03 17:47:58 +0800275/*
276 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
277 * from kernel command line because firmware_class is generally built in
278 * kernel instead of module.
279 */
280module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
281MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
282
Ming Lei746058f2012-10-09 12:01:03 +0800283/* Don't inline this: 'struct kstat' is biggish */
Cesar Eduardo Barros60dac5e2012-10-27 20:37:10 -0200284static noinline_for_stack long fw_file_size(struct file *file)
Ming Lei746058f2012-10-09 12:01:03 +0800285{
286 struct kstat st;
Al Viro3dadecce2013-01-24 02:18:08 -0500287 if (vfs_getattr(&file->f_path, &st))
Ming Lei746058f2012-10-09 12:01:03 +0800288 return -1;
289 if (!S_ISREG(st.mode))
290 return -1;
291 if (st.size != (long)st.size)
292 return -1;
293 return st.size;
294}
295
296static bool fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf)
297{
298 long size;
299 char *buf;
300
301 size = fw_file_size(file);
Luciano Coelho4adf07f2013-01-15 10:43:43 +0200302 if (size <= 0)
Ming Lei746058f2012-10-09 12:01:03 +0800303 return false;
304 buf = vmalloc(size);
305 if (!buf)
306 return false;
307 if (kernel_read(file, 0, buf, size) != size) {
308 vfree(buf);
309 return false;
310 }
311 fw_buf->data = buf;
312 fw_buf->size = size;
313 return true;
314}
315
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100316static bool fw_get_filesystem_firmware(struct device *device,
317 struct firmware_buf *buf)
Ming Lei746058f2012-10-09 12:01:03 +0800318{
319 int i;
320 bool success = false;
321 char *path = __getname();
322
323 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
324 struct file *file;
Ming Lei27602842012-11-03 17:47:58 +0800325
326 /* skip the unset customized path */
327 if (!fw_path[i][0])
328 continue;
329
Ming Lei746058f2012-10-09 12:01:03 +0800330 snprintf(path, PATH_MAX, "%s/%s", fw_path[i], buf->fw_id);
331
332 file = filp_open(path, O_RDONLY, 0);
333 if (IS_ERR(file))
334 continue;
335 success = fw_read_file_contents(file, buf);
336 fput(file);
337 if (success)
338 break;
339 }
340 __putname(path);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100341
342 if (success) {
343 dev_dbg(device, "firmware: direct-loading firmware %s\n",
344 buf->fw_id);
345 mutex_lock(&fw_lock);
346 set_bit(FW_STATUS_DONE, &buf->status);
347 complete_all(&buf->completion);
348 mutex_unlock(&fw_lock);
349 }
350
Ming Lei746058f2012-10-09 12:01:03 +0800351 return success;
352}
353
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100354/* firmware holds the ownership of pages */
355static void firmware_free_data(const struct firmware *fw)
356{
357 /* Loaded directly? */
358 if (!fw->priv) {
359 vfree(fw->data);
360 return;
361 }
362 fw_free_buf(fw->priv);
363}
364
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100365/* store the pages buffer info firmware from buf */
366static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
367{
368 fw->priv = buf;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100369#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100370 fw->pages = buf->pages;
371#endif
372 fw->size = buf->size;
373 fw->data = buf->data;
374
375 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
376 __func__, buf->fw_id, buf, buf->data,
377 (unsigned int)buf->size);
378}
379
380#ifdef CONFIG_PM_SLEEP
381static void fw_name_devm_release(struct device *dev, void *res)
382{
383 struct fw_name_devm *fwn = res;
384
385 if (fwn->magic == (unsigned long)&fw_cache)
386 pr_debug("%s: fw_name-%s devm-%p released\n",
387 __func__, fwn->name, res);
388}
389
390static int fw_devm_match(struct device *dev, void *res,
391 void *match_data)
392{
393 struct fw_name_devm *fwn = res;
394
395 return (fwn->magic == (unsigned long)&fw_cache) &&
396 !strcmp(fwn->name, match_data);
397}
398
399static struct fw_name_devm *fw_find_devm_name(struct device *dev,
400 const char *name)
401{
402 struct fw_name_devm *fwn;
403
404 fwn = devres_find(dev, fw_name_devm_release,
405 fw_devm_match, (void *)name);
406 return fwn;
407}
408
409/* add firmware name into devres list */
410static int fw_add_devm_name(struct device *dev, const char *name)
411{
412 struct fw_name_devm *fwn;
413
414 fwn = fw_find_devm_name(dev, name);
415 if (fwn)
416 return 1;
417
418 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
419 strlen(name) + 1, GFP_KERNEL);
420 if (!fwn)
421 return -ENOMEM;
422
423 fwn->magic = (unsigned long)&fw_cache;
424 strcpy(fwn->name, name);
425 devres_add(dev, fwn);
426
427 return 0;
428}
429#else
430static int fw_add_devm_name(struct device *dev, const char *name)
431{
432 return 0;
433}
434#endif
435
436
437/*
438 * user-mode helper code
439 */
440#ifdef CONFIG_FW_LOADER_USER_HELPER
441struct firmware_priv {
442 struct delayed_work timeout_work;
443 bool nowait;
444 struct device dev;
445 struct firmware_buf *buf;
446 struct firmware *fw;
447};
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100448
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700449static struct firmware_priv *to_firmware_priv(struct device *dev)
450{
451 return container_of(dev, struct firmware_priv, dev);
452}
453
Takashi Iwai28cb7112013-05-22 18:28:38 +0200454static void __fw_load_abort(struct firmware_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Ming Lei87597932013-06-15 16:36:38 +0800456 /*
457 * There is a small window in which user can write to 'loading'
458 * between loading done and disappearance of 'loading'
459 */
460 if (test_bit(FW_STATUS_DONE, &buf->status))
461 return;
462
Takashi Iwai28cb7112013-05-22 18:28:38 +0200463 list_del_init(&buf->pending_list);
Ming Lei12446912012-08-04 12:01:20 +0800464 set_bit(FW_STATUS_ABORT, &buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800465 complete_all(&buf->completion);
Takashi Iwai28cb7112013-05-22 18:28:38 +0200466}
467
468static void fw_load_abort(struct firmware_priv *fw_priv)
469{
470 struct firmware_buf *buf = fw_priv->buf;
471
472 __fw_load_abort(buf);
Ming Lei87597932013-06-15 16:36:38 +0800473
474 /* avoid user action after loading abort */
475 fw_priv->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Takashi Iwai807be032013-01-31 11:13:57 +0100478#define is_fw_load_aborted(buf) \
479 test_bit(FW_STATUS_ABORT, &(buf)->status)
480
Takashi Iwai28cb7112013-05-22 18:28:38 +0200481static LIST_HEAD(pending_fw_head);
482
483/* reboot notifier for avoid deadlock with usermode_lock */
484static int fw_shutdown_notify(struct notifier_block *unused1,
485 unsigned long unused2, void *unused3)
486{
487 mutex_lock(&fw_lock);
488 while (!list_empty(&pending_fw_head))
489 __fw_load_abort(list_first_entry(&pending_fw_head,
490 struct firmware_buf,
491 pending_list));
492 mutex_unlock(&fw_lock);
493 return NOTIFY_DONE;
494}
495
496static struct notifier_block fw_shutdown_nb = {
497 .notifier_call = fw_shutdown_notify,
498};
499
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700500static ssize_t firmware_timeout_show(struct class *class,
501 struct class_attribute *attr,
502 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 return sprintf(buf, "%d\n", loading_timeout);
505}
506
507/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800508 * firmware_timeout_store - set number of seconds to wait for firmware
509 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800510 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800511 * @buf: buffer to scan for timeout value
512 * @count: number of bytes in @buf
513 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800515 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 * firmware will be provided.
517 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800518 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700520static ssize_t firmware_timeout_store(struct class *class,
521 struct class_attribute *attr,
522 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
524 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700525 if (loading_timeout < 0)
526 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return count;
529}
530
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800531static struct class_attribute firmware_class_attrs[] = {
532 __ATTR(timeout, S_IWUSR | S_IRUGO,
533 firmware_timeout_show, firmware_timeout_store),
534 __ATTR_NULL
535};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800537static void fw_dev_release(struct device *dev)
538{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700539 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800540
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800541 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800542
543 module_put(THIS_MODULE);
544}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Kay Sievers7eff2e72007-08-14 15:15:12 +0200546static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700548 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Ming Lei12446912012-08-04 12:01:20 +0800550 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200552 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700553 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200554 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
555 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 return 0;
558}
559
Adrian Bunk1b81d662006-05-20 15:00:16 -0700560static struct class firmware_class = {
561 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800562 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700563 .dev_uevent = firmware_uevent,
564 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700565};
566
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700567static ssize_t firmware_loading_show(struct device *dev,
568 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700570 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800571 int loading = 0;
572
573 mutex_lock(&fw_lock);
574 if (fw_priv->buf)
575 loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
576 mutex_unlock(&fw_lock);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return sprintf(buf, "%d\n", loading);
579}
580
David Woodhouse6e03a202009-04-09 22:04:07 -0700581/* Some architectures don't have PAGE_KERNEL_RO */
582#ifndef PAGE_KERNEL_RO
583#define PAGE_KERNEL_RO PAGE_KERNEL
584#endif
Ming Lei253c9242012-10-09 12:01:02 +0800585
586/* one pages buffer should be mapped/unmapped only once */
587static int fw_map_pages_buf(struct firmware_buf *buf)
588{
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100589 if (!buf->is_paged_buf)
Ming Lei746058f2012-10-09 12:01:03 +0800590 return 0;
591
Ming Lei253c9242012-10-09 12:01:02 +0800592 if (buf->data)
593 vunmap(buf->data);
594 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
595 if (!buf->data)
596 return -ENOMEM;
597 return 0;
598}
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800601 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700602 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800603 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800604 * @buf: buffer to scan for loading control value
605 * @count: number of bytes in @buf
606 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 * The relevant values are:
608 *
609 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800610 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 * -1: Conclude the load with an error and discard any written data.
612 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700613static ssize_t firmware_loading_store(struct device *dev,
614 struct device_attribute *attr,
615 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700617 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800618 struct firmware_buf *fw_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700620 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Neil Hormaneea915b2012-01-02 15:31:23 -0500622 mutex_lock(&fw_lock);
Ming Lei87597932013-06-15 16:36:38 +0800623 fw_buf = fw_priv->buf;
Ming Lei12446912012-08-04 12:01:20 +0800624 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500625 goto out;
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 switch (loading) {
628 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800629 /* discarding any previous partial load */
Ming Lei12446912012-08-04 12:01:20 +0800630 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
631 for (i = 0; i < fw_buf->nr_pages; i++)
632 __free_page(fw_buf->pages[i]);
633 kfree(fw_buf->pages);
634 fw_buf->pages = NULL;
635 fw_buf->page_array_size = 0;
636 fw_buf->nr_pages = 0;
637 set_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei28eefa72012-08-04 12:01:17 +0800638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 break;
640 case 0:
Ming Lei12446912012-08-04 12:01:20 +0800641 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
642 set_bit(FW_STATUS_DONE, &fw_buf->status);
643 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei253c9242012-10-09 12:01:02 +0800644
645 /*
646 * Several loading requests may be pending on
647 * one same firmware buf, so let all requests
648 * see the mapped 'buf->data' once the loading
649 * is completed.
650 * */
651 fw_map_pages_buf(fw_buf);
Takashi Iwai28cb7112013-05-22 18:28:38 +0200652 list_del_init(&fw_buf->pending_list);
Ming Lei1f2b7952012-08-04 12:01:21 +0800653 complete_all(&fw_buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 break;
655 }
656 /* fallthrough */
657 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700658 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 /* fallthrough */
660 case -1:
661 fw_load_abort(fw_priv);
662 break;
663 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500664out:
665 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return count;
667}
668
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700669static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700671static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
672 struct bin_attribute *bin_attr,
673 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200675 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700676 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800677 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700678 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Laura Garciacad1e552006-05-23 23:22:38 +0200680 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800681 buf = fw_priv->buf;
682 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 ret_count = -ENODEV;
684 goto out;
685 }
Ming Lei12446912012-08-04 12:01:20 +0800686 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200687 ret_count = 0;
688 goto out;
689 }
Ming Lei12446912012-08-04 12:01:20 +0800690 if (count > buf->size - offset)
691 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700692
693 ret_count = count;
694
695 while (count) {
696 void *page_data;
697 int page_nr = offset >> PAGE_SHIFT;
698 int page_ofs = offset & (PAGE_SIZE-1);
699 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
700
Ming Lei12446912012-08-04 12:01:20 +0800701 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700702
703 memcpy(buffer, page_data + page_ofs, page_cnt);
704
Ming Lei12446912012-08-04 12:01:20 +0800705 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700706 buffer += page_cnt;
707 offset += page_cnt;
708 count -= page_cnt;
709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710out:
Laura Garciacad1e552006-05-23 23:22:38 +0200711 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return ret_count;
713}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800714
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700715static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Ming Lei12446912012-08-04 12:01:20 +0800717 struct firmware_buf *buf = fw_priv->buf;
David Woodhouse6e03a202009-04-09 22:04:07 -0700718 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
David Woodhouse6e03a202009-04-09 22:04:07 -0700720 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800721 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700722 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800723 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700724 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
David Woodhouse6e03a202009-04-09 22:04:07 -0700726 new_pages = kmalloc(new_array_size * sizeof(void *),
727 GFP_KERNEL);
728 if (!new_pages) {
729 fw_load_abort(fw_priv);
730 return -ENOMEM;
731 }
Ming Lei12446912012-08-04 12:01:20 +0800732 memcpy(new_pages, buf->pages,
733 buf->page_array_size * sizeof(void *));
734 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
735 (new_array_size - buf->page_array_size));
736 kfree(buf->pages);
737 buf->pages = new_pages;
738 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700740
Ming Lei12446912012-08-04 12:01:20 +0800741 while (buf->nr_pages < pages_needed) {
742 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700743 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
744
Ming Lei12446912012-08-04 12:01:20 +0800745 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700746 fw_load_abort(fw_priv);
747 return -ENOMEM;
748 }
Ming Lei12446912012-08-04 12:01:20 +0800749 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return 0;
752}
753
754/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800755 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700756 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700757 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700758 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800759 * @buffer: buffer being written
760 * @offset: buffer offset for write in total data store area
761 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800763 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 * the driver as a firmware image.
765 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700766static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
767 struct bin_attribute *bin_attr,
768 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200770 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700771 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800772 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 ssize_t retval;
774
775 if (!capable(CAP_SYS_RAWIO))
776 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700777
Laura Garciacad1e552006-05-23 23:22:38 +0200778 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800779 buf = fw_priv->buf;
780 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 retval = -ENODEV;
782 goto out;
783 }
Ming Lei65710cb2012-08-04 12:01:16 +0800784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 retval = fw_realloc_buffer(fw_priv, offset + count);
786 if (retval)
787 goto out;
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700790
791 while (count) {
792 void *page_data;
793 int page_nr = offset >> PAGE_SHIFT;
794 int page_ofs = offset & (PAGE_SIZE - 1);
795 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
796
Ming Lei12446912012-08-04 12:01:20 +0800797 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700798
799 memcpy(page_data + page_ofs, buffer, page_cnt);
800
Ming Lei12446912012-08-04 12:01:20 +0800801 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700802 buffer += page_cnt;
803 offset += page_cnt;
804 count -= page_cnt;
805 }
806
Ming Lei12446912012-08-04 12:01:20 +0800807 buf->size = max_t(size_t, offset, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808out:
Laura Garciacad1e552006-05-23 23:22:38 +0200809 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return retval;
811}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800812
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700813static struct bin_attribute firmware_attr_data = {
814 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 .size = 0,
816 .read = firmware_data_read,
817 .write = firmware_data_write,
818};
819
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800820static void firmware_class_timeout_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800822 struct firmware_priv *fw_priv = container_of(work,
823 struct firmware_priv, timeout_work.work);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700824
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800825 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 fw_load_abort(fw_priv);
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800827 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700830static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200831fw_create_instance(struct firmware *firmware, const char *fw_name,
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700832 struct device *device, bool uevent, bool nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700834 struct firmware_priv *fw_priv;
835 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Ming Lei12446912012-08-04 12:01:20 +0800837 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700838 if (!fw_priv) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700839 dev_err(device, "%s: kmalloc failed\n", __func__);
Ming Lei12446912012-08-04 12:01:20 +0800840 fw_priv = ERR_PTR(-ENOMEM);
841 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700844 fw_priv->nowait = nowait;
Ming Lei1f2b7952012-08-04 12:01:21 +0800845 fw_priv->fw = firmware;
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800846 INIT_DELAYED_WORK(&fw_priv->timeout_work,
847 firmware_class_timeout_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700849 f_dev = &fw_priv->dev;
850
851 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800852 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700853 f_dev->parent = device;
854 f_dev->class = &firmware_class;
Ming Lei12446912012-08-04 12:01:20 +0800855exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700856 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100858
859/* load a firmware via user helper */
860static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
861 long timeout)
862{
863 int retval = 0;
864 struct device *f_dev = &fw_priv->dev;
865 struct firmware_buf *buf = fw_priv->buf;
866
867 /* fall back on userspace loading */
868 buf->is_paged_buf = true;
869
870 dev_set_uevent_suppress(f_dev, true);
871
872 /* Need to pin this module until class device is destroyed */
873 __module_get(THIS_MODULE);
874
875 retval = device_add(f_dev);
876 if (retval) {
877 dev_err(f_dev, "%s: device_register failed\n", __func__);
878 goto err_put_dev;
879 }
880
881 retval = device_create_bin_file(f_dev, &firmware_attr_data);
882 if (retval) {
883 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
884 goto err_del_dev;
885 }
886
Maxime Bizondf77f002013-08-29 20:28:13 +0200887 mutex_lock(&fw_lock);
888 list_add(&buf->pending_list, &pending_fw_head);
889 mutex_unlock(&fw_lock);
890
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100891 retval = device_create_file(f_dev, &dev_attr_loading);
892 if (retval) {
Maxime Bizondf77f002013-08-29 20:28:13 +0200893 mutex_lock(&fw_lock);
894 list_del_init(&buf->pending_list);
895 mutex_unlock(&fw_lock);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100896 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
897 goto err_del_bin_attr;
898 }
899
900 if (uevent) {
901 dev_set_uevent_suppress(f_dev, false);
902 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
903 if (timeout != MAX_SCHEDULE_TIMEOUT)
904 schedule_delayed_work(&fw_priv->timeout_work, timeout);
905
906 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
907 }
908
909 wait_for_completion(&buf->completion);
910
911 cancel_delayed_work_sync(&fw_priv->timeout_work);
912
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100913 device_remove_file(f_dev, &dev_attr_loading);
914err_del_bin_attr:
915 device_remove_bin_file(f_dev, &firmware_attr_data);
916err_del_dev:
917 device_del(f_dev);
918err_put_dev:
919 put_device(f_dev);
920 return retval;
921}
922
923static int fw_load_from_user_helper(struct firmware *firmware,
924 const char *name, struct device *device,
925 bool uevent, bool nowait, long timeout)
926{
927 struct firmware_priv *fw_priv;
928
929 fw_priv = fw_create_instance(firmware, name, device, uevent, nowait);
930 if (IS_ERR(fw_priv))
931 return PTR_ERR(fw_priv);
932
933 fw_priv->buf = firmware->priv;
934 return _request_firmware_load(fw_priv, uevent, timeout);
935}
936#else /* CONFIG_FW_LOADER_USER_HELPER */
937static inline int
938fw_load_from_user_helper(struct firmware *firmware, const char *name,
939 struct device *device, bool uevent, bool nowait,
940 long timeout)
941{
942 return -ENOENT;
943}
Takashi Iwai807be032013-01-31 11:13:57 +0100944
945/* No abort during direct loading */
946#define is_fw_load_aborted(buf) false
947
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100948#endif /* CONFIG_FW_LOADER_USER_HELPER */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Ming Leif531f05a2012-08-04 12:01:25 +0800950
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100951/* wait until the shared firmware_buf becomes ready (or error) */
952static int sync_cached_firmware_buf(struct firmware_buf *buf)
Ming Lei1f2b7952012-08-04 12:01:21 +0800953{
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100954 int ret = 0;
955
956 mutex_lock(&fw_lock);
957 while (!test_bit(FW_STATUS_DONE, &buf->status)) {
Takashi Iwai807be032013-01-31 11:13:57 +0100958 if (is_fw_load_aborted(buf)) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100959 ret = -ENOENT;
960 break;
961 }
962 mutex_unlock(&fw_lock);
963 wait_for_completion(&buf->completion);
964 mutex_lock(&fw_lock);
965 }
966 mutex_unlock(&fw_lock);
967 return ret;
Ming Lei1f2b7952012-08-04 12:01:21 +0800968}
969
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100970/* prepare firmware and firmware_buf structs;
971 * return 0 if a firmware is already assigned, 1 if need to load one,
972 * or a negative error code
973 */
974static int
975_request_firmware_prepare(struct firmware **firmware_p, const char *name,
976 struct device *device)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700977{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +0800979 struct firmware_buf *buf;
980 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Jiri Slaby4aed0642005-09-13 01:25:01 -0700982 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700984 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
985 __func__);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100986 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800989 if (fw_get_builtin_firmware(firmware, name)) {
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100990 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100991 return 0; /* assigned */
David Woodhouse5658c762008-05-23 13:52:42 +0100992 }
993
Ming Lei1f2b7952012-08-04 12:01:21 +0800994 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
Ming Lei1f2b7952012-08-04 12:01:21 +0800995
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100996 /*
997 * bind with 'buf' now to avoid warning in failure path
998 * of requesting firmware.
999 */
1000 firmware->priv = buf;
Ming Lei1f2b7952012-08-04 12:01:21 +08001001
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001002 if (ret > 0) {
1003 ret = sync_cached_firmware_buf(buf);
1004 if (!ret) {
1005 fw_set_page_data(buf, firmware);
1006 return 0; /* assigned */
1007 }
Stephen Boyddddb5542012-03-28 23:30:43 +02001008 }
Ming Lei1f2b7952012-08-04 12:01:21 +08001009
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001010 if (ret < 0)
1011 return ret;
1012 return 1; /* need to load */
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001013}
1014
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001015static int assign_firmware_buf(struct firmware *fw, struct device *device)
1016{
1017 struct firmware_buf *buf = fw->priv;
1018
1019 mutex_lock(&fw_lock);
Takashi Iwai807be032013-01-31 11:13:57 +01001020 if (!buf->size || is_fw_load_aborted(buf)) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001021 mutex_unlock(&fw_lock);
1022 return -ENOENT;
1023 }
1024
1025 /*
1026 * add firmware name into devres list so that we can auto cache
1027 * and uncache firmware for device.
1028 *
1029 * device may has been deleted already, but the problem
1030 * should be fixed in devres or driver core.
1031 */
1032 if (device)
1033 fw_add_devm_name(device, buf->fw_id);
1034
1035 /*
1036 * After caching firmware image is started, let it piggyback
1037 * on request firmware.
1038 */
1039 if (buf->fwc->state == FW_LOADER_START_CACHE) {
1040 if (fw_cache_piggyback_on_request(buf->fw_id))
1041 kref_get(&buf->ref);
1042 }
1043
1044 /* pass the pages buffer to driver at the last minute */
1045 fw_set_page_data(buf, fw);
1046 mutex_unlock(&fw_lock);
1047 return 0;
1048}
1049
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001050/* called from request_firmware() and request_firmware_work_func() */
1051static int
1052_request_firmware(const struct firmware **firmware_p, const char *name,
1053 struct device *device, bool uevent, bool nowait)
1054{
1055 struct firmware *fw;
1056 long timeout;
1057 int ret;
1058
1059 if (!firmware_p)
1060 return -EINVAL;
1061
1062 ret = _request_firmware_prepare(&fw, name, device);
1063 if (ret <= 0) /* error or already assigned */
1064 goto out;
1065
1066 ret = 0;
1067 timeout = firmware_loading_timeout();
1068 if (nowait) {
1069 timeout = usermodehelper_read_lock_wait(timeout);
1070 if (!timeout) {
1071 dev_dbg(device, "firmware: %s loading timed out\n",
1072 name);
1073 ret = -EBUSY;
1074 goto out;
1075 }
1076 } else {
1077 ret = usermodehelper_read_trylock();
1078 if (WARN_ON(ret)) {
1079 dev_err(device, "firmware: %s will not be loaded\n",
1080 name);
1081 goto out;
1082 }
1083 }
1084
1085 if (!fw_get_filesystem_firmware(device, fw->priv))
1086 ret = fw_load_from_user_helper(fw, name, device,
1087 uevent, nowait, timeout);
1088 if (!ret)
1089 ret = assign_firmware_buf(fw, device);
1090
1091 usermodehelper_read_unlock();
1092
1093 out:
1094 if (ret < 0) {
1095 release_firmware(fw);
1096 fw = NULL;
1097 }
1098
1099 *firmware_p = fw;
1100 return ret;
1101}
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103/**
Kay Sievers312c0042005-11-16 09:00:00 +01001104 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001105 * @firmware_p: pointer to firmware image
1106 * @name: name of firmware file
1107 * @device: device for which firmware is being loaded
1108 *
1109 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001110 * of @name for device @device.
1111 *
1112 * Should be called from user context where sleeping is allowed.
1113 *
Kay Sievers312c0042005-11-16 09:00:00 +01001114 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001115 * should be distinctive enough not to be confused with any other
1116 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +08001117 *
1118 * Caller must hold the reference count of @device.
Ming Lei6a927852012-11-03 17:48:16 +08001119 *
1120 * The function can be called safely inside device's suspend and
1121 * resume callback.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001122 **/
1123int
1124request_firmware(const struct firmware **firmware_p, const char *name,
1125 struct device *device)
1126{
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001127 return _request_firmware(firmware_p, name, device, true, false);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001128}
1129
1130/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001132 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001134void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
1136 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001137 if (!fw_is_builtin_firmware(fw))
1138 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 kfree(fw);
1140 }
1141}
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143/* Async support */
1144struct firmware_work {
1145 struct work_struct work;
1146 struct module *module;
1147 const char *name;
1148 struct device *device;
1149 void *context;
1150 void (*cont)(const struct firmware *fw, void *context);
Bob Liu072fc8f2011-01-26 18:33:32 +08001151 bool uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152};
1153
Stephen Boyda36cf842012-03-28 23:31:00 +02001154static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Stephen Boyda36cf842012-03-28 23:31:00 +02001156 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 const struct firmware *fw;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001158
Stephen Boyda36cf842012-03-28 23:31:00 +02001159 fw_work = container_of(work, struct firmware_work, work);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001160
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001161 _request_firmware(&fw, fw_work->name, fw_work->device,
1162 fw_work->uevent, true);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001163 fw_work->cont(fw, fw_work->context);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001164 put_device(fw_work->device); /* taken in request_firmware_nowait() */
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 module_put(fw_work->module);
1167 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169
1170/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001171 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001172 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001173 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001174 * is non-zero else the firmware copy must be done manually.
1175 * @name: name of firmware file
1176 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001177 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001178 * @context: will be passed over to @cont, and
1179 * @fw may be %NULL if firmware request fails.
1180 * @cont: function will be called asynchronously when the firmware
1181 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001183 * Caller must hold the reference count of @device.
1184 *
Ming Lei6f21a622012-08-04 12:01:24 +08001185 * Asynchronous variant of request_firmware() for user contexts:
1186 * - sleep for as small periods as possible since it may
1187 * increase kernel boot time of built-in device drivers
1188 * requesting firmware in their ->probe() methods, if
1189 * @gfp is GFP_KERNEL.
1190 *
1191 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 **/
1193int
1194request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001195 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001196 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 void (*cont)(const struct firmware *fw, void *context))
1198{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001199 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001201 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (!fw_work)
1203 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001204
1205 fw_work->module = module;
1206 fw_work->name = name;
1207 fw_work->device = device;
1208 fw_work->context = context;
1209 fw_work->cont = cont;
1210 fw_work->uevent = uevent;
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (!try_module_get(module)) {
1213 kfree(fw_work);
1214 return -EFAULT;
1215 }
1216
Ming Lei0cfc1e12012-08-04 12:01:23 +08001217 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001218 INIT_WORK(&fw_work->work, request_firmware_work_func);
1219 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return 0;
1221}
1222
Ming Lei2887b392012-08-04 12:01:22 +08001223/**
1224 * cache_firmware - cache one firmware image in kernel memory space
1225 * @fw_name: the firmware image name
1226 *
1227 * Cache firmware in kernel memory so that drivers can use it when
1228 * system isn't ready for them to request firmware image from userspace.
1229 * Once it returns successfully, driver can use request_firmware or its
1230 * nowait version to get the cached firmware without any interacting
1231 * with userspace
1232 *
1233 * Return 0 if the firmware image has been cached successfully
1234 * Return !0 otherwise
1235 *
1236 */
1237int cache_firmware(const char *fw_name)
1238{
1239 int ret;
1240 const struct firmware *fw;
1241
1242 pr_debug("%s: %s\n", __func__, fw_name);
1243
1244 ret = request_firmware(&fw, fw_name, NULL);
1245 if (!ret)
1246 kfree(fw);
1247
1248 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1249
1250 return ret;
1251}
1252
1253/**
1254 * uncache_firmware - remove one cached firmware image
1255 * @fw_name: the firmware image name
1256 *
1257 * Uncache one firmware image which has been cached successfully
1258 * before.
1259 *
1260 * Return 0 if the firmware cache has been removed successfully
1261 * Return !0 otherwise
1262 *
1263 */
1264int uncache_firmware(const char *fw_name)
1265{
1266 struct firmware_buf *buf;
1267 struct firmware fw;
1268
1269 pr_debug("%s: %s\n", __func__, fw_name);
1270
1271 if (fw_get_builtin_firmware(&fw, fw_name))
1272 return 0;
1273
1274 buf = fw_lookup_buf(fw_name);
1275 if (buf) {
1276 fw_free_buf(buf);
1277 return 0;
1278 }
1279
1280 return -EINVAL;
1281}
1282
Ming Leicfe016b2012-09-08 17:32:30 +08001283#ifdef CONFIG_PM_SLEEP
Ming Leid28d3882012-10-09 12:01:04 +08001284static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1285
Ming Lei37276a52012-08-04 12:01:27 +08001286static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1287{
1288 struct fw_cache_entry *fce;
1289
1290 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1291 if (!fce)
1292 goto exit;
1293
1294 strcpy(fce->name, name);
1295exit:
1296 return fce;
1297}
1298
Ming Lei373304f2012-10-09 12:01:01 +08001299static int __fw_entry_found(const char *name)
1300{
1301 struct firmware_cache *fwc = &fw_cache;
1302 struct fw_cache_entry *fce;
1303
1304 list_for_each_entry(fce, &fwc->fw_names, list) {
1305 if (!strcmp(fce->name, name))
1306 return 1;
1307 }
1308 return 0;
1309}
1310
Ming Leiac39b3e2012-08-20 19:04:16 +08001311static int fw_cache_piggyback_on_request(const char *name)
1312{
1313 struct firmware_cache *fwc = &fw_cache;
1314 struct fw_cache_entry *fce;
1315 int ret = 0;
1316
1317 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001318 if (__fw_entry_found(name))
1319 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001320
1321 fce = alloc_fw_cache_entry(name);
1322 if (fce) {
1323 ret = 1;
1324 list_add(&fce->list, &fwc->fw_names);
1325 pr_debug("%s: fw: %s\n", __func__, name);
1326 }
1327found:
1328 spin_unlock(&fwc->name_lock);
1329 return ret;
1330}
1331
Ming Lei37276a52012-08-04 12:01:27 +08001332static void free_fw_cache_entry(struct fw_cache_entry *fce)
1333{
1334 kfree(fce);
1335}
1336
1337static void __async_dev_cache_fw_image(void *fw_entry,
1338 async_cookie_t cookie)
1339{
1340 struct fw_cache_entry *fce = fw_entry;
1341 struct firmware_cache *fwc = &fw_cache;
1342 int ret;
1343
1344 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001345 if (ret) {
1346 spin_lock(&fwc->name_lock);
1347 list_del(&fce->list);
1348 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001349
Ming Leiac39b3e2012-08-20 19:04:16 +08001350 free_fw_cache_entry(fce);
1351 }
Ming Lei37276a52012-08-04 12:01:27 +08001352}
1353
1354/* called with dev->devres_lock held */
1355static void dev_create_fw_entry(struct device *dev, void *res,
1356 void *data)
1357{
1358 struct fw_name_devm *fwn = res;
1359 const char *fw_name = fwn->name;
1360 struct list_head *head = data;
1361 struct fw_cache_entry *fce;
1362
1363 fce = alloc_fw_cache_entry(fw_name);
1364 if (fce)
1365 list_add(&fce->list, head);
1366}
1367
1368static int devm_name_match(struct device *dev, void *res,
1369 void *match_data)
1370{
1371 struct fw_name_devm *fwn = res;
1372 return (fwn->magic == (unsigned long)match_data);
1373}
1374
Ming Leiab6dd8e2012-08-17 22:07:00 +08001375static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001376{
1377 LIST_HEAD(todo);
1378 struct fw_cache_entry *fce;
1379 struct fw_cache_entry *fce_next;
1380 struct firmware_cache *fwc = &fw_cache;
1381
1382 devres_for_each_res(dev, fw_name_devm_release,
1383 devm_name_match, &fw_cache,
1384 dev_create_fw_entry, &todo);
1385
1386 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1387 list_del(&fce->list);
1388
1389 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001390 /* only one cache entry for one firmware */
1391 if (!__fw_entry_found(fce->name)) {
Ming Lei373304f2012-10-09 12:01:01 +08001392 list_add(&fce->list, &fwc->fw_names);
1393 } else {
1394 free_fw_cache_entry(fce);
1395 fce = NULL;
1396 }
Ming Lei37276a52012-08-04 12:01:27 +08001397 spin_unlock(&fwc->name_lock);
1398
Ming Lei373304f2012-10-09 12:01:01 +08001399 if (fce)
Ming Leid28d3882012-10-09 12:01:04 +08001400 async_schedule_domain(__async_dev_cache_fw_image,
1401 (void *)fce,
1402 &fw_cache_domain);
Ming Lei37276a52012-08-04 12:01:27 +08001403 }
1404}
1405
1406static void __device_uncache_fw_images(void)
1407{
1408 struct firmware_cache *fwc = &fw_cache;
1409 struct fw_cache_entry *fce;
1410
1411 spin_lock(&fwc->name_lock);
1412 while (!list_empty(&fwc->fw_names)) {
1413 fce = list_entry(fwc->fw_names.next,
1414 struct fw_cache_entry, list);
1415 list_del(&fce->list);
1416 spin_unlock(&fwc->name_lock);
1417
1418 uncache_firmware(fce->name);
1419 free_fw_cache_entry(fce);
1420
1421 spin_lock(&fwc->name_lock);
1422 }
1423 spin_unlock(&fwc->name_lock);
1424}
1425
1426/**
1427 * device_cache_fw_images - cache devices' firmware
1428 *
1429 * If one device called request_firmware or its nowait version
1430 * successfully before, the firmware names are recored into the
1431 * device's devres link list, so device_cache_fw_images can call
1432 * cache_firmware() to cache these firmwares for the device,
1433 * then the device driver can load its firmwares easily at
1434 * time when system is not ready to complete loading firmware.
1435 */
1436static void device_cache_fw_images(void)
1437{
1438 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001439 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001440 DEFINE_WAIT(wait);
1441
1442 pr_debug("%s\n", __func__);
1443
Ming Lei373304f2012-10-09 12:01:01 +08001444 /* cancel uncache work */
1445 cancel_delayed_work_sync(&fwc->work);
1446
Ming Leiffe53f62012-08-04 12:01:28 +08001447 /*
1448 * use small loading timeout for caching devices' firmware
1449 * because all these firmware images have been loaded
1450 * successfully at lease once, also system is ready for
1451 * completing firmware loading now. The maximum size of
1452 * firmware in current distributions is about 2M bytes,
1453 * so 10 secs should be enough.
1454 */
1455 old_timeout = loading_timeout;
1456 loading_timeout = 10;
1457
Ming Leiac39b3e2012-08-20 19:04:16 +08001458 mutex_lock(&fw_lock);
1459 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001460 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001461 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001462
1463 /* wait for completion of caching firmware for all devices */
Ming Leid28d3882012-10-09 12:01:04 +08001464 async_synchronize_full_domain(&fw_cache_domain);
Ming Leiffe53f62012-08-04 12:01:28 +08001465
1466 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001467}
1468
1469/**
1470 * device_uncache_fw_images - uncache devices' firmware
1471 *
1472 * uncache all firmwares which have been cached successfully
1473 * by device_uncache_fw_images earlier
1474 */
1475static void device_uncache_fw_images(void)
1476{
1477 pr_debug("%s\n", __func__);
1478 __device_uncache_fw_images();
1479}
1480
1481static void device_uncache_fw_images_work(struct work_struct *work)
1482{
1483 device_uncache_fw_images();
1484}
1485
1486/**
1487 * device_uncache_fw_images_delay - uncache devices firmwares
1488 * @delay: number of milliseconds to delay uncache device firmwares
1489 *
1490 * uncache all devices's firmwares which has been cached successfully
1491 * by device_cache_fw_images after @delay milliseconds.
1492 */
1493static void device_uncache_fw_images_delay(unsigned long delay)
1494{
1495 schedule_delayed_work(&fw_cache.work,
1496 msecs_to_jiffies(delay));
1497}
1498
Ming Lei07646d92012-08-04 12:01:29 +08001499static int fw_pm_notify(struct notifier_block *notify_block,
1500 unsigned long mode, void *unused)
1501{
1502 switch (mode) {
1503 case PM_HIBERNATION_PREPARE:
1504 case PM_SUSPEND_PREPARE:
1505 device_cache_fw_images();
1506 break;
1507
1508 case PM_POST_SUSPEND:
1509 case PM_POST_HIBERNATION:
1510 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001511 /*
1512 * In case that system sleep failed and syscore_suspend is
1513 * not called.
1514 */
1515 mutex_lock(&fw_lock);
1516 fw_cache.state = FW_LOADER_NO_CACHE;
1517 mutex_unlock(&fw_lock);
1518
Ming Lei07646d92012-08-04 12:01:29 +08001519 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1520 break;
1521 }
1522
1523 return 0;
1524}
Ming Lei07646d92012-08-04 12:01:29 +08001525
Ming Leiac39b3e2012-08-20 19:04:16 +08001526/* stop caching firmware once syscore_suspend is reached */
1527static int fw_suspend(void)
1528{
1529 fw_cache.state = FW_LOADER_NO_CACHE;
1530 return 0;
1531}
1532
1533static struct syscore_ops fw_syscore_ops = {
1534 .suspend = fw_suspend,
1535};
Ming Leicfe016b2012-09-08 17:32:30 +08001536#else
1537static int fw_cache_piggyback_on_request(const char *name)
1538{
1539 return 0;
1540}
1541#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001542
Ming Lei37276a52012-08-04 12:01:27 +08001543static void __init fw_cache_init(void)
1544{
1545 spin_lock_init(&fw_cache.lock);
1546 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001547 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001548
Ming Leicfe016b2012-09-08 17:32:30 +08001549#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +08001550 spin_lock_init(&fw_cache.name_lock);
1551 INIT_LIST_HEAD(&fw_cache.fw_names);
Ming Lei37276a52012-08-04 12:01:27 +08001552
Ming Lei37276a52012-08-04 12:01:27 +08001553 INIT_DELAYED_WORK(&fw_cache.work,
1554 device_uncache_fw_images_work);
Ming Lei07646d92012-08-04 12:01:29 +08001555
1556 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1557 register_pm_notifier(&fw_cache.pm_notify);
Ming Leiac39b3e2012-08-20 19:04:16 +08001558
1559 register_syscore_ops(&fw_syscore_ops);
Ming Leicfe016b2012-09-08 17:32:30 +08001560#endif
Ming Lei37276a52012-08-04 12:01:27 +08001561}
1562
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001563static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
Ming Lei1f2b7952012-08-04 12:01:21 +08001565 fw_cache_init();
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001566#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwai28cb7112013-05-22 18:28:38 +02001567 register_reboot_notifier(&fw_shutdown_nb);
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001568 return class_register(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001569#else
1570 return 0;
1571#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001573
1574static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
Ming Leicfe016b2012-09-08 17:32:30 +08001576#ifdef CONFIG_PM_SLEEP
Ming Leiac39b3e2012-08-20 19:04:16 +08001577 unregister_syscore_ops(&fw_syscore_ops);
Ming Lei07646d92012-08-04 12:01:29 +08001578 unregister_pm_notifier(&fw_cache.pm_notify);
Ming Leicfe016b2012-09-08 17:32:30 +08001579#endif
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001580#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwai28cb7112013-05-22 18:28:38 +02001581 unregister_reboot_notifier(&fw_shutdown_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 class_unregister(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001583#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584}
1585
Shaohua Lia30a6a22006-09-27 01:50:52 -07001586fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587module_exit(firmware_class_exit);
1588
1589EXPORT_SYMBOL(release_firmware);
1590EXPORT_SYMBOL(request_firmware);
1591EXPORT_SYMBOL(request_firmware_nowait);
Ming Lei2887b392012-08-04 12:01:22 +08001592EXPORT_SYMBOL_GPL(cache_firmware);
1593EXPORT_SYMBOL_GPL(uncache_firmware);