blob: c4150431185fdd6edd6be583194068bc12bf7720 [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 Iwaife304142013-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;
Ming Leiaf5bc112013-05-27 18:30:04 +0800131 bool need_uevent;
David Woodhouse6e03a202009-04-09 22:04:07 -0700132 struct page **pages;
133 int nr_pages;
134 int page_array_size;
Takashi Iwaife304142013-05-22 18:28:38 +0200135 struct list_head pending_list;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100136#endif
Dmitry Torokhove1771232010-03-13 23:49:23 -0800137 char fw_id[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
Ming Lei37276a52012-08-04 12:01:27 +0800140struct fw_cache_entry {
141 struct list_head list;
142 char name[];
143};
144
Ming Leif531f05a2012-08-04 12:01:25 +0800145struct fw_name_devm {
146 unsigned long magic;
147 char name[];
148};
149
Ming Lei1f2b7952012-08-04 12:01:21 +0800150#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
151
Ming Leiac39b3e2012-08-20 19:04:16 +0800152#define FW_LOADER_NO_CACHE 0
153#define FW_LOADER_START_CACHE 1
154
155static int fw_cache_piggyback_on_request(const char *name);
156
Ming Lei1f2b7952012-08-04 12:01:21 +0800157/* fw_lock could be moved to 'struct firmware_priv' but since it is just
158 * guarding for corner cases a global lock should be OK */
159static DEFINE_MUTEX(fw_lock);
160
161static struct firmware_cache fw_cache;
162
163static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
164 struct firmware_cache *fwc)
165{
166 struct firmware_buf *buf;
167
168 buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
169
170 if (!buf)
171 return buf;
172
173 kref_init(&buf->ref);
174 strcpy(buf->fw_id, fw_name);
175 buf->fwc = fwc;
176 init_completion(&buf->completion);
Takashi Iwaife304142013-05-22 18:28:38 +0200177#ifdef CONFIG_FW_LOADER_USER_HELPER
178 INIT_LIST_HEAD(&buf->pending_list);
179#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800180
181 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
182
183 return buf;
184}
185
Ming Lei2887b392012-08-04 12:01:22 +0800186static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
187{
188 struct firmware_buf *tmp;
189 struct firmware_cache *fwc = &fw_cache;
190
191 list_for_each_entry(tmp, &fwc->head, list)
192 if (!strcmp(tmp->fw_id, fw_name))
193 return tmp;
194 return NULL;
195}
196
Ming Lei1f2b7952012-08-04 12:01:21 +0800197static int fw_lookup_and_allocate_buf(const char *fw_name,
198 struct firmware_cache *fwc,
199 struct firmware_buf **buf)
200{
201 struct firmware_buf *tmp;
202
203 spin_lock(&fwc->lock);
Ming Lei2887b392012-08-04 12:01:22 +0800204 tmp = __fw_lookup_buf(fw_name);
205 if (tmp) {
206 kref_get(&tmp->ref);
207 spin_unlock(&fwc->lock);
208 *buf = tmp;
209 return 1;
210 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800211 tmp = __allocate_fw_buf(fw_name, fwc);
212 if (tmp)
213 list_add(&tmp->list, &fwc->head);
214 spin_unlock(&fwc->lock);
215
216 *buf = tmp;
217
218 return tmp ? 0 : -ENOMEM;
219}
220
Ming Lei2887b392012-08-04 12:01:22 +0800221static struct firmware_buf *fw_lookup_buf(const char *fw_name)
222{
223 struct firmware_buf *tmp;
224 struct firmware_cache *fwc = &fw_cache;
225
226 spin_lock(&fwc->lock);
227 tmp = __fw_lookup_buf(fw_name);
228 spin_unlock(&fwc->lock);
229
230 return tmp;
231}
232
Ming Lei1f2b7952012-08-04 12:01:21 +0800233static void __fw_free_buf(struct kref *ref)
234{
235 struct firmware_buf *buf = to_fwbuf(ref);
236 struct firmware_cache *fwc = buf->fwc;
Ming Lei1f2b7952012-08-04 12:01:21 +0800237
238 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
239 __func__, buf->fw_id, buf, buf->data,
240 (unsigned int)buf->size);
241
Ming Lei1f2b7952012-08-04 12:01:21 +0800242 list_del(&buf->list);
243 spin_unlock(&fwc->lock);
244
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100245#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100246 if (buf->is_paged_buf) {
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100247 int i;
Ming Lei746058f2012-10-09 12:01:03 +0800248 vunmap(buf->data);
249 for (i = 0; i < buf->nr_pages; i++)
250 __free_page(buf->pages[i]);
251 kfree(buf->pages);
252 } else
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100253#endif
Ming Lei746058f2012-10-09 12:01:03 +0800254 vfree(buf->data);
Ming Lei1f2b7952012-08-04 12:01:21 +0800255 kfree(buf);
256}
257
258static void fw_free_buf(struct firmware_buf *buf)
259{
Chuansheng Liubd9eb7f2012-11-10 01:27:22 +0800260 struct firmware_cache *fwc = buf->fwc;
261 spin_lock(&fwc->lock);
262 if (!kref_put(&buf->ref, __fw_free_buf))
263 spin_unlock(&fwc->lock);
Ming Lei1f2b7952012-08-04 12:01:21 +0800264}
265
Ming Lei746058f2012-10-09 12:01:03 +0800266/* direct firmware loading support */
Ming Lei27602842012-11-03 17:47:58 +0800267static char fw_path_para[256];
268static const char * const fw_path[] = {
269 fw_path_para,
Ming Lei746058f2012-10-09 12:01:03 +0800270 "/lib/firmware/updates/" UTS_RELEASE,
271 "/lib/firmware/updates",
272 "/lib/firmware/" UTS_RELEASE,
273 "/lib/firmware"
274};
275
Ming Lei27602842012-11-03 17:47:58 +0800276/*
277 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
278 * from kernel command line because firmware_class is generally built in
279 * kernel instead of module.
280 */
281module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
282MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
283
Ming Lei746058f2012-10-09 12:01:03 +0800284/* Don't inline this: 'struct kstat' is biggish */
Cesar Eduardo Barros60dac5e2012-10-27 20:37:10 -0200285static noinline_for_stack long fw_file_size(struct file *file)
Ming Lei746058f2012-10-09 12:01:03 +0800286{
287 struct kstat st;
Al Viro3dadecc2013-01-24 02:18:08 -0500288 if (vfs_getattr(&file->f_path, &st))
Ming Lei746058f2012-10-09 12:01:03 +0800289 return -1;
290 if (!S_ISREG(st.mode))
291 return -1;
292 if (st.size != (long)st.size)
293 return -1;
294 return st.size;
295}
296
297static bool fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf)
298{
299 long size;
300 char *buf;
301
302 size = fw_file_size(file);
Luciano Coelho4adf07f2013-01-15 10:43:43 +0200303 if (size <= 0)
Ming Lei746058f2012-10-09 12:01:03 +0800304 return false;
305 buf = vmalloc(size);
306 if (!buf)
307 return false;
308 if (kernel_read(file, 0, buf, size) != size) {
309 vfree(buf);
310 return false;
311 }
312 fw_buf->data = buf;
313 fw_buf->size = size;
314 return true;
315}
316
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100317static bool fw_get_filesystem_firmware(struct device *device,
318 struct firmware_buf *buf)
Ming Lei746058f2012-10-09 12:01:03 +0800319{
320 int i;
321 bool success = false;
322 char *path = __getname();
323
324 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
325 struct file *file;
Ming Lei27602842012-11-03 17:47:58 +0800326
327 /* skip the unset customized path */
328 if (!fw_path[i][0])
329 continue;
330
Ming Lei746058f2012-10-09 12:01:03 +0800331 snprintf(path, PATH_MAX, "%s/%s", fw_path[i], buf->fw_id);
332
333 file = filp_open(path, O_RDONLY, 0);
334 if (IS_ERR(file))
335 continue;
336 success = fw_read_file_contents(file, buf);
337 fput(file);
338 if (success)
339 break;
340 }
341 __putname(path);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100342
343 if (success) {
344 dev_dbg(device, "firmware: direct-loading firmware %s\n",
345 buf->fw_id);
346 mutex_lock(&fw_lock);
347 set_bit(FW_STATUS_DONE, &buf->status);
348 complete_all(&buf->completion);
349 mutex_unlock(&fw_lock);
350 }
351
Ming Lei746058f2012-10-09 12:01:03 +0800352 return success;
353}
354
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100355/* firmware holds the ownership of pages */
356static void firmware_free_data(const struct firmware *fw)
357{
358 /* Loaded directly? */
359 if (!fw->priv) {
360 vfree(fw->data);
361 return;
362 }
363 fw_free_buf(fw->priv);
364}
365
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100366/* store the pages buffer info firmware from buf */
367static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
368{
369 fw->priv = buf;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100370#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100371 fw->pages = buf->pages;
372#endif
373 fw->size = buf->size;
374 fw->data = buf->data;
375
376 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
377 __func__, buf->fw_id, buf, buf->data,
378 (unsigned int)buf->size);
379}
380
381#ifdef CONFIG_PM_SLEEP
382static void fw_name_devm_release(struct device *dev, void *res)
383{
384 struct fw_name_devm *fwn = res;
385
386 if (fwn->magic == (unsigned long)&fw_cache)
387 pr_debug("%s: fw_name-%s devm-%p released\n",
388 __func__, fwn->name, res);
389}
390
391static int fw_devm_match(struct device *dev, void *res,
392 void *match_data)
393{
394 struct fw_name_devm *fwn = res;
395
396 return (fwn->magic == (unsigned long)&fw_cache) &&
397 !strcmp(fwn->name, match_data);
398}
399
400static struct fw_name_devm *fw_find_devm_name(struct device *dev,
401 const char *name)
402{
403 struct fw_name_devm *fwn;
404
405 fwn = devres_find(dev, fw_name_devm_release,
406 fw_devm_match, (void *)name);
407 return fwn;
408}
409
410/* add firmware name into devres list */
411static int fw_add_devm_name(struct device *dev, const char *name)
412{
413 struct fw_name_devm *fwn;
414
415 fwn = fw_find_devm_name(dev, name);
416 if (fwn)
417 return 1;
418
419 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
420 strlen(name) + 1, GFP_KERNEL);
421 if (!fwn)
422 return -ENOMEM;
423
424 fwn->magic = (unsigned long)&fw_cache;
425 strcpy(fwn->name, name);
426 devres_add(dev, fwn);
427
428 return 0;
429}
430#else
431static int fw_add_devm_name(struct device *dev, const char *name)
432{
433 return 0;
434}
435#endif
436
437
438/*
439 * user-mode helper code
440 */
441#ifdef CONFIG_FW_LOADER_USER_HELPER
442struct firmware_priv {
443 struct delayed_work timeout_work;
444 bool nowait;
445 struct device dev;
446 struct firmware_buf *buf;
447 struct firmware *fw;
448};
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100449
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700450static struct firmware_priv *to_firmware_priv(struct device *dev)
451{
452 return container_of(dev, struct firmware_priv, dev);
453}
454
Takashi Iwaife304142013-05-22 18:28:38 +0200455static void fw_load_abort(struct firmware_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Takashi Iwaife304142013-05-22 18:28:38 +0200457 list_del_init(&buf->pending_list);
Ming Lei12446912012-08-04 12:01:20 +0800458 set_bit(FW_STATUS_ABORT, &buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800459 complete_all(&buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
Takashi Iwai807be032013-01-31 11:13:57 +0100462#define is_fw_load_aborted(buf) \
463 test_bit(FW_STATUS_ABORT, &(buf)->status)
464
Takashi Iwaife304142013-05-22 18:28:38 +0200465static LIST_HEAD(pending_fw_head);
466
467/* reboot notifier for avoid deadlock with usermode_lock */
468static int fw_shutdown_notify(struct notifier_block *unused1,
469 unsigned long unused2, void *unused3)
470{
471 mutex_lock(&fw_lock);
472 while (!list_empty(&pending_fw_head))
473 fw_load_abort(list_first_entry(&pending_fw_head,
474 struct firmware_buf,
475 pending_list));
476 mutex_unlock(&fw_lock);
477 return NOTIFY_DONE;
478}
479
480static struct notifier_block fw_shutdown_nb = {
481 .notifier_call = fw_shutdown_notify,
482};
483
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700484static ssize_t firmware_timeout_show(struct class *class,
485 struct class_attribute *attr,
486 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 return sprintf(buf, "%d\n", loading_timeout);
489}
490
491/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800492 * firmware_timeout_store - set number of seconds to wait for firmware
493 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800494 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800495 * @buf: buffer to scan for timeout value
496 * @count: number of bytes in @buf
497 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800499 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 * firmware will be provided.
501 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800502 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700504static ssize_t firmware_timeout_store(struct class *class,
505 struct class_attribute *attr,
506 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700509 if (loading_timeout < 0)
510 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return count;
513}
514
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800515static struct class_attribute firmware_class_attrs[] = {
516 __ATTR(timeout, S_IWUSR | S_IRUGO,
517 firmware_timeout_show, firmware_timeout_store),
518 __ATTR_NULL
519};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800521static void fw_dev_release(struct device *dev)
522{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700523 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800524
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800525 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800526
527 module_put(THIS_MODULE);
528}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Kay Sievers7eff2e72007-08-14 15:15:12 +0200530static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700532 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Ming Lei12446912012-08-04 12:01:20 +0800534 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200536 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700537 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200538 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
539 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 return 0;
542}
543
Adrian Bunk1b81d662006-05-20 15:00:16 -0700544static struct class firmware_class = {
545 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800546 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700547 .dev_uevent = firmware_uevent,
548 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700549};
550
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700551static ssize_t firmware_loading_show(struct device *dev,
552 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700554 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800555 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return sprintf(buf, "%d\n", loading);
558}
559
David Woodhouse6e03a202009-04-09 22:04:07 -0700560/* Some architectures don't have PAGE_KERNEL_RO */
561#ifndef PAGE_KERNEL_RO
562#define PAGE_KERNEL_RO PAGE_KERNEL
563#endif
Ming Lei253c9242012-10-09 12:01:02 +0800564
565/* one pages buffer should be mapped/unmapped only once */
566static int fw_map_pages_buf(struct firmware_buf *buf)
567{
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100568 if (!buf->is_paged_buf)
Ming Lei746058f2012-10-09 12:01:03 +0800569 return 0;
570
Ming Lei253c9242012-10-09 12:01:02 +0800571 if (buf->data)
572 vunmap(buf->data);
573 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
574 if (!buf->data)
575 return -ENOMEM;
576 return 0;
577}
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800580 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700581 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800582 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800583 * @buf: buffer to scan for loading control value
584 * @count: number of bytes in @buf
585 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 * The relevant values are:
587 *
588 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800589 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 * -1: Conclude the load with an error and discard any written data.
591 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700592static ssize_t firmware_loading_store(struct device *dev,
593 struct device_attribute *attr,
594 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700596 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800597 struct firmware_buf *fw_buf = fw_priv->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700599 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Neil Hormaneea915b2012-01-02 15:31:23 -0500601 mutex_lock(&fw_lock);
602
Ming Lei12446912012-08-04 12:01:20 +0800603 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500604 goto out;
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 switch (loading) {
607 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800608 /* discarding any previous partial load */
Ming Lei12446912012-08-04 12:01:20 +0800609 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
610 for (i = 0; i < fw_buf->nr_pages; i++)
611 __free_page(fw_buf->pages[i]);
612 kfree(fw_buf->pages);
613 fw_buf->pages = NULL;
614 fw_buf->page_array_size = 0;
615 fw_buf->nr_pages = 0;
616 set_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei28eefa72012-08-04 12:01:17 +0800617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 break;
619 case 0:
Ming Lei12446912012-08-04 12:01:20 +0800620 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
621 set_bit(FW_STATUS_DONE, &fw_buf->status);
622 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei253c9242012-10-09 12:01:02 +0800623
624 /*
625 * Several loading requests may be pending on
626 * one same firmware buf, so let all requests
627 * see the mapped 'buf->data' once the loading
628 * is completed.
629 * */
630 fw_map_pages_buf(fw_buf);
Takashi Iwaife304142013-05-22 18:28:38 +0200631 list_del_init(&fw_buf->pending_list);
Ming Lei1f2b7952012-08-04 12:01:21 +0800632 complete_all(&fw_buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 break;
634 }
635 /* fallthrough */
636 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700637 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 /* fallthrough */
639 case -1:
Takashi Iwaife304142013-05-22 18:28:38 +0200640 fw_load_abort(fw_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 break;
642 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500643out:
644 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return count;
646}
647
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700648static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700650static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
651 struct bin_attribute *bin_attr,
652 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200654 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700655 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800656 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700657 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Laura Garciacad1e552006-05-23 23:22:38 +0200659 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800660 buf = fw_priv->buf;
661 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 ret_count = -ENODEV;
663 goto out;
664 }
Ming Lei12446912012-08-04 12:01:20 +0800665 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200666 ret_count = 0;
667 goto out;
668 }
Ming Lei12446912012-08-04 12:01:20 +0800669 if (count > buf->size - offset)
670 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700671
672 ret_count = count;
673
674 while (count) {
675 void *page_data;
676 int page_nr = offset >> PAGE_SHIFT;
677 int page_ofs = offset & (PAGE_SIZE-1);
678 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
679
Ming Lei12446912012-08-04 12:01:20 +0800680 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700681
682 memcpy(buffer, page_data + page_ofs, page_cnt);
683
Ming Lei12446912012-08-04 12:01:20 +0800684 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700685 buffer += page_cnt;
686 offset += page_cnt;
687 count -= page_cnt;
688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689out:
Laura Garciacad1e552006-05-23 23:22:38 +0200690 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return ret_count;
692}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800693
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700694static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Ming Lei12446912012-08-04 12:01:20 +0800696 struct firmware_buf *buf = fw_priv->buf;
David Woodhouse6e03a202009-04-09 22:04:07 -0700697 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
David Woodhouse6e03a202009-04-09 22:04:07 -0700699 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800700 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700701 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800702 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700703 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
David Woodhouse6e03a202009-04-09 22:04:07 -0700705 new_pages = kmalloc(new_array_size * sizeof(void *),
706 GFP_KERNEL);
707 if (!new_pages) {
Takashi Iwaife304142013-05-22 18:28:38 +0200708 fw_load_abort(buf);
David Woodhouse6e03a202009-04-09 22:04:07 -0700709 return -ENOMEM;
710 }
Ming Lei12446912012-08-04 12:01:20 +0800711 memcpy(new_pages, buf->pages,
712 buf->page_array_size * sizeof(void *));
713 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
714 (new_array_size - buf->page_array_size));
715 kfree(buf->pages);
716 buf->pages = new_pages;
717 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700719
Ming Lei12446912012-08-04 12:01:20 +0800720 while (buf->nr_pages < pages_needed) {
721 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700722 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
723
Ming Lei12446912012-08-04 12:01:20 +0800724 if (!buf->pages[buf->nr_pages]) {
Takashi Iwaife304142013-05-22 18:28:38 +0200725 fw_load_abort(buf);
David Woodhouse6e03a202009-04-09 22:04:07 -0700726 return -ENOMEM;
727 }
Ming Lei12446912012-08-04 12:01:20 +0800728 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return 0;
731}
732
733/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800734 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700735 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700736 * @kobj: kobject for the device
Randy Dunlap42e61f4a2007-07-23 21:42:11 -0700737 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800738 * @buffer: buffer being written
739 * @offset: buffer offset for write in total data store area
740 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800742 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 * the driver as a firmware image.
744 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700745static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
746 struct bin_attribute *bin_attr,
747 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200749 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700750 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800751 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 ssize_t retval;
753
754 if (!capable(CAP_SYS_RAWIO))
755 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700756
Laura Garciacad1e552006-05-23 23:22:38 +0200757 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800758 buf = fw_priv->buf;
759 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 retval = -ENODEV;
761 goto out;
762 }
Ming Lei65710cb2012-08-04 12:01:16 +0800763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 retval = fw_realloc_buffer(fw_priv, offset + count);
765 if (retval)
766 goto out;
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700769
770 while (count) {
771 void *page_data;
772 int page_nr = offset >> PAGE_SHIFT;
773 int page_ofs = offset & (PAGE_SIZE - 1);
774 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
775
Ming Lei12446912012-08-04 12:01:20 +0800776 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700777
778 memcpy(page_data + page_ofs, buffer, page_cnt);
779
Ming Lei12446912012-08-04 12:01:20 +0800780 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700781 buffer += page_cnt;
782 offset += page_cnt;
783 count -= page_cnt;
784 }
785
Ming Lei12446912012-08-04 12:01:20 +0800786 buf->size = max_t(size_t, offset, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787out:
Laura Garciacad1e552006-05-23 23:22:38 +0200788 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return retval;
790}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800791
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700792static struct bin_attribute firmware_attr_data = {
793 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 .size = 0,
795 .read = firmware_data_read,
796 .write = firmware_data_write,
797};
798
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800799static void firmware_class_timeout_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800801 struct firmware_priv *fw_priv = container_of(work,
802 struct firmware_priv, timeout_work.work);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700803
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800804 mutex_lock(&fw_lock);
805 if (test_bit(FW_STATUS_DONE, &(fw_priv->buf->status))) {
806 mutex_unlock(&fw_lock);
807 return;
808 }
Takashi Iwaife304142013-05-22 18:28:38 +0200809 fw_load_abort(fw_priv->buf);
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800810 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700813static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200814fw_create_instance(struct firmware *firmware, const char *fw_name,
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700815 struct device *device, bool uevent, bool nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700817 struct firmware_priv *fw_priv;
818 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Ming Lei12446912012-08-04 12:01:20 +0800820 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700821 if (!fw_priv) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700822 dev_err(device, "%s: kmalloc failed\n", __func__);
Ming Lei12446912012-08-04 12:01:20 +0800823 fw_priv = ERR_PTR(-ENOMEM);
824 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700827 fw_priv->nowait = nowait;
Ming Lei1f2b7952012-08-04 12:01:21 +0800828 fw_priv->fw = firmware;
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800829 INIT_DELAYED_WORK(&fw_priv->timeout_work,
830 firmware_class_timeout_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700832 f_dev = &fw_priv->dev;
833
834 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800835 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700836 f_dev->parent = device;
837 f_dev->class = &firmware_class;
Ming Lei12446912012-08-04 12:01:20 +0800838exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700839 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100841
842/* load a firmware via user helper */
843static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
844 long timeout)
845{
846 int retval = 0;
847 struct device *f_dev = &fw_priv->dev;
848 struct firmware_buf *buf = fw_priv->buf;
849
850 /* fall back on userspace loading */
851 buf->is_paged_buf = true;
852
853 dev_set_uevent_suppress(f_dev, true);
854
855 /* Need to pin this module until class device is destroyed */
856 __module_get(THIS_MODULE);
857
858 retval = device_add(f_dev);
859 if (retval) {
860 dev_err(f_dev, "%s: device_register failed\n", __func__);
861 goto err_put_dev;
862 }
863
864 retval = device_create_bin_file(f_dev, &firmware_attr_data);
865 if (retval) {
866 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
867 goto err_del_dev;
868 }
869
870 retval = device_create_file(f_dev, &dev_attr_loading);
871 if (retval) {
872 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
873 goto err_del_bin_attr;
874 }
875
876 if (uevent) {
Ming Leiaf5bc112013-05-27 18:30:04 +0800877 buf->need_uevent = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100878 dev_set_uevent_suppress(f_dev, false);
879 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
880 if (timeout != MAX_SCHEDULE_TIMEOUT)
881 schedule_delayed_work(&fw_priv->timeout_work, timeout);
882
883 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
884 }
885
Takashi Iwaife304142013-05-22 18:28:38 +0200886 mutex_lock(&fw_lock);
887 list_add(&buf->pending_list, &pending_fw_head);
888 mutex_unlock(&fw_lock);
889
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100890 wait_for_completion(&buf->completion);
891
892 cancel_delayed_work_sync(&fw_priv->timeout_work);
893
894 fw_priv->buf = NULL;
895
896 device_remove_file(f_dev, &dev_attr_loading);
897err_del_bin_attr:
898 device_remove_bin_file(f_dev, &firmware_attr_data);
899err_del_dev:
900 device_del(f_dev);
901err_put_dev:
902 put_device(f_dev);
903 return retval;
904}
905
906static int fw_load_from_user_helper(struct firmware *firmware,
907 const char *name, struct device *device,
908 bool uevent, bool nowait, long timeout)
909{
910 struct firmware_priv *fw_priv;
911
912 fw_priv = fw_create_instance(firmware, name, device, uevent, nowait);
913 if (IS_ERR(fw_priv))
914 return PTR_ERR(fw_priv);
915
916 fw_priv->buf = firmware->priv;
917 return _request_firmware_load(fw_priv, uevent, timeout);
918}
919#else /* CONFIG_FW_LOADER_USER_HELPER */
920static inline int
921fw_load_from_user_helper(struct firmware *firmware, const char *name,
922 struct device *device, bool uevent, bool nowait,
923 long timeout)
924{
925 return -ENOENT;
926}
Takashi Iwai807be032013-01-31 11:13:57 +0100927
928/* No abort during direct loading */
929#define is_fw_load_aborted(buf) false
930
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100931#endif /* CONFIG_FW_LOADER_USER_HELPER */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Ming Leif531f05a2012-08-04 12:01:25 +0800933
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100934/* wait until the shared firmware_buf becomes ready (or error) */
935static int sync_cached_firmware_buf(struct firmware_buf *buf)
Ming Lei1f2b7952012-08-04 12:01:21 +0800936{
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100937 int ret = 0;
938
939 mutex_lock(&fw_lock);
940 while (!test_bit(FW_STATUS_DONE, &buf->status)) {
Takashi Iwai807be032013-01-31 11:13:57 +0100941 if (is_fw_load_aborted(buf)) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100942 ret = -ENOENT;
943 break;
944 }
945 mutex_unlock(&fw_lock);
946 wait_for_completion(&buf->completion);
947 mutex_lock(&fw_lock);
948 }
949 mutex_unlock(&fw_lock);
950 return ret;
Ming Lei1f2b7952012-08-04 12:01:21 +0800951}
952
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100953/* prepare firmware and firmware_buf structs;
954 * return 0 if a firmware is already assigned, 1 if need to load one,
955 * or a negative error code
956 */
957static int
958_request_firmware_prepare(struct firmware **firmware_p, const char *name,
959 struct device *device)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700960{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +0800962 struct firmware_buf *buf;
963 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Jiri Slaby4aed0642005-09-13 01:25:01 -0700965 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700967 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
968 __func__);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100969 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800972 if (fw_get_builtin_firmware(firmware, name)) {
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100973 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100974 return 0; /* assigned */
David Woodhouse5658c762008-05-23 13:52:42 +0100975 }
976
Ming Lei1f2b7952012-08-04 12:01:21 +0800977 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
Ming Lei1f2b7952012-08-04 12:01:21 +0800978
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100979 /*
980 * bind with 'buf' now to avoid warning in failure path
981 * of requesting firmware.
982 */
983 firmware->priv = buf;
Ming Lei1f2b7952012-08-04 12:01:21 +0800984
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100985 if (ret > 0) {
986 ret = sync_cached_firmware_buf(buf);
987 if (!ret) {
988 fw_set_page_data(buf, firmware);
989 return 0; /* assigned */
990 }
Stephen Boyddddb5542012-03-28 23:30:43 +0200991 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800992
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100993 if (ret < 0)
994 return ret;
995 return 1; /* need to load */
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200996}
997
Ming Leie771d1a2013-05-27 18:30:03 +0800998static int assign_firmware_buf(struct firmware *fw, struct device *device,
999 bool skip_cache)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001000{
1001 struct firmware_buf *buf = fw->priv;
1002
1003 mutex_lock(&fw_lock);
Takashi Iwai807be032013-01-31 11:13:57 +01001004 if (!buf->size || is_fw_load_aborted(buf)) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001005 mutex_unlock(&fw_lock);
1006 return -ENOENT;
1007 }
1008
1009 /*
1010 * add firmware name into devres list so that we can auto cache
1011 * and uncache firmware for device.
1012 *
1013 * device may has been deleted already, but the problem
1014 * should be fixed in devres or driver core.
1015 */
Ming Leie771d1a2013-05-27 18:30:03 +08001016 if (device && !skip_cache)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001017 fw_add_devm_name(device, buf->fw_id);
1018
1019 /*
1020 * After caching firmware image is started, let it piggyback
1021 * on request firmware.
1022 */
1023 if (buf->fwc->state == FW_LOADER_START_CACHE) {
1024 if (fw_cache_piggyback_on_request(buf->fw_id))
1025 kref_get(&buf->ref);
1026 }
1027
1028 /* pass the pages buffer to driver at the last minute */
1029 fw_set_page_data(buf, fw);
1030 mutex_unlock(&fw_lock);
1031 return 0;
1032}
1033
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001034/* called from request_firmware() and request_firmware_work_func() */
1035static int
1036_request_firmware(const struct firmware **firmware_p, const char *name,
1037 struct device *device, bool uevent, bool nowait)
1038{
1039 struct firmware *fw;
1040 long timeout;
1041 int ret;
1042
1043 if (!firmware_p)
1044 return -EINVAL;
1045
1046 ret = _request_firmware_prepare(&fw, name, device);
1047 if (ret <= 0) /* error or already assigned */
1048 goto out;
1049
1050 ret = 0;
1051 timeout = firmware_loading_timeout();
1052 if (nowait) {
1053 timeout = usermodehelper_read_lock_wait(timeout);
1054 if (!timeout) {
1055 dev_dbg(device, "firmware: %s loading timed out\n",
1056 name);
1057 ret = -EBUSY;
1058 goto out;
1059 }
1060 } else {
1061 ret = usermodehelper_read_trylock();
1062 if (WARN_ON(ret)) {
1063 dev_err(device, "firmware: %s will not be loaded\n",
1064 name);
1065 goto out;
1066 }
1067 }
1068
1069 if (!fw_get_filesystem_firmware(device, fw->priv))
1070 ret = fw_load_from_user_helper(fw, name, device,
1071 uevent, nowait, timeout);
Ming Leie771d1a2013-05-27 18:30:03 +08001072
1073 /* don't cache firmware handled without uevent */
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001074 if (!ret)
Ming Leie771d1a2013-05-27 18:30:03 +08001075 ret = assign_firmware_buf(fw, device, !uevent);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001076
1077 usermodehelper_read_unlock();
1078
1079 out:
1080 if (ret < 0) {
1081 release_firmware(fw);
1082 fw = NULL;
1083 }
1084
1085 *firmware_p = fw;
1086 return ret;
1087}
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089/**
Kay Sievers312c0042005-11-16 09:00:00 +01001090 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001091 * @firmware_p: pointer to firmware image
1092 * @name: name of firmware file
1093 * @device: device for which firmware is being loaded
1094 *
1095 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001096 * of @name for device @device.
1097 *
1098 * Should be called from user context where sleeping is allowed.
1099 *
Kay Sievers312c0042005-11-16 09:00:00 +01001100 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001101 * should be distinctive enough not to be confused with any other
1102 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +08001103 *
1104 * Caller must hold the reference count of @device.
Ming Lei6a927852012-11-03 17:48:16 +08001105 *
1106 * The function can be called safely inside device's suspend and
1107 * resume callback.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001108 **/
1109int
1110request_firmware(const struct firmware **firmware_p, const char *name,
1111 struct device *device)
1112{
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001113 return _request_firmware(firmware_p, name, device, true, false);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001114}
Daniel Mackf4945132013-05-23 22:17:18 +02001115EXPORT_SYMBOL(request_firmware);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001116
1117/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001119 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001121void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
1123 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001124 if (!fw_is_builtin_firmware(fw))
1125 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 kfree(fw);
1127 }
1128}
Daniel Mackf4945132013-05-23 22:17:18 +02001129EXPORT_SYMBOL(release_firmware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131/* Async support */
1132struct firmware_work {
1133 struct work_struct work;
1134 struct module *module;
1135 const char *name;
1136 struct device *device;
1137 void *context;
1138 void (*cont)(const struct firmware *fw, void *context);
Bob Liu072fc8f2011-01-26 18:33:32 +08001139 bool uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140};
1141
Stephen Boyda36cf842012-03-28 23:31:00 +02001142static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
Stephen Boyda36cf842012-03-28 23:31:00 +02001144 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 const struct firmware *fw;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001146
Stephen Boyda36cf842012-03-28 23:31:00 +02001147 fw_work = container_of(work, struct firmware_work, work);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001148
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001149 _request_firmware(&fw, fw_work->name, fw_work->device,
1150 fw_work->uevent, true);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001151 fw_work->cont(fw, fw_work->context);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001152 put_device(fw_work->device); /* taken in request_firmware_nowait() */
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 module_put(fw_work->module);
1155 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156}
1157
1158/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001159 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001160 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001161 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001162 * is non-zero else the firmware copy must be done manually.
1163 * @name: name of firmware file
1164 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001165 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001166 * @context: will be passed over to @cont, and
1167 * @fw may be %NULL if firmware request fails.
1168 * @cont: function will be called asynchronously when the firmware
1169 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001171 * Caller must hold the reference count of @device.
1172 *
Ming Lei6f21a622012-08-04 12:01:24 +08001173 * Asynchronous variant of request_firmware() for user contexts:
1174 * - sleep for as small periods as possible since it may
1175 * increase kernel boot time of built-in device drivers
1176 * requesting firmware in their ->probe() methods, if
1177 * @gfp is GFP_KERNEL.
1178 *
1179 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 **/
1181int
1182request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001183 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001184 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 void (*cont)(const struct firmware *fw, void *context))
1186{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001187 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001189 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if (!fw_work)
1191 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001192
1193 fw_work->module = module;
1194 fw_work->name = name;
1195 fw_work->device = device;
1196 fw_work->context = context;
1197 fw_work->cont = cont;
1198 fw_work->uevent = uevent;
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if (!try_module_get(module)) {
1201 kfree(fw_work);
1202 return -EFAULT;
1203 }
1204
Ming Lei0cfc1e12012-08-04 12:01:23 +08001205 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001206 INIT_WORK(&fw_work->work, request_firmware_work_func);
1207 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return 0;
1209}
Daniel Mackf4945132013-05-23 22:17:18 +02001210EXPORT_SYMBOL(request_firmware_nowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Ming Lei2887b392012-08-04 12:01:22 +08001212/**
1213 * cache_firmware - cache one firmware image in kernel memory space
1214 * @fw_name: the firmware image name
1215 *
1216 * Cache firmware in kernel memory so that drivers can use it when
1217 * system isn't ready for them to request firmware image from userspace.
1218 * Once it returns successfully, driver can use request_firmware or its
1219 * nowait version to get the cached firmware without any interacting
1220 * with userspace
1221 *
1222 * Return 0 if the firmware image has been cached successfully
1223 * Return !0 otherwise
1224 *
1225 */
1226int cache_firmware(const char *fw_name)
1227{
1228 int ret;
1229 const struct firmware *fw;
1230
1231 pr_debug("%s: %s\n", __func__, fw_name);
1232
1233 ret = request_firmware(&fw, fw_name, NULL);
1234 if (!ret)
1235 kfree(fw);
1236
1237 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1238
1239 return ret;
1240}
Daniel Mackf4945132013-05-23 22:17:18 +02001241EXPORT_SYMBOL_GPL(cache_firmware);
Ming Lei2887b392012-08-04 12:01:22 +08001242
1243/**
1244 * uncache_firmware - remove one cached firmware image
1245 * @fw_name: the firmware image name
1246 *
1247 * Uncache one firmware image which has been cached successfully
1248 * before.
1249 *
1250 * Return 0 if the firmware cache has been removed successfully
1251 * Return !0 otherwise
1252 *
1253 */
1254int uncache_firmware(const char *fw_name)
1255{
1256 struct firmware_buf *buf;
1257 struct firmware fw;
1258
1259 pr_debug("%s: %s\n", __func__, fw_name);
1260
1261 if (fw_get_builtin_firmware(&fw, fw_name))
1262 return 0;
1263
1264 buf = fw_lookup_buf(fw_name);
1265 if (buf) {
1266 fw_free_buf(buf);
1267 return 0;
1268 }
1269
1270 return -EINVAL;
1271}
Daniel Mackf4945132013-05-23 22:17:18 +02001272EXPORT_SYMBOL_GPL(uncache_firmware);
Ming Lei2887b392012-08-04 12:01:22 +08001273
Ming Leicfe016b2012-09-08 17:32:30 +08001274#ifdef CONFIG_PM_SLEEP
Ming Leid28d3882012-10-09 12:01:04 +08001275static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1276
Ming Lei37276a52012-08-04 12:01:27 +08001277static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1278{
1279 struct fw_cache_entry *fce;
1280
1281 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1282 if (!fce)
1283 goto exit;
1284
1285 strcpy(fce->name, name);
1286exit:
1287 return fce;
1288}
1289
Ming Lei373304f2012-10-09 12:01:01 +08001290static int __fw_entry_found(const char *name)
1291{
1292 struct firmware_cache *fwc = &fw_cache;
1293 struct fw_cache_entry *fce;
1294
1295 list_for_each_entry(fce, &fwc->fw_names, list) {
1296 if (!strcmp(fce->name, name))
1297 return 1;
1298 }
1299 return 0;
1300}
1301
Ming Leiac39b3e2012-08-20 19:04:16 +08001302static int fw_cache_piggyback_on_request(const char *name)
1303{
1304 struct firmware_cache *fwc = &fw_cache;
1305 struct fw_cache_entry *fce;
1306 int ret = 0;
1307
1308 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001309 if (__fw_entry_found(name))
1310 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001311
1312 fce = alloc_fw_cache_entry(name);
1313 if (fce) {
1314 ret = 1;
1315 list_add(&fce->list, &fwc->fw_names);
1316 pr_debug("%s: fw: %s\n", __func__, name);
1317 }
1318found:
1319 spin_unlock(&fwc->name_lock);
1320 return ret;
1321}
1322
Ming Lei37276a52012-08-04 12:01:27 +08001323static void free_fw_cache_entry(struct fw_cache_entry *fce)
1324{
1325 kfree(fce);
1326}
1327
1328static void __async_dev_cache_fw_image(void *fw_entry,
1329 async_cookie_t cookie)
1330{
1331 struct fw_cache_entry *fce = fw_entry;
1332 struct firmware_cache *fwc = &fw_cache;
1333 int ret;
1334
1335 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001336 if (ret) {
1337 spin_lock(&fwc->name_lock);
1338 list_del(&fce->list);
1339 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001340
Ming Leiac39b3e2012-08-20 19:04:16 +08001341 free_fw_cache_entry(fce);
1342 }
Ming Lei37276a52012-08-04 12:01:27 +08001343}
1344
1345/* called with dev->devres_lock held */
1346static void dev_create_fw_entry(struct device *dev, void *res,
1347 void *data)
1348{
1349 struct fw_name_devm *fwn = res;
1350 const char *fw_name = fwn->name;
1351 struct list_head *head = data;
1352 struct fw_cache_entry *fce;
1353
1354 fce = alloc_fw_cache_entry(fw_name);
1355 if (fce)
1356 list_add(&fce->list, head);
1357}
1358
1359static int devm_name_match(struct device *dev, void *res,
1360 void *match_data)
1361{
1362 struct fw_name_devm *fwn = res;
1363 return (fwn->magic == (unsigned long)match_data);
1364}
1365
Ming Leiab6dd8e2012-08-17 22:07:00 +08001366static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001367{
1368 LIST_HEAD(todo);
1369 struct fw_cache_entry *fce;
1370 struct fw_cache_entry *fce_next;
1371 struct firmware_cache *fwc = &fw_cache;
1372
1373 devres_for_each_res(dev, fw_name_devm_release,
1374 devm_name_match, &fw_cache,
1375 dev_create_fw_entry, &todo);
1376
1377 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1378 list_del(&fce->list);
1379
1380 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001381 /* only one cache entry for one firmware */
1382 if (!__fw_entry_found(fce->name)) {
Ming Lei373304f2012-10-09 12:01:01 +08001383 list_add(&fce->list, &fwc->fw_names);
1384 } else {
1385 free_fw_cache_entry(fce);
1386 fce = NULL;
1387 }
Ming Lei37276a52012-08-04 12:01:27 +08001388 spin_unlock(&fwc->name_lock);
1389
Ming Lei373304f2012-10-09 12:01:01 +08001390 if (fce)
Ming Leid28d3882012-10-09 12:01:04 +08001391 async_schedule_domain(__async_dev_cache_fw_image,
1392 (void *)fce,
1393 &fw_cache_domain);
Ming Lei37276a52012-08-04 12:01:27 +08001394 }
1395}
1396
1397static void __device_uncache_fw_images(void)
1398{
1399 struct firmware_cache *fwc = &fw_cache;
1400 struct fw_cache_entry *fce;
1401
1402 spin_lock(&fwc->name_lock);
1403 while (!list_empty(&fwc->fw_names)) {
1404 fce = list_entry(fwc->fw_names.next,
1405 struct fw_cache_entry, list);
1406 list_del(&fce->list);
1407 spin_unlock(&fwc->name_lock);
1408
1409 uncache_firmware(fce->name);
1410 free_fw_cache_entry(fce);
1411
1412 spin_lock(&fwc->name_lock);
1413 }
1414 spin_unlock(&fwc->name_lock);
1415}
1416
Ming Leiaf5bc112013-05-27 18:30:04 +08001417/* kill pending requests without uevent to avoid blocking suspend */
1418static void kill_requests_without_uevent(void)
1419{
1420 struct firmware_buf *buf;
1421 struct firmware_buf *next;
1422
1423 mutex_lock(&fw_lock);
1424 list_for_each_entry_safe(buf, next, &pending_fw_head, pending_list) {
1425 if (!buf->need_uevent)
1426 fw_load_abort(buf);
1427 }
1428 mutex_unlock(&fw_lock);
1429}
1430
Ming Lei37276a52012-08-04 12:01:27 +08001431/**
1432 * device_cache_fw_images - cache devices' firmware
1433 *
1434 * If one device called request_firmware or its nowait version
1435 * successfully before, the firmware names are recored into the
1436 * device's devres link list, so device_cache_fw_images can call
1437 * cache_firmware() to cache these firmwares for the device,
1438 * then the device driver can load its firmwares easily at
1439 * time when system is not ready to complete loading firmware.
1440 */
1441static void device_cache_fw_images(void)
1442{
1443 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001444 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001445 DEFINE_WAIT(wait);
1446
1447 pr_debug("%s\n", __func__);
1448
Ming Lei373304f2012-10-09 12:01:01 +08001449 /* cancel uncache work */
1450 cancel_delayed_work_sync(&fwc->work);
1451
Ming Leiffe53f62012-08-04 12:01:28 +08001452 /*
1453 * use small loading timeout for caching devices' firmware
1454 * because all these firmware images have been loaded
1455 * successfully at lease once, also system is ready for
1456 * completing firmware loading now. The maximum size of
1457 * firmware in current distributions is about 2M bytes,
1458 * so 10 secs should be enough.
1459 */
1460 old_timeout = loading_timeout;
1461 loading_timeout = 10;
1462
Ming Leiac39b3e2012-08-20 19:04:16 +08001463 mutex_lock(&fw_lock);
1464 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001465 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001466 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001467
1468 /* wait for completion of caching firmware for all devices */
Ming Leid28d3882012-10-09 12:01:04 +08001469 async_synchronize_full_domain(&fw_cache_domain);
Ming Leiffe53f62012-08-04 12:01:28 +08001470
1471 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001472}
1473
1474/**
1475 * device_uncache_fw_images - uncache devices' firmware
1476 *
1477 * uncache all firmwares which have been cached successfully
1478 * by device_uncache_fw_images earlier
1479 */
1480static void device_uncache_fw_images(void)
1481{
1482 pr_debug("%s\n", __func__);
1483 __device_uncache_fw_images();
1484}
1485
1486static void device_uncache_fw_images_work(struct work_struct *work)
1487{
1488 device_uncache_fw_images();
1489}
1490
1491/**
1492 * device_uncache_fw_images_delay - uncache devices firmwares
1493 * @delay: number of milliseconds to delay uncache device firmwares
1494 *
1495 * uncache all devices's firmwares which has been cached successfully
1496 * by device_cache_fw_images after @delay milliseconds.
1497 */
1498static void device_uncache_fw_images_delay(unsigned long delay)
1499{
1500 schedule_delayed_work(&fw_cache.work,
1501 msecs_to_jiffies(delay));
1502}
1503
Ming Lei07646d92012-08-04 12:01:29 +08001504static int fw_pm_notify(struct notifier_block *notify_block,
1505 unsigned long mode, void *unused)
1506{
1507 switch (mode) {
1508 case PM_HIBERNATION_PREPARE:
1509 case PM_SUSPEND_PREPARE:
Ming Leiaf5bc112013-05-27 18:30:04 +08001510 kill_requests_without_uevent();
Ming Lei07646d92012-08-04 12:01:29 +08001511 device_cache_fw_images();
1512 break;
1513
1514 case PM_POST_SUSPEND:
1515 case PM_POST_HIBERNATION:
1516 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001517 /*
1518 * In case that system sleep failed and syscore_suspend is
1519 * not called.
1520 */
1521 mutex_lock(&fw_lock);
1522 fw_cache.state = FW_LOADER_NO_CACHE;
1523 mutex_unlock(&fw_lock);
1524
Ming Lei07646d92012-08-04 12:01:29 +08001525 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1526 break;
1527 }
1528
1529 return 0;
1530}
Ming Lei07646d92012-08-04 12:01:29 +08001531
Ming Leiac39b3e2012-08-20 19:04:16 +08001532/* stop caching firmware once syscore_suspend is reached */
1533static int fw_suspend(void)
1534{
1535 fw_cache.state = FW_LOADER_NO_CACHE;
1536 return 0;
1537}
1538
1539static struct syscore_ops fw_syscore_ops = {
1540 .suspend = fw_suspend,
1541};
Ming Leicfe016b2012-09-08 17:32:30 +08001542#else
1543static int fw_cache_piggyback_on_request(const char *name)
1544{
1545 return 0;
1546}
1547#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001548
Ming Lei37276a52012-08-04 12:01:27 +08001549static void __init fw_cache_init(void)
1550{
1551 spin_lock_init(&fw_cache.lock);
1552 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001553 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001554
Ming Leicfe016b2012-09-08 17:32:30 +08001555#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +08001556 spin_lock_init(&fw_cache.name_lock);
1557 INIT_LIST_HEAD(&fw_cache.fw_names);
Ming Lei37276a52012-08-04 12:01:27 +08001558
Ming Lei37276a52012-08-04 12:01:27 +08001559 INIT_DELAYED_WORK(&fw_cache.work,
1560 device_uncache_fw_images_work);
Ming Lei07646d92012-08-04 12:01:29 +08001561
1562 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1563 register_pm_notifier(&fw_cache.pm_notify);
Ming Leiac39b3e2012-08-20 19:04:16 +08001564
1565 register_syscore_ops(&fw_syscore_ops);
Ming Leicfe016b2012-09-08 17:32:30 +08001566#endif
Ming Lei37276a52012-08-04 12:01:27 +08001567}
1568
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001569static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
Ming Lei1f2b7952012-08-04 12:01:21 +08001571 fw_cache_init();
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001572#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaife304142013-05-22 18:28:38 +02001573 register_reboot_notifier(&fw_shutdown_nb);
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001574 return class_register(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001575#else
1576 return 0;
1577#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001579
1580static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
Ming Leicfe016b2012-09-08 17:32:30 +08001582#ifdef CONFIG_PM_SLEEP
Ming Leiac39b3e2012-08-20 19:04:16 +08001583 unregister_syscore_ops(&fw_syscore_ops);
Ming Lei07646d92012-08-04 12:01:29 +08001584 unregister_pm_notifier(&fw_cache.pm_notify);
Ming Leicfe016b2012-09-08 17:32:30 +08001585#endif
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001586#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaife304142013-05-22 18:28:38 +02001587 unregister_reboot_notifier(&fw_shutdown_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 class_unregister(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001589#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590}
1591
Shaohua Lia30a6a22006-09-27 01:50:52 -07001592fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593module_exit(firmware_class_exit);