blob: f3c6434b586b29f3dfe611187a3053770b715457 [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;
David Woodhouse6e03a202009-04-09 22:04:07 -0700131 struct page **pages;
132 int nr_pages;
133 int page_array_size;
Takashi Iwaife304142013-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 Iwaife304142013-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 Viro3dadecc2013-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 Iwaife304142013-05-22 18:28:38 +0200454static void fw_load_abort(struct firmware_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Takashi Iwaife304142013-05-22 18:28:38 +0200456 list_del_init(&buf->pending_list);
Ming Lei12446912012-08-04 12:01:20 +0800457 set_bit(FW_STATUS_ABORT, &buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800458 complete_all(&buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
Takashi Iwai807be032013-01-31 11:13:57 +0100461#define is_fw_load_aborted(buf) \
462 test_bit(FW_STATUS_ABORT, &(buf)->status)
463
Takashi Iwaife304142013-05-22 18:28:38 +0200464static LIST_HEAD(pending_fw_head);
465
466/* reboot notifier for avoid deadlock with usermode_lock */
467static int fw_shutdown_notify(struct notifier_block *unused1,
468 unsigned long unused2, void *unused3)
469{
470 mutex_lock(&fw_lock);
471 while (!list_empty(&pending_fw_head))
472 fw_load_abort(list_first_entry(&pending_fw_head,
473 struct firmware_buf,
474 pending_list));
475 mutex_unlock(&fw_lock);
476 return NOTIFY_DONE;
477}
478
479static struct notifier_block fw_shutdown_nb = {
480 .notifier_call = fw_shutdown_notify,
481};
482
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700483static ssize_t firmware_timeout_show(struct class *class,
484 struct class_attribute *attr,
485 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 return sprintf(buf, "%d\n", loading_timeout);
488}
489
490/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800491 * firmware_timeout_store - set number of seconds to wait for firmware
492 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800493 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800494 * @buf: buffer to scan for timeout value
495 * @count: number of bytes in @buf
496 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800498 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 * firmware will be provided.
500 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800501 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700503static ssize_t firmware_timeout_store(struct class *class,
504 struct class_attribute *attr,
505 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700508 if (loading_timeout < 0)
509 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return count;
512}
513
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800514static struct class_attribute firmware_class_attrs[] = {
515 __ATTR(timeout, S_IWUSR | S_IRUGO,
516 firmware_timeout_show, firmware_timeout_store),
517 __ATTR_NULL
518};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800520static void fw_dev_release(struct device *dev)
521{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700522 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800523
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800524 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800525
526 module_put(THIS_MODULE);
527}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Kay Sievers7eff2e72007-08-14 15:15:12 +0200529static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700531 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Ming Lei12446912012-08-04 12:01:20 +0800533 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200535 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700536 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200537 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
538 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 return 0;
541}
542
Adrian Bunk1b81d662006-05-20 15:00:16 -0700543static struct class firmware_class = {
544 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800545 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700546 .dev_uevent = firmware_uevent,
547 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700548};
549
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700550static ssize_t firmware_loading_show(struct device *dev,
551 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700553 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800554 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return sprintf(buf, "%d\n", loading);
557}
558
David Woodhouse6e03a202009-04-09 22:04:07 -0700559/* Some architectures don't have PAGE_KERNEL_RO */
560#ifndef PAGE_KERNEL_RO
561#define PAGE_KERNEL_RO PAGE_KERNEL
562#endif
Ming Lei253c9242012-10-09 12:01:02 +0800563
564/* one pages buffer should be mapped/unmapped only once */
565static int fw_map_pages_buf(struct firmware_buf *buf)
566{
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100567 if (!buf->is_paged_buf)
Ming Lei746058f2012-10-09 12:01:03 +0800568 return 0;
569
Ming Lei253c9242012-10-09 12:01:02 +0800570 if (buf->data)
571 vunmap(buf->data);
572 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
573 if (!buf->data)
574 return -ENOMEM;
575 return 0;
576}
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800579 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700580 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800581 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800582 * @buf: buffer to scan for loading control value
583 * @count: number of bytes in @buf
584 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 * The relevant values are:
586 *
587 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800588 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 * -1: Conclude the load with an error and discard any written data.
590 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700591static ssize_t firmware_loading_store(struct device *dev,
592 struct device_attribute *attr,
593 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700595 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800596 struct firmware_buf *fw_buf = fw_priv->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700598 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Neil Hormaneea915b2012-01-02 15:31:23 -0500600 mutex_lock(&fw_lock);
601
Ming Lei12446912012-08-04 12:01:20 +0800602 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500603 goto out;
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 switch (loading) {
606 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800607 /* discarding any previous partial load */
Ming Lei12446912012-08-04 12:01:20 +0800608 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
609 for (i = 0; i < fw_buf->nr_pages; i++)
610 __free_page(fw_buf->pages[i]);
611 kfree(fw_buf->pages);
612 fw_buf->pages = NULL;
613 fw_buf->page_array_size = 0;
614 fw_buf->nr_pages = 0;
615 set_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei28eefa72012-08-04 12:01:17 +0800616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 break;
618 case 0:
Ming Lei12446912012-08-04 12:01:20 +0800619 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
620 set_bit(FW_STATUS_DONE, &fw_buf->status);
621 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei253c9242012-10-09 12:01:02 +0800622
623 /*
624 * Several loading requests may be pending on
625 * one same firmware buf, so let all requests
626 * see the mapped 'buf->data' once the loading
627 * is completed.
628 * */
629 fw_map_pages_buf(fw_buf);
Takashi Iwaife304142013-05-22 18:28:38 +0200630 list_del_init(&fw_buf->pending_list);
Ming Lei1f2b7952012-08-04 12:01:21 +0800631 complete_all(&fw_buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 break;
633 }
634 /* fallthrough */
635 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700636 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 /* fallthrough */
638 case -1:
Takashi Iwaife304142013-05-22 18:28:38 +0200639 fw_load_abort(fw_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 break;
641 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500642out:
643 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return count;
645}
646
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700647static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700649static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
650 struct bin_attribute *bin_attr,
651 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200653 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700654 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800655 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700656 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Laura Garciacad1e552006-05-23 23:22:38 +0200658 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800659 buf = fw_priv->buf;
660 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 ret_count = -ENODEV;
662 goto out;
663 }
Ming Lei12446912012-08-04 12:01:20 +0800664 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200665 ret_count = 0;
666 goto out;
667 }
Ming Lei12446912012-08-04 12:01:20 +0800668 if (count > buf->size - offset)
669 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700670
671 ret_count = count;
672
673 while (count) {
674 void *page_data;
675 int page_nr = offset >> PAGE_SHIFT;
676 int page_ofs = offset & (PAGE_SIZE-1);
677 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
678
Ming Lei12446912012-08-04 12:01:20 +0800679 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700680
681 memcpy(buffer, page_data + page_ofs, page_cnt);
682
Ming Lei12446912012-08-04 12:01:20 +0800683 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700684 buffer += page_cnt;
685 offset += page_cnt;
686 count -= page_cnt;
687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688out:
Laura Garciacad1e552006-05-23 23:22:38 +0200689 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return ret_count;
691}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800692
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700693static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Ming Lei12446912012-08-04 12:01:20 +0800695 struct firmware_buf *buf = fw_priv->buf;
David Woodhouse6e03a202009-04-09 22:04:07 -0700696 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
David Woodhouse6e03a202009-04-09 22:04:07 -0700698 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800699 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700700 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800701 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700702 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
David Woodhouse6e03a202009-04-09 22:04:07 -0700704 new_pages = kmalloc(new_array_size * sizeof(void *),
705 GFP_KERNEL);
706 if (!new_pages) {
Takashi Iwaife304142013-05-22 18:28:38 +0200707 fw_load_abort(buf);
David Woodhouse6e03a202009-04-09 22:04:07 -0700708 return -ENOMEM;
709 }
Ming Lei12446912012-08-04 12:01:20 +0800710 memcpy(new_pages, buf->pages,
711 buf->page_array_size * sizeof(void *));
712 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
713 (new_array_size - buf->page_array_size));
714 kfree(buf->pages);
715 buf->pages = new_pages;
716 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700718
Ming Lei12446912012-08-04 12:01:20 +0800719 while (buf->nr_pages < pages_needed) {
720 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700721 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
722
Ming Lei12446912012-08-04 12:01:20 +0800723 if (!buf->pages[buf->nr_pages]) {
Takashi Iwaife304142013-05-22 18:28:38 +0200724 fw_load_abort(buf);
David Woodhouse6e03a202009-04-09 22:04:07 -0700725 return -ENOMEM;
726 }
Ming Lei12446912012-08-04 12:01:20 +0800727 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return 0;
730}
731
732/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800733 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700734 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700735 * @kobj: kobject for the device
Randy Dunlap42e61f4a2007-07-23 21:42:11 -0700736 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800737 * @buffer: buffer being written
738 * @offset: buffer offset for write in total data store area
739 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800741 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 * the driver as a firmware image.
743 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700744static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
745 struct bin_attribute *bin_attr,
746 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200748 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700749 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800750 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 ssize_t retval;
752
753 if (!capable(CAP_SYS_RAWIO))
754 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700755
Laura Garciacad1e552006-05-23 23:22:38 +0200756 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800757 buf = fw_priv->buf;
758 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 retval = -ENODEV;
760 goto out;
761 }
Ming Lei65710cb2012-08-04 12:01:16 +0800762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 retval = fw_realloc_buffer(fw_priv, offset + count);
764 if (retval)
765 goto out;
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700768
769 while (count) {
770 void *page_data;
771 int page_nr = offset >> PAGE_SHIFT;
772 int page_ofs = offset & (PAGE_SIZE - 1);
773 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
774
Ming Lei12446912012-08-04 12:01:20 +0800775 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700776
777 memcpy(page_data + page_ofs, buffer, page_cnt);
778
Ming Lei12446912012-08-04 12:01:20 +0800779 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700780 buffer += page_cnt;
781 offset += page_cnt;
782 count -= page_cnt;
783 }
784
Ming Lei12446912012-08-04 12:01:20 +0800785 buf->size = max_t(size_t, offset, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786out:
Laura Garciacad1e552006-05-23 23:22:38 +0200787 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return retval;
789}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800790
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700791static struct bin_attribute firmware_attr_data = {
792 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 .size = 0,
794 .read = firmware_data_read,
795 .write = firmware_data_write,
796};
797
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800798static void firmware_class_timeout_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800800 struct firmware_priv *fw_priv = container_of(work,
801 struct firmware_priv, timeout_work.work);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700802
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800803 mutex_lock(&fw_lock);
804 if (test_bit(FW_STATUS_DONE, &(fw_priv->buf->status))) {
805 mutex_unlock(&fw_lock);
806 return;
807 }
Takashi Iwaife304142013-05-22 18:28:38 +0200808 fw_load_abort(fw_priv->buf);
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800809 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700812static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200813fw_create_instance(struct firmware *firmware, const char *fw_name,
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700814 struct device *device, bool uevent, bool nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700816 struct firmware_priv *fw_priv;
817 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Ming Lei12446912012-08-04 12:01:20 +0800819 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700820 if (!fw_priv) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700821 dev_err(device, "%s: kmalloc failed\n", __func__);
Ming Lei12446912012-08-04 12:01:20 +0800822 fw_priv = ERR_PTR(-ENOMEM);
823 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700826 fw_priv->nowait = nowait;
Ming Lei1f2b7952012-08-04 12:01:21 +0800827 fw_priv->fw = firmware;
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800828 INIT_DELAYED_WORK(&fw_priv->timeout_work,
829 firmware_class_timeout_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700831 f_dev = &fw_priv->dev;
832
833 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800834 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700835 f_dev->parent = device;
836 f_dev->class = &firmware_class;
Ming Lei12446912012-08-04 12:01:20 +0800837exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700838 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100840
841/* load a firmware via user helper */
842static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
843 long timeout)
844{
845 int retval = 0;
846 struct device *f_dev = &fw_priv->dev;
847 struct firmware_buf *buf = fw_priv->buf;
848
849 /* fall back on userspace loading */
850 buf->is_paged_buf = true;
851
852 dev_set_uevent_suppress(f_dev, true);
853
854 /* Need to pin this module until class device is destroyed */
855 __module_get(THIS_MODULE);
856
857 retval = device_add(f_dev);
858 if (retval) {
859 dev_err(f_dev, "%s: device_register failed\n", __func__);
860 goto err_put_dev;
861 }
862
863 retval = device_create_bin_file(f_dev, &firmware_attr_data);
864 if (retval) {
865 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
866 goto err_del_dev;
867 }
868
869 retval = device_create_file(f_dev, &dev_attr_loading);
870 if (retval) {
871 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
872 goto err_del_bin_attr;
873 }
874
875 if (uevent) {
876 dev_set_uevent_suppress(f_dev, false);
877 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
878 if (timeout != MAX_SCHEDULE_TIMEOUT)
879 schedule_delayed_work(&fw_priv->timeout_work, timeout);
880
881 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
882 }
883
Takashi Iwaife304142013-05-22 18:28:38 +0200884 mutex_lock(&fw_lock);
885 list_add(&buf->pending_list, &pending_fw_head);
886 mutex_unlock(&fw_lock);
887
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100888 wait_for_completion(&buf->completion);
889
890 cancel_delayed_work_sync(&fw_priv->timeout_work);
891
892 fw_priv->buf = NULL;
893
894 device_remove_file(f_dev, &dev_attr_loading);
895err_del_bin_attr:
896 device_remove_bin_file(f_dev, &firmware_attr_data);
897err_del_dev:
898 device_del(f_dev);
899err_put_dev:
900 put_device(f_dev);
901 return retval;
902}
903
904static int fw_load_from_user_helper(struct firmware *firmware,
905 const char *name, struct device *device,
906 bool uevent, bool nowait, long timeout)
907{
908 struct firmware_priv *fw_priv;
909
910 fw_priv = fw_create_instance(firmware, name, device, uevent, nowait);
911 if (IS_ERR(fw_priv))
912 return PTR_ERR(fw_priv);
913
914 fw_priv->buf = firmware->priv;
915 return _request_firmware_load(fw_priv, uevent, timeout);
916}
917#else /* CONFIG_FW_LOADER_USER_HELPER */
918static inline int
919fw_load_from_user_helper(struct firmware *firmware, const char *name,
920 struct device *device, bool uevent, bool nowait,
921 long timeout)
922{
923 return -ENOENT;
924}
Takashi Iwai807be032013-01-31 11:13:57 +0100925
926/* No abort during direct loading */
927#define is_fw_load_aborted(buf) false
928
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100929#endif /* CONFIG_FW_LOADER_USER_HELPER */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Ming Leif531f05a2012-08-04 12:01:25 +0800931
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100932/* wait until the shared firmware_buf becomes ready (or error) */
933static int sync_cached_firmware_buf(struct firmware_buf *buf)
Ming Lei1f2b7952012-08-04 12:01:21 +0800934{
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100935 int ret = 0;
936
937 mutex_lock(&fw_lock);
938 while (!test_bit(FW_STATUS_DONE, &buf->status)) {
Takashi Iwai807be032013-01-31 11:13:57 +0100939 if (is_fw_load_aborted(buf)) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100940 ret = -ENOENT;
941 break;
942 }
943 mutex_unlock(&fw_lock);
944 wait_for_completion(&buf->completion);
945 mutex_lock(&fw_lock);
946 }
947 mutex_unlock(&fw_lock);
948 return ret;
Ming Lei1f2b7952012-08-04 12:01:21 +0800949}
950
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100951/* prepare firmware and firmware_buf structs;
952 * return 0 if a firmware is already assigned, 1 if need to load one,
953 * or a negative error code
954 */
955static int
956_request_firmware_prepare(struct firmware **firmware_p, const char *name,
957 struct device *device)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700958{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +0800960 struct firmware_buf *buf;
961 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Jiri Slaby4aed0642005-09-13 01:25:01 -0700963 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700965 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
966 __func__);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100967 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800970 if (fw_get_builtin_firmware(firmware, name)) {
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100971 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100972 return 0; /* assigned */
David Woodhouse5658c762008-05-23 13:52:42 +0100973 }
974
Ming Lei1f2b7952012-08-04 12:01:21 +0800975 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
Ming Lei1f2b7952012-08-04 12:01:21 +0800976
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100977 /*
978 * bind with 'buf' now to avoid warning in failure path
979 * of requesting firmware.
980 */
981 firmware->priv = buf;
Ming Lei1f2b7952012-08-04 12:01:21 +0800982
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100983 if (ret > 0) {
984 ret = sync_cached_firmware_buf(buf);
985 if (!ret) {
986 fw_set_page_data(buf, firmware);
987 return 0; /* assigned */
988 }
Stephen Boyddddb5542012-03-28 23:30:43 +0200989 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800990
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100991 if (ret < 0)
992 return ret;
993 return 1; /* need to load */
Rafael J. Wysocki811fa402012-03-28 23:29:55 +0200994}
995
Ming Leie771d1a2013-05-27 18:30:03 +0800996static int assign_firmware_buf(struct firmware *fw, struct device *device,
997 bool skip_cache)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100998{
999 struct firmware_buf *buf = fw->priv;
1000
1001 mutex_lock(&fw_lock);
Takashi Iwai807be032013-01-31 11:13:57 +01001002 if (!buf->size || is_fw_load_aborted(buf)) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001003 mutex_unlock(&fw_lock);
1004 return -ENOENT;
1005 }
1006
1007 /*
1008 * add firmware name into devres list so that we can auto cache
1009 * and uncache firmware for device.
1010 *
1011 * device may has been deleted already, but the problem
1012 * should be fixed in devres or driver core.
1013 */
Ming Leie771d1a2013-05-27 18:30:03 +08001014 if (device && !skip_cache)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001015 fw_add_devm_name(device, buf->fw_id);
1016
1017 /*
1018 * After caching firmware image is started, let it piggyback
1019 * on request firmware.
1020 */
1021 if (buf->fwc->state == FW_LOADER_START_CACHE) {
1022 if (fw_cache_piggyback_on_request(buf->fw_id))
1023 kref_get(&buf->ref);
1024 }
1025
1026 /* pass the pages buffer to driver at the last minute */
1027 fw_set_page_data(buf, fw);
1028 mutex_unlock(&fw_lock);
1029 return 0;
1030}
1031
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001032/* called from request_firmware() and request_firmware_work_func() */
1033static int
1034_request_firmware(const struct firmware **firmware_p, const char *name,
1035 struct device *device, bool uevent, bool nowait)
1036{
1037 struct firmware *fw;
1038 long timeout;
1039 int ret;
1040
1041 if (!firmware_p)
1042 return -EINVAL;
1043
1044 ret = _request_firmware_prepare(&fw, name, device);
1045 if (ret <= 0) /* error or already assigned */
1046 goto out;
1047
1048 ret = 0;
1049 timeout = firmware_loading_timeout();
1050 if (nowait) {
1051 timeout = usermodehelper_read_lock_wait(timeout);
1052 if (!timeout) {
1053 dev_dbg(device, "firmware: %s loading timed out\n",
1054 name);
1055 ret = -EBUSY;
1056 goto out;
1057 }
1058 } else {
1059 ret = usermodehelper_read_trylock();
1060 if (WARN_ON(ret)) {
1061 dev_err(device, "firmware: %s will not be loaded\n",
1062 name);
1063 goto out;
1064 }
1065 }
1066
1067 if (!fw_get_filesystem_firmware(device, fw->priv))
1068 ret = fw_load_from_user_helper(fw, name, device,
1069 uevent, nowait, timeout);
Ming Leie771d1a2013-05-27 18:30:03 +08001070
1071 /* don't cache firmware handled without uevent */
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001072 if (!ret)
Ming Leie771d1a2013-05-27 18:30:03 +08001073 ret = assign_firmware_buf(fw, device, !uevent);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001074
1075 usermodehelper_read_unlock();
1076
1077 out:
1078 if (ret < 0) {
1079 release_firmware(fw);
1080 fw = NULL;
1081 }
1082
1083 *firmware_p = fw;
1084 return ret;
1085}
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087/**
Kay Sievers312c0042005-11-16 09:00:00 +01001088 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001089 * @firmware_p: pointer to firmware image
1090 * @name: name of firmware file
1091 * @device: device for which firmware is being loaded
1092 *
1093 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab02005-09-06 15:17:13 -07001094 * of @name for device @device.
1095 *
1096 * Should be called from user context where sleeping is allowed.
1097 *
Kay Sievers312c0042005-11-16 09:00:00 +01001098 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab02005-09-06 15:17:13 -07001099 * should be distinctive enough not to be confused with any other
1100 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +08001101 *
1102 * Caller must hold the reference count of @device.
Ming Lei6a927852012-11-03 17:48:16 +08001103 *
1104 * The function can be called safely inside device's suspend and
1105 * resume callback.
Abhay Salunke6e3eaab02005-09-06 15:17:13 -07001106 **/
1107int
1108request_firmware(const struct firmware **firmware_p, const char *name,
1109 struct device *device)
1110{
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001111 return _request_firmware(firmware_p, name, device, true, false);
Abhay Salunke6e3eaab02005-09-06 15:17:13 -07001112}
Daniel Mackf4945132013-05-23 22:17:18 +02001113EXPORT_SYMBOL(request_firmware);
Abhay Salunke6e3eaab02005-09-06 15:17:13 -07001114
1115/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001117 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001119void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
1121 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001122 if (!fw_is_builtin_firmware(fw))
1123 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 kfree(fw);
1125 }
1126}
Daniel Mackf4945132013-05-23 22:17:18 +02001127EXPORT_SYMBOL(release_firmware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129/* Async support */
1130struct firmware_work {
1131 struct work_struct work;
1132 struct module *module;
1133 const char *name;
1134 struct device *device;
1135 void *context;
1136 void (*cont)(const struct firmware *fw, void *context);
Bob Liu072fc8f2011-01-26 18:33:32 +08001137 bool uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138};
1139
Stephen Boyda36cf842012-03-28 23:31:00 +02001140static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
Stephen Boyda36cf842012-03-28 23:31:00 +02001142 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 const struct firmware *fw;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001144
Stephen Boyda36cf842012-03-28 23:31:00 +02001145 fw_work = container_of(work, struct firmware_work, work);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001146
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001147 _request_firmware(&fw, fw_work->name, fw_work->device,
1148 fw_work->uevent, true);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001149 fw_work->cont(fw, fw_work->context);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001150 put_device(fw_work->device); /* taken in request_firmware_nowait() */
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 module_put(fw_work->module);
1153 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
1156/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001157 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001158 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001159 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001160 * is non-zero else the firmware copy must be done manually.
1161 * @name: name of firmware file
1162 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001163 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001164 * @context: will be passed over to @cont, and
1165 * @fw may be %NULL if firmware request fails.
1166 * @cont: function will be called asynchronously when the firmware
1167 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001169 * Caller must hold the reference count of @device.
1170 *
Ming Lei6f21a622012-08-04 12:01:24 +08001171 * Asynchronous variant of request_firmware() for user contexts:
1172 * - sleep for as small periods as possible since it may
1173 * increase kernel boot time of built-in device drivers
1174 * requesting firmware in their ->probe() methods, if
1175 * @gfp is GFP_KERNEL.
1176 *
1177 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 **/
1179int
1180request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001181 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001182 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 void (*cont)(const struct firmware *fw, void *context))
1184{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001185 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001187 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 if (!fw_work)
1189 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001190
1191 fw_work->module = module;
1192 fw_work->name = name;
1193 fw_work->device = device;
1194 fw_work->context = context;
1195 fw_work->cont = cont;
1196 fw_work->uevent = uevent;
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 if (!try_module_get(module)) {
1199 kfree(fw_work);
1200 return -EFAULT;
1201 }
1202
Ming Lei0cfc1e12012-08-04 12:01:23 +08001203 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001204 INIT_WORK(&fw_work->work, request_firmware_work_func);
1205 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 return 0;
1207}
Daniel Mackf4945132013-05-23 22:17:18 +02001208EXPORT_SYMBOL(request_firmware_nowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Ming Lei2887b392012-08-04 12:01:22 +08001210/**
1211 * cache_firmware - cache one firmware image in kernel memory space
1212 * @fw_name: the firmware image name
1213 *
1214 * Cache firmware in kernel memory so that drivers can use it when
1215 * system isn't ready for them to request firmware image from userspace.
1216 * Once it returns successfully, driver can use request_firmware or its
1217 * nowait version to get the cached firmware without any interacting
1218 * with userspace
1219 *
1220 * Return 0 if the firmware image has been cached successfully
1221 * Return !0 otherwise
1222 *
1223 */
1224int cache_firmware(const char *fw_name)
1225{
1226 int ret;
1227 const struct firmware *fw;
1228
1229 pr_debug("%s: %s\n", __func__, fw_name);
1230
1231 ret = request_firmware(&fw, fw_name, NULL);
1232 if (!ret)
1233 kfree(fw);
1234
1235 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1236
1237 return ret;
1238}
Daniel Mackf4945132013-05-23 22:17:18 +02001239EXPORT_SYMBOL_GPL(cache_firmware);
Ming Lei2887b392012-08-04 12:01:22 +08001240
1241/**
1242 * uncache_firmware - remove one cached firmware image
1243 * @fw_name: the firmware image name
1244 *
1245 * Uncache one firmware image which has been cached successfully
1246 * before.
1247 *
1248 * Return 0 if the firmware cache has been removed successfully
1249 * Return !0 otherwise
1250 *
1251 */
1252int uncache_firmware(const char *fw_name)
1253{
1254 struct firmware_buf *buf;
1255 struct firmware fw;
1256
1257 pr_debug("%s: %s\n", __func__, fw_name);
1258
1259 if (fw_get_builtin_firmware(&fw, fw_name))
1260 return 0;
1261
1262 buf = fw_lookup_buf(fw_name);
1263 if (buf) {
1264 fw_free_buf(buf);
1265 return 0;
1266 }
1267
1268 return -EINVAL;
1269}
Daniel Mackf4945132013-05-23 22:17:18 +02001270EXPORT_SYMBOL_GPL(uncache_firmware);
Ming Lei2887b392012-08-04 12:01:22 +08001271
Ming Leicfe016b2012-09-08 17:32:30 +08001272#ifdef CONFIG_PM_SLEEP
Ming Leid28d3882012-10-09 12:01:04 +08001273static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1274
Ming Lei37276a52012-08-04 12:01:27 +08001275static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1276{
1277 struct fw_cache_entry *fce;
1278
1279 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1280 if (!fce)
1281 goto exit;
1282
1283 strcpy(fce->name, name);
1284exit:
1285 return fce;
1286}
1287
Ming Lei373304f2012-10-09 12:01:01 +08001288static int __fw_entry_found(const char *name)
1289{
1290 struct firmware_cache *fwc = &fw_cache;
1291 struct fw_cache_entry *fce;
1292
1293 list_for_each_entry(fce, &fwc->fw_names, list) {
1294 if (!strcmp(fce->name, name))
1295 return 1;
1296 }
1297 return 0;
1298}
1299
Ming Leiac39b3e2012-08-20 19:04:16 +08001300static int fw_cache_piggyback_on_request(const char *name)
1301{
1302 struct firmware_cache *fwc = &fw_cache;
1303 struct fw_cache_entry *fce;
1304 int ret = 0;
1305
1306 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001307 if (__fw_entry_found(name))
1308 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001309
1310 fce = alloc_fw_cache_entry(name);
1311 if (fce) {
1312 ret = 1;
1313 list_add(&fce->list, &fwc->fw_names);
1314 pr_debug("%s: fw: %s\n", __func__, name);
1315 }
1316found:
1317 spin_unlock(&fwc->name_lock);
1318 return ret;
1319}
1320
Ming Lei37276a52012-08-04 12:01:27 +08001321static void free_fw_cache_entry(struct fw_cache_entry *fce)
1322{
1323 kfree(fce);
1324}
1325
1326static void __async_dev_cache_fw_image(void *fw_entry,
1327 async_cookie_t cookie)
1328{
1329 struct fw_cache_entry *fce = fw_entry;
1330 struct firmware_cache *fwc = &fw_cache;
1331 int ret;
1332
1333 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001334 if (ret) {
1335 spin_lock(&fwc->name_lock);
1336 list_del(&fce->list);
1337 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001338
Ming Leiac39b3e2012-08-20 19:04:16 +08001339 free_fw_cache_entry(fce);
1340 }
Ming Lei37276a52012-08-04 12:01:27 +08001341}
1342
1343/* called with dev->devres_lock held */
1344static void dev_create_fw_entry(struct device *dev, void *res,
1345 void *data)
1346{
1347 struct fw_name_devm *fwn = res;
1348 const char *fw_name = fwn->name;
1349 struct list_head *head = data;
1350 struct fw_cache_entry *fce;
1351
1352 fce = alloc_fw_cache_entry(fw_name);
1353 if (fce)
1354 list_add(&fce->list, head);
1355}
1356
1357static int devm_name_match(struct device *dev, void *res,
1358 void *match_data)
1359{
1360 struct fw_name_devm *fwn = res;
1361 return (fwn->magic == (unsigned long)match_data);
1362}
1363
Ming Leiab6dd8e2012-08-17 22:07:00 +08001364static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001365{
1366 LIST_HEAD(todo);
1367 struct fw_cache_entry *fce;
1368 struct fw_cache_entry *fce_next;
1369 struct firmware_cache *fwc = &fw_cache;
1370
1371 devres_for_each_res(dev, fw_name_devm_release,
1372 devm_name_match, &fw_cache,
1373 dev_create_fw_entry, &todo);
1374
1375 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1376 list_del(&fce->list);
1377
1378 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001379 /* only one cache entry for one firmware */
1380 if (!__fw_entry_found(fce->name)) {
Ming Lei373304f2012-10-09 12:01:01 +08001381 list_add(&fce->list, &fwc->fw_names);
1382 } else {
1383 free_fw_cache_entry(fce);
1384 fce = NULL;
1385 }
Ming Lei37276a52012-08-04 12:01:27 +08001386 spin_unlock(&fwc->name_lock);
1387
Ming Lei373304f2012-10-09 12:01:01 +08001388 if (fce)
Ming Leid28d3882012-10-09 12:01:04 +08001389 async_schedule_domain(__async_dev_cache_fw_image,
1390 (void *)fce,
1391 &fw_cache_domain);
Ming Lei37276a52012-08-04 12:01:27 +08001392 }
1393}
1394
1395static void __device_uncache_fw_images(void)
1396{
1397 struct firmware_cache *fwc = &fw_cache;
1398 struct fw_cache_entry *fce;
1399
1400 spin_lock(&fwc->name_lock);
1401 while (!list_empty(&fwc->fw_names)) {
1402 fce = list_entry(fwc->fw_names.next,
1403 struct fw_cache_entry, list);
1404 list_del(&fce->list);
1405 spin_unlock(&fwc->name_lock);
1406
1407 uncache_firmware(fce->name);
1408 free_fw_cache_entry(fce);
1409
1410 spin_lock(&fwc->name_lock);
1411 }
1412 spin_unlock(&fwc->name_lock);
1413}
1414
1415/**
1416 * device_cache_fw_images - cache devices' firmware
1417 *
1418 * If one device called request_firmware or its nowait version
1419 * successfully before, the firmware names are recored into the
1420 * device's devres link list, so device_cache_fw_images can call
1421 * cache_firmware() to cache these firmwares for the device,
1422 * then the device driver can load its firmwares easily at
1423 * time when system is not ready to complete loading firmware.
1424 */
1425static void device_cache_fw_images(void)
1426{
1427 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001428 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001429 DEFINE_WAIT(wait);
1430
1431 pr_debug("%s\n", __func__);
1432
Ming Lei373304f2012-10-09 12:01:01 +08001433 /* cancel uncache work */
1434 cancel_delayed_work_sync(&fwc->work);
1435
Ming Leiffe53f62012-08-04 12:01:28 +08001436 /*
1437 * use small loading timeout for caching devices' firmware
1438 * because all these firmware images have been loaded
1439 * successfully at lease once, also system is ready for
1440 * completing firmware loading now. The maximum size of
1441 * firmware in current distributions is about 2M bytes,
1442 * so 10 secs should be enough.
1443 */
1444 old_timeout = loading_timeout;
1445 loading_timeout = 10;
1446
Ming Leiac39b3e2012-08-20 19:04:16 +08001447 mutex_lock(&fw_lock);
1448 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001449 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001450 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001451
1452 /* wait for completion of caching firmware for all devices */
Ming Leid28d3882012-10-09 12:01:04 +08001453 async_synchronize_full_domain(&fw_cache_domain);
Ming Leiffe53f62012-08-04 12:01:28 +08001454
1455 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001456}
1457
1458/**
1459 * device_uncache_fw_images - uncache devices' firmware
1460 *
1461 * uncache all firmwares which have been cached successfully
1462 * by device_uncache_fw_images earlier
1463 */
1464static void device_uncache_fw_images(void)
1465{
1466 pr_debug("%s\n", __func__);
1467 __device_uncache_fw_images();
1468}
1469
1470static void device_uncache_fw_images_work(struct work_struct *work)
1471{
1472 device_uncache_fw_images();
1473}
1474
1475/**
1476 * device_uncache_fw_images_delay - uncache devices firmwares
1477 * @delay: number of milliseconds to delay uncache device firmwares
1478 *
1479 * uncache all devices's firmwares which has been cached successfully
1480 * by device_cache_fw_images after @delay milliseconds.
1481 */
1482static void device_uncache_fw_images_delay(unsigned long delay)
1483{
1484 schedule_delayed_work(&fw_cache.work,
1485 msecs_to_jiffies(delay));
1486}
1487
Ming Lei07646d92012-08-04 12:01:29 +08001488static int fw_pm_notify(struct notifier_block *notify_block,
1489 unsigned long mode, void *unused)
1490{
1491 switch (mode) {
1492 case PM_HIBERNATION_PREPARE:
1493 case PM_SUSPEND_PREPARE:
1494 device_cache_fw_images();
1495 break;
1496
1497 case PM_POST_SUSPEND:
1498 case PM_POST_HIBERNATION:
1499 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001500 /*
1501 * In case that system sleep failed and syscore_suspend is
1502 * not called.
1503 */
1504 mutex_lock(&fw_lock);
1505 fw_cache.state = FW_LOADER_NO_CACHE;
1506 mutex_unlock(&fw_lock);
1507
Ming Lei07646d92012-08-04 12:01:29 +08001508 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1509 break;
1510 }
1511
1512 return 0;
1513}
Ming Lei07646d92012-08-04 12:01:29 +08001514
Ming Leiac39b3e2012-08-20 19:04:16 +08001515/* stop caching firmware once syscore_suspend is reached */
1516static int fw_suspend(void)
1517{
1518 fw_cache.state = FW_LOADER_NO_CACHE;
1519 return 0;
1520}
1521
1522static struct syscore_ops fw_syscore_ops = {
1523 .suspend = fw_suspend,
1524};
Ming Leicfe016b2012-09-08 17:32:30 +08001525#else
1526static int fw_cache_piggyback_on_request(const char *name)
1527{
1528 return 0;
1529}
1530#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001531
Ming Lei37276a52012-08-04 12:01:27 +08001532static void __init fw_cache_init(void)
1533{
1534 spin_lock_init(&fw_cache.lock);
1535 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001536 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001537
Ming Leicfe016b2012-09-08 17:32:30 +08001538#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +08001539 spin_lock_init(&fw_cache.name_lock);
1540 INIT_LIST_HEAD(&fw_cache.fw_names);
Ming Lei37276a52012-08-04 12:01:27 +08001541
Ming Lei37276a52012-08-04 12:01:27 +08001542 INIT_DELAYED_WORK(&fw_cache.work,
1543 device_uncache_fw_images_work);
Ming Lei07646d92012-08-04 12:01:29 +08001544
1545 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1546 register_pm_notifier(&fw_cache.pm_notify);
Ming Leiac39b3e2012-08-20 19:04:16 +08001547
1548 register_syscore_ops(&fw_syscore_ops);
Ming Leicfe016b2012-09-08 17:32:30 +08001549#endif
Ming Lei37276a52012-08-04 12:01:27 +08001550}
1551
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001552static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
Ming Lei1f2b7952012-08-04 12:01:21 +08001554 fw_cache_init();
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001555#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaife304142013-05-22 18:28:38 +02001556 register_reboot_notifier(&fw_shutdown_nb);
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001557 return class_register(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001558#else
1559 return 0;
1560#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001562
1563static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
Ming Leicfe016b2012-09-08 17:32:30 +08001565#ifdef CONFIG_PM_SLEEP
Ming Leiac39b3e2012-08-20 19:04:16 +08001566 unregister_syscore_ops(&fw_syscore_ops);
Ming Lei07646d92012-08-04 12:01:29 +08001567 unregister_pm_notifier(&fw_cache.pm_notify);
Ming Leicfe016b2012-09-08 17:32:30 +08001568#endif
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001569#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaife304142013-05-22 18:28:38 +02001570 unregister_reboot_notifier(&fw_shutdown_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 class_unregister(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001572#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573}
1574
Shaohua Lia30a6a22006-09-27 01:50:52 -07001575fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576module_exit(firmware_class_exit);