blob: e4107d5f036b408a3c4d71b34838cbd1dd424540 [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
221static void __fw_free_buf(struct kref *ref)
222{
223 struct firmware_buf *buf = to_fwbuf(ref);
224 struct firmware_cache *fwc = buf->fwc;
Ming Lei1f2b7952012-08-04 12:01:21 +0800225
226 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
227 __func__, buf->fw_id, buf, buf->data,
228 (unsigned int)buf->size);
229
Ming Lei1f2b7952012-08-04 12:01:21 +0800230 list_del(&buf->list);
231 spin_unlock(&fwc->lock);
232
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100233#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100234 if (buf->is_paged_buf) {
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100235 int i;
Ming Lei746058f2012-10-09 12:01:03 +0800236 vunmap(buf->data);
237 for (i = 0; i < buf->nr_pages; i++)
238 __free_page(buf->pages[i]);
239 kfree(buf->pages);
240 } else
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100241#endif
Ming Lei746058f2012-10-09 12:01:03 +0800242 vfree(buf->data);
Ming Lei1f2b7952012-08-04 12:01:21 +0800243 kfree(buf);
244}
245
246static void fw_free_buf(struct firmware_buf *buf)
247{
Chuansheng Liubd9eb7f2012-11-10 01:27:22 +0800248 struct firmware_cache *fwc = buf->fwc;
249 spin_lock(&fwc->lock);
250 if (!kref_put(&buf->ref, __fw_free_buf))
251 spin_unlock(&fwc->lock);
Ming Lei1f2b7952012-08-04 12:01:21 +0800252}
253
Ming Lei746058f2012-10-09 12:01:03 +0800254/* direct firmware loading support */
Ming Lei27602842012-11-03 17:47:58 +0800255static char fw_path_para[256];
256static const char * const fw_path[] = {
257 fw_path_para,
Ming Lei746058f2012-10-09 12:01:03 +0800258 "/lib/firmware/updates/" UTS_RELEASE,
259 "/lib/firmware/updates",
260 "/lib/firmware/" UTS_RELEASE,
261 "/lib/firmware"
262};
263
Ming Lei27602842012-11-03 17:47:58 +0800264/*
265 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
266 * from kernel command line because firmware_class is generally built in
267 * kernel instead of module.
268 */
269module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
270MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
271
Ming Lei746058f2012-10-09 12:01:03 +0800272/* Don't inline this: 'struct kstat' is biggish */
Cesar Eduardo Barros60dac5e2012-10-27 20:37:10 -0200273static noinline_for_stack long fw_file_size(struct file *file)
Ming Lei746058f2012-10-09 12:01:03 +0800274{
275 struct kstat st;
Al Viro3dadecc2013-01-24 02:18:08 -0500276 if (vfs_getattr(&file->f_path, &st))
Ming Lei746058f2012-10-09 12:01:03 +0800277 return -1;
278 if (!S_ISREG(st.mode))
279 return -1;
280 if (st.size != (long)st.size)
281 return -1;
282 return st.size;
283}
284
285static bool fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf)
286{
287 long size;
288 char *buf;
289
290 size = fw_file_size(file);
Luciano Coelho4adf07f2013-01-15 10:43:43 +0200291 if (size <= 0)
Ming Lei746058f2012-10-09 12:01:03 +0800292 return false;
293 buf = vmalloc(size);
294 if (!buf)
295 return false;
296 if (kernel_read(file, 0, buf, size) != size) {
297 vfree(buf);
298 return false;
299 }
300 fw_buf->data = buf;
301 fw_buf->size = size;
302 return true;
303}
304
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100305static bool fw_get_filesystem_firmware(struct device *device,
306 struct firmware_buf *buf)
Ming Lei746058f2012-10-09 12:01:03 +0800307{
308 int i;
309 bool success = false;
310 char *path = __getname();
311
312 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
313 struct file *file;
Ming Lei27602842012-11-03 17:47:58 +0800314
315 /* skip the unset customized path */
316 if (!fw_path[i][0])
317 continue;
318
Ming Lei746058f2012-10-09 12:01:03 +0800319 snprintf(path, PATH_MAX, "%s/%s", fw_path[i], buf->fw_id);
320
321 file = filp_open(path, O_RDONLY, 0);
322 if (IS_ERR(file))
323 continue;
324 success = fw_read_file_contents(file, buf);
325 fput(file);
326 if (success)
327 break;
328 }
329 __putname(path);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100330
331 if (success) {
332 dev_dbg(device, "firmware: direct-loading firmware %s\n",
333 buf->fw_id);
334 mutex_lock(&fw_lock);
335 set_bit(FW_STATUS_DONE, &buf->status);
336 complete_all(&buf->completion);
337 mutex_unlock(&fw_lock);
338 }
339
Ming Lei746058f2012-10-09 12:01:03 +0800340 return success;
341}
342
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100343/* firmware holds the ownership of pages */
344static void firmware_free_data(const struct firmware *fw)
345{
346 /* Loaded directly? */
347 if (!fw->priv) {
348 vfree(fw->data);
349 return;
350 }
351 fw_free_buf(fw->priv);
352}
353
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100354/* store the pages buffer info firmware from buf */
355static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
356{
357 fw->priv = buf;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100358#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100359 fw->pages = buf->pages;
360#endif
361 fw->size = buf->size;
362 fw->data = buf->data;
363
364 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
365 __func__, buf->fw_id, buf, buf->data,
366 (unsigned int)buf->size);
367}
368
369#ifdef CONFIG_PM_SLEEP
370static void fw_name_devm_release(struct device *dev, void *res)
371{
372 struct fw_name_devm *fwn = res;
373
374 if (fwn->magic == (unsigned long)&fw_cache)
375 pr_debug("%s: fw_name-%s devm-%p released\n",
376 __func__, fwn->name, res);
377}
378
379static int fw_devm_match(struct device *dev, void *res,
380 void *match_data)
381{
382 struct fw_name_devm *fwn = res;
383
384 return (fwn->magic == (unsigned long)&fw_cache) &&
385 !strcmp(fwn->name, match_data);
386}
387
388static struct fw_name_devm *fw_find_devm_name(struct device *dev,
389 const char *name)
390{
391 struct fw_name_devm *fwn;
392
393 fwn = devres_find(dev, fw_name_devm_release,
394 fw_devm_match, (void *)name);
395 return fwn;
396}
397
398/* add firmware name into devres list */
399static int fw_add_devm_name(struct device *dev, const char *name)
400{
401 struct fw_name_devm *fwn;
402
403 fwn = fw_find_devm_name(dev, name);
404 if (fwn)
405 return 1;
406
407 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
408 strlen(name) + 1, GFP_KERNEL);
409 if (!fwn)
410 return -ENOMEM;
411
412 fwn->magic = (unsigned long)&fw_cache;
413 strcpy(fwn->name, name);
414 devres_add(dev, fwn);
415
416 return 0;
417}
418#else
419static int fw_add_devm_name(struct device *dev, const char *name)
420{
421 return 0;
422}
423#endif
424
425
426/*
427 * user-mode helper code
428 */
429#ifdef CONFIG_FW_LOADER_USER_HELPER
430struct firmware_priv {
431 struct delayed_work timeout_work;
432 bool nowait;
433 struct device dev;
434 struct firmware_buf *buf;
435 struct firmware *fw;
436};
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100437
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700438static struct firmware_priv *to_firmware_priv(struct device *dev)
439{
440 return container_of(dev, struct firmware_priv, dev);
441}
442
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700443static void __fw_load_abort(struct firmware_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Ming Lei87597932013-06-15 16:36:38 +0800445 /*
446 * There is a small window in which user can write to 'loading'
447 * between loading done and disappearance of 'loading'
448 */
449 if (test_bit(FW_STATUS_DONE, &buf->status))
450 return;
451
Takashi Iwaife304142013-05-22 18:28:38 +0200452 list_del_init(&buf->pending_list);
Ming Lei12446912012-08-04 12:01:20 +0800453 set_bit(FW_STATUS_ABORT, &buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800454 complete_all(&buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700457static void fw_load_abort(struct firmware_priv *fw_priv)
458{
459 struct firmware_buf *buf = fw_priv->buf;
460
461 __fw_load_abort(buf);
Ming Lei87597932013-06-15 16:36:38 +0800462
463 /* avoid user action after loading abort */
464 fw_priv->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Takashi Iwai807be032013-01-31 11:13:57 +0100467#define is_fw_load_aborted(buf) \
468 test_bit(FW_STATUS_ABORT, &(buf)->status)
469
Takashi Iwaife304142013-05-22 18:28:38 +0200470static LIST_HEAD(pending_fw_head);
471
472/* reboot notifier for avoid deadlock with usermode_lock */
473static int fw_shutdown_notify(struct notifier_block *unused1,
474 unsigned long unused2, void *unused3)
475{
476 mutex_lock(&fw_lock);
477 while (!list_empty(&pending_fw_head))
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700478 __fw_load_abort(list_first_entry(&pending_fw_head,
Takashi Iwaife304142013-05-22 18:28:38 +0200479 struct firmware_buf,
480 pending_list));
481 mutex_unlock(&fw_lock);
482 return NOTIFY_DONE;
483}
484
485static struct notifier_block fw_shutdown_nb = {
486 .notifier_call = fw_shutdown_notify,
487};
488
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700489static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
490 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 return sprintf(buf, "%d\n", loading_timeout);
493}
494
495/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800496 * firmware_timeout_store - set number of seconds to wait for firmware
497 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800498 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800499 * @buf: buffer to scan for timeout value
500 * @count: number of bytes in @buf
501 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800503 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 * firmware will be provided.
505 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800506 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 **/
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700508static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
509 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700512 if (loading_timeout < 0)
513 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return count;
516}
517
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800518static struct class_attribute firmware_class_attrs[] = {
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700519 __ATTR_RW(timeout),
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800520 __ATTR_NULL
521};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800523static void fw_dev_release(struct device *dev)
524{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700525 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800526
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800527 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800528}
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 Lei87597932013-06-15 16:36:38 +0800555 int loading = 0;
556
557 mutex_lock(&fw_lock);
558 if (fw_priv->buf)
559 loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
560 mutex_unlock(&fw_lock);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return sprintf(buf, "%d\n", loading);
563}
564
David Woodhouse6e03a202009-04-09 22:04:07 -0700565/* Some architectures don't have PAGE_KERNEL_RO */
566#ifndef PAGE_KERNEL_RO
567#define PAGE_KERNEL_RO PAGE_KERNEL
568#endif
Ming Lei253c9242012-10-09 12:01:02 +0800569
570/* one pages buffer should be mapped/unmapped only once */
571static int fw_map_pages_buf(struct firmware_buf *buf)
572{
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100573 if (!buf->is_paged_buf)
Ming Lei746058f2012-10-09 12:01:03 +0800574 return 0;
575
Ming Lei253c9242012-10-09 12:01:02 +0800576 if (buf->data)
577 vunmap(buf->data);
578 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
579 if (!buf->data)
580 return -ENOMEM;
581 return 0;
582}
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800585 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700586 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800587 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800588 * @buf: buffer to scan for loading control value
589 * @count: number of bytes in @buf
590 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 * The relevant values are:
592 *
593 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800594 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 * -1: Conclude the load with an error and discard any written data.
596 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700597static ssize_t firmware_loading_store(struct device *dev,
598 struct device_attribute *attr,
599 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700601 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800602 struct firmware_buf *fw_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700604 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Neil Hormaneea915b2012-01-02 15:31:23 -0500606 mutex_lock(&fw_lock);
Ming Lei87597932013-06-15 16:36:38 +0800607 fw_buf = fw_priv->buf;
Ming Lei12446912012-08-04 12:01:20 +0800608 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500609 goto out;
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 switch (loading) {
612 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800613 /* discarding any previous partial load */
Ming Lei12446912012-08-04 12:01:20 +0800614 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
615 for (i = 0; i < fw_buf->nr_pages; i++)
616 __free_page(fw_buf->pages[i]);
617 kfree(fw_buf->pages);
618 fw_buf->pages = NULL;
619 fw_buf->page_array_size = 0;
620 fw_buf->nr_pages = 0;
621 set_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei28eefa72012-08-04 12:01:17 +0800622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 break;
624 case 0:
Ming Lei12446912012-08-04 12:01:20 +0800625 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
626 set_bit(FW_STATUS_DONE, &fw_buf->status);
627 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei253c9242012-10-09 12:01:02 +0800628
629 /*
630 * Several loading requests may be pending on
631 * one same firmware buf, so let all requests
632 * see the mapped 'buf->data' once the loading
633 * is completed.
634 * */
635 fw_map_pages_buf(fw_buf);
Takashi Iwaife304142013-05-22 18:28:38 +0200636 list_del_init(&fw_buf->pending_list);
Ming Lei1f2b7952012-08-04 12:01:21 +0800637 complete_all(&fw_buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 break;
639 }
640 /* fallthrough */
641 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700642 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 /* fallthrough */
644 case -1:
645 fw_load_abort(fw_priv);
646 break;
647 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500648out:
649 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return count;
651}
652
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700653static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700655static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
656 struct bin_attribute *bin_attr,
657 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200659 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700660 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800661 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700662 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Laura Garciacad1e552006-05-23 23:22:38 +0200664 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800665 buf = fw_priv->buf;
666 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 ret_count = -ENODEV;
668 goto out;
669 }
Ming Lei12446912012-08-04 12:01:20 +0800670 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200671 ret_count = 0;
672 goto out;
673 }
Ming Lei12446912012-08-04 12:01:20 +0800674 if (count > buf->size - offset)
675 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700676
677 ret_count = count;
678
679 while (count) {
680 void *page_data;
681 int page_nr = offset >> PAGE_SHIFT;
682 int page_ofs = offset & (PAGE_SIZE-1);
683 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
684
Ming Lei12446912012-08-04 12:01:20 +0800685 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700686
687 memcpy(buffer, page_data + page_ofs, page_cnt);
688
Ming Lei12446912012-08-04 12:01:20 +0800689 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700690 buffer += page_cnt;
691 offset += page_cnt;
692 count -= page_cnt;
693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694out:
Laura Garciacad1e552006-05-23 23:22:38 +0200695 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return ret_count;
697}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800698
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700699static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Ming Lei12446912012-08-04 12:01:20 +0800701 struct firmware_buf *buf = fw_priv->buf;
David Woodhouse6e03a202009-04-09 22:04:07 -0700702 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
David Woodhouse6e03a202009-04-09 22:04:07 -0700704 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800705 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700706 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800707 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700708 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
David Woodhouse6e03a202009-04-09 22:04:07 -0700710 new_pages = kmalloc(new_array_size * sizeof(void *),
711 GFP_KERNEL);
712 if (!new_pages) {
713 fw_load_abort(fw_priv);
714 return -ENOMEM;
715 }
Ming Lei12446912012-08-04 12:01:20 +0800716 memcpy(new_pages, buf->pages,
717 buf->page_array_size * sizeof(void *));
718 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
719 (new_array_size - buf->page_array_size));
720 kfree(buf->pages);
721 buf->pages = new_pages;
722 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700724
Ming Lei12446912012-08-04 12:01:20 +0800725 while (buf->nr_pages < pages_needed) {
726 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700727 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
728
Ming Lei12446912012-08-04 12:01:20 +0800729 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700730 fw_load_abort(fw_priv);
731 return -ENOMEM;
732 }
Ming Lei12446912012-08-04 12:01:20 +0800733 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return 0;
736}
737
738/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800739 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700740 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700741 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700742 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800743 * @buffer: buffer being written
744 * @offset: buffer offset for write in total data store area
745 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800747 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 * the driver as a firmware image.
749 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700750static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
751 struct bin_attribute *bin_attr,
752 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200754 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700755 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800756 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 ssize_t retval;
758
759 if (!capable(CAP_SYS_RAWIO))
760 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700761
Laura Garciacad1e552006-05-23 23:22:38 +0200762 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800763 buf = fw_priv->buf;
764 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 retval = -ENODEV;
766 goto out;
767 }
Ming Lei65710cb2012-08-04 12:01:16 +0800768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 retval = fw_realloc_buffer(fw_priv, offset + count);
770 if (retval)
771 goto out;
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 retval = count;
David Woodhouse6e03a202009-04-09 22:04:07 -0700774
775 while (count) {
776 void *page_data;
777 int page_nr = offset >> PAGE_SHIFT;
778 int page_ofs = offset & (PAGE_SIZE - 1);
779 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
780
Ming Lei12446912012-08-04 12:01:20 +0800781 page_data = kmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700782
783 memcpy(page_data + page_ofs, buffer, page_cnt);
784
Ming Lei12446912012-08-04 12:01:20 +0800785 kunmap(buf->pages[page_nr]);
David Woodhouse6e03a202009-04-09 22:04:07 -0700786 buffer += page_cnt;
787 offset += page_cnt;
788 count -= page_cnt;
789 }
790
Ming Lei12446912012-08-04 12:01:20 +0800791 buf->size = max_t(size_t, offset, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792out:
Laura Garciacad1e552006-05-23 23:22:38 +0200793 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return retval;
795}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800796
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700797static struct bin_attribute firmware_attr_data = {
798 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 .size = 0,
800 .read = firmware_data_read,
801 .write = firmware_data_write,
802};
803
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800804static void firmware_class_timeout_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800806 struct firmware_priv *fw_priv = container_of(work,
807 struct firmware_priv, timeout_work.work);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700808
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800809 mutex_lock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 fw_load_abort(fw_priv);
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800811 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700814static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200815fw_create_instance(struct firmware *firmware, const char *fw_name,
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700816 struct device *device, bool uevent, bool nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700818 struct firmware_priv *fw_priv;
819 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Ming Lei12446912012-08-04 12:01:20 +0800821 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700822 if (!fw_priv) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700823 dev_err(device, "%s: kmalloc failed\n", __func__);
Ming Lei12446912012-08-04 12:01:20 +0800824 fw_priv = ERR_PTR(-ENOMEM);
825 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700828 fw_priv->nowait = nowait;
Ming Lei1f2b7952012-08-04 12:01:21 +0800829 fw_priv->fw = firmware;
Chuansheng Liuce2fcbd2012-11-08 19:14:40 +0800830 INIT_DELAYED_WORK(&fw_priv->timeout_work,
831 firmware_class_timeout_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700833 f_dev = &fw_priv->dev;
834
835 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800836 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700837 f_dev->parent = device;
838 f_dev->class = &firmware_class;
Ming Lei12446912012-08-04 12:01:20 +0800839exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700840 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100842
843/* load a firmware via user helper */
844static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
845 long timeout)
846{
847 int retval = 0;
848 struct device *f_dev = &fw_priv->dev;
849 struct firmware_buf *buf = fw_priv->buf;
850
851 /* fall back on userspace loading */
852 buf->is_paged_buf = true;
853
854 dev_set_uevent_suppress(f_dev, true);
855
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100856 retval = device_add(f_dev);
857 if (retval) {
858 dev_err(f_dev, "%s: device_register failed\n", __func__);
859 goto err_put_dev;
860 }
861
862 retval = device_create_bin_file(f_dev, &firmware_attr_data);
863 if (retval) {
864 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
865 goto err_del_dev;
866 }
867
868 retval = device_create_file(f_dev, &dev_attr_loading);
869 if (retval) {
870 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
871 goto err_del_bin_attr;
872 }
873
874 if (uevent) {
Ming Leiaf5bc112013-05-27 18:30:04 +0800875 buf->need_uevent = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100876 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
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100892 device_remove_file(f_dev, &dev_attr_loading);
893err_del_bin_attr:
894 device_remove_bin_file(f_dev, &firmware_attr_data);
895err_del_dev:
896 device_del(f_dev);
897err_put_dev:
898 put_device(f_dev);
899 return retval;
900}
901
902static int fw_load_from_user_helper(struct firmware *firmware,
903 const char *name, struct device *device,
904 bool uevent, bool nowait, long timeout)
905{
906 struct firmware_priv *fw_priv;
907
908 fw_priv = fw_create_instance(firmware, name, device, uevent, nowait);
909 if (IS_ERR(fw_priv))
910 return PTR_ERR(fw_priv);
911
912 fw_priv->buf = firmware->priv;
913 return _request_firmware_load(fw_priv, uevent, timeout);
914}
Ming Leiddf1f062013-06-04 10:01:13 +0800915
Ming Lei5b7cb7a2013-06-06 19:52:54 +0800916#ifdef CONFIG_PM_SLEEP
Ming Leiddf1f062013-06-04 10:01:13 +0800917/* kill pending requests without uevent to avoid blocking suspend */
918static void kill_requests_without_uevent(void)
919{
920 struct firmware_buf *buf;
921 struct firmware_buf *next;
922
923 mutex_lock(&fw_lock);
924 list_for_each_entry_safe(buf, next, &pending_fw_head, pending_list) {
925 if (!buf->need_uevent)
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700926 __fw_load_abort(buf);
Ming Leiddf1f062013-06-04 10:01:13 +0800927 }
928 mutex_unlock(&fw_lock);
929}
Ming Lei5b7cb7a2013-06-06 19:52:54 +0800930#endif
Ming Leiddf1f062013-06-04 10:01:13 +0800931
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100932#else /* CONFIG_FW_LOADER_USER_HELPER */
933static inline int
934fw_load_from_user_helper(struct firmware *firmware, const char *name,
935 struct device *device, bool uevent, bool nowait,
936 long timeout)
937{
938 return -ENOENT;
939}
Takashi Iwai807be032013-01-31 11:13:57 +0100940
941/* No abort during direct loading */
942#define is_fw_load_aborted(buf) false
943
Ming Lei5b7cb7a2013-06-06 19:52:54 +0800944#ifdef CONFIG_PM_SLEEP
Ming Leiddf1f062013-06-04 10:01:13 +0800945static inline void kill_requests_without_uevent(void) { }
Ming Lei5b7cb7a2013-06-06 19:52:54 +0800946#endif
Ming Leiddf1f062013-06-04 10:01:13 +0800947
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100948#endif /* CONFIG_FW_LOADER_USER_HELPER */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Ming Leif531f05a2012-08-04 12:01:25 +0800950
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100951/* wait until the shared firmware_buf becomes ready (or error) */
952static int sync_cached_firmware_buf(struct firmware_buf *buf)
Ming Lei1f2b7952012-08-04 12:01:21 +0800953{
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100954 int ret = 0;
955
956 mutex_lock(&fw_lock);
957 while (!test_bit(FW_STATUS_DONE, &buf->status)) {
Takashi Iwai807be032013-01-31 11:13:57 +0100958 if (is_fw_load_aborted(buf)) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100959 ret = -ENOENT;
960 break;
961 }
962 mutex_unlock(&fw_lock);
963 wait_for_completion(&buf->completion);
964 mutex_lock(&fw_lock);
965 }
966 mutex_unlock(&fw_lock);
967 return ret;
Ming Lei1f2b7952012-08-04 12:01:21 +0800968}
969
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100970/* prepare firmware and firmware_buf structs;
971 * return 0 if a firmware is already assigned, 1 if need to load one,
972 * or a negative error code
973 */
974static int
975_request_firmware_prepare(struct firmware **firmware_p, const char *name,
976 struct device *device)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700977{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +0800979 struct firmware_buf *buf;
980 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Jiri Slaby4aed0642005-09-13 01:25:01 -0700982 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700984 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
985 __func__);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100986 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -0800989 if (fw_get_builtin_firmware(firmware, name)) {
Rafael J. Wysocki6f18ff92010-02-27 21:43:22 +0100990 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100991 return 0; /* assigned */
David Woodhouse5658c762008-05-23 13:52:42 +0100992 }
993
Ming Lei1f2b7952012-08-04 12:01:21 +0800994 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
Ming Lei1f2b7952012-08-04 12:01:21 +0800995
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100996 /*
997 * bind with 'buf' now to avoid warning in failure path
998 * of requesting firmware.
999 */
1000 firmware->priv = buf;
Ming Lei1f2b7952012-08-04 12:01:21 +08001001
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001002 if (ret > 0) {
1003 ret = sync_cached_firmware_buf(buf);
1004 if (!ret) {
1005 fw_set_page_data(buf, firmware);
1006 return 0; /* assigned */
1007 }
Stephen Boyddddb5542012-03-28 23:30:43 +02001008 }
Ming Lei1f2b7952012-08-04 12:01:21 +08001009
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001010 if (ret < 0)
1011 return ret;
1012 return 1; /* need to load */
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001013}
1014
Ming Leie771d1a2013-05-27 18:30:03 +08001015static int assign_firmware_buf(struct firmware *fw, struct device *device,
1016 bool skip_cache)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001017{
1018 struct firmware_buf *buf = fw->priv;
1019
1020 mutex_lock(&fw_lock);
Takashi Iwai807be032013-01-31 11:13:57 +01001021 if (!buf->size || is_fw_load_aborted(buf)) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001022 mutex_unlock(&fw_lock);
1023 return -ENOENT;
1024 }
1025
1026 /*
1027 * add firmware name into devres list so that we can auto cache
1028 * and uncache firmware for device.
1029 *
1030 * device may has been deleted already, but the problem
1031 * should be fixed in devres or driver core.
1032 */
Ming Leie771d1a2013-05-27 18:30:03 +08001033 if (device && !skip_cache)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001034 fw_add_devm_name(device, buf->fw_id);
1035
1036 /*
1037 * After caching firmware image is started, let it piggyback
1038 * on request firmware.
1039 */
1040 if (buf->fwc->state == FW_LOADER_START_CACHE) {
1041 if (fw_cache_piggyback_on_request(buf->fw_id))
1042 kref_get(&buf->ref);
1043 }
1044
1045 /* pass the pages buffer to driver at the last minute */
1046 fw_set_page_data(buf, fw);
1047 mutex_unlock(&fw_lock);
1048 return 0;
1049}
1050
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001051/* called from request_firmware() and request_firmware_work_func() */
1052static int
1053_request_firmware(const struct firmware **firmware_p, const char *name,
1054 struct device *device, bool uevent, bool nowait)
1055{
1056 struct firmware *fw;
1057 long timeout;
1058 int ret;
1059
1060 if (!firmware_p)
1061 return -EINVAL;
1062
1063 ret = _request_firmware_prepare(&fw, name, device);
1064 if (ret <= 0) /* error or already assigned */
1065 goto out;
1066
1067 ret = 0;
1068 timeout = firmware_loading_timeout();
1069 if (nowait) {
1070 timeout = usermodehelper_read_lock_wait(timeout);
1071 if (!timeout) {
1072 dev_dbg(device, "firmware: %s loading timed out\n",
1073 name);
1074 ret = -EBUSY;
1075 goto out;
1076 }
1077 } else {
1078 ret = usermodehelper_read_trylock();
1079 if (WARN_ON(ret)) {
1080 dev_err(device, "firmware: %s will not be loaded\n",
1081 name);
1082 goto out;
1083 }
1084 }
1085
1086 if (!fw_get_filesystem_firmware(device, fw->priv))
1087 ret = fw_load_from_user_helper(fw, name, device,
1088 uevent, nowait, timeout);
Ming Leie771d1a2013-05-27 18:30:03 +08001089
1090 /* don't cache firmware handled without uevent */
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001091 if (!ret)
Ming Leie771d1a2013-05-27 18:30:03 +08001092 ret = assign_firmware_buf(fw, device, !uevent);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001093
1094 usermodehelper_read_unlock();
1095
1096 out:
1097 if (ret < 0) {
1098 release_firmware(fw);
1099 fw = NULL;
1100 }
1101
1102 *firmware_p = fw;
1103 return ret;
1104}
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106/**
Kay Sievers312c0042005-11-16 09:00:00 +01001107 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001108 * @firmware_p: pointer to firmware image
1109 * @name: name of firmware file
1110 * @device: device for which firmware is being loaded
1111 *
1112 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001113 * of @name for device @device.
1114 *
1115 * Should be called from user context where sleeping is allowed.
1116 *
Kay Sievers312c0042005-11-16 09:00:00 +01001117 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001118 * should be distinctive enough not to be confused with any other
1119 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +08001120 *
1121 * Caller must hold the reference count of @device.
Ming Lei6a927852012-11-03 17:48:16 +08001122 *
1123 * The function can be called safely inside device's suspend and
1124 * resume callback.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001125 **/
1126int
1127request_firmware(const struct firmware **firmware_p, const char *name,
1128 struct device *device)
1129{
Ming Leid6c8aa32013-06-06 20:01:48 +08001130 int ret;
1131
1132 /* Need to pin this module until return */
1133 __module_get(THIS_MODULE);
1134 ret = _request_firmware(firmware_p, name, device, true, false);
1135 module_put(THIS_MODULE);
1136 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001137}
Daniel Mackf4945132013-05-23 22:17:18 +02001138EXPORT_SYMBOL(request_firmware);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001139
1140/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001142 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001144void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
1146 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001147 if (!fw_is_builtin_firmware(fw))
1148 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 kfree(fw);
1150 }
1151}
Daniel Mackf4945132013-05-23 22:17:18 +02001152EXPORT_SYMBOL(release_firmware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154/* Async support */
1155struct firmware_work {
1156 struct work_struct work;
1157 struct module *module;
1158 const char *name;
1159 struct device *device;
1160 void *context;
1161 void (*cont)(const struct firmware *fw, void *context);
Bob Liu072fc8f2011-01-26 18:33:32 +08001162 bool uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163};
1164
Stephen Boyda36cf842012-03-28 23:31:00 +02001165static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
Stephen Boyda36cf842012-03-28 23:31:00 +02001167 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 const struct firmware *fw;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001169
Stephen Boyda36cf842012-03-28 23:31:00 +02001170 fw_work = container_of(work, struct firmware_work, work);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001171
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001172 _request_firmware(&fw, fw_work->name, fw_work->device,
1173 fw_work->uevent, true);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001174 fw_work->cont(fw, fw_work->context);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001175 put_device(fw_work->device); /* taken in request_firmware_nowait() */
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 module_put(fw_work->module);
1178 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
1181/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001182 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001183 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001184 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001185 * is non-zero else the firmware copy must be done manually.
1186 * @name: name of firmware file
1187 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001188 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001189 * @context: will be passed over to @cont, and
1190 * @fw may be %NULL if firmware request fails.
1191 * @cont: function will be called asynchronously when the firmware
1192 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001194 * Caller must hold the reference count of @device.
1195 *
Ming Lei6f21a622012-08-04 12:01:24 +08001196 * Asynchronous variant of request_firmware() for user contexts:
1197 * - sleep for as small periods as possible since it may
1198 * increase kernel boot time of built-in device drivers
1199 * requesting firmware in their ->probe() methods, if
1200 * @gfp is GFP_KERNEL.
1201 *
1202 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 **/
1204int
1205request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001206 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001207 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 void (*cont)(const struct firmware *fw, void *context))
1209{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001210 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001212 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 if (!fw_work)
1214 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001215
1216 fw_work->module = module;
1217 fw_work->name = name;
1218 fw_work->device = device;
1219 fw_work->context = context;
1220 fw_work->cont = cont;
1221 fw_work->uevent = uevent;
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (!try_module_get(module)) {
1224 kfree(fw_work);
1225 return -EFAULT;
1226 }
1227
Ming Lei0cfc1e12012-08-04 12:01:23 +08001228 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001229 INIT_WORK(&fw_work->work, request_firmware_work_func);
1230 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return 0;
1232}
Daniel Mackf4945132013-05-23 22:17:18 +02001233EXPORT_SYMBOL(request_firmware_nowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Ming Lei90f890812013-06-20 12:30:16 +08001235#ifdef CONFIG_PM_SLEEP
1236static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1237
Ming Lei2887b392012-08-04 12:01:22 +08001238/**
1239 * cache_firmware - cache one firmware image in kernel memory space
1240 * @fw_name: the firmware image name
1241 *
1242 * Cache firmware in kernel memory so that drivers can use it when
1243 * system isn't ready for them to request firmware image from userspace.
1244 * Once it returns successfully, driver can use request_firmware or its
1245 * nowait version to get the cached firmware without any interacting
1246 * with userspace
1247 *
1248 * Return 0 if the firmware image has been cached successfully
1249 * Return !0 otherwise
1250 *
1251 */
Ming Lei93232e42013-06-06 20:01:47 +08001252static int cache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001253{
1254 int ret;
1255 const struct firmware *fw;
1256
1257 pr_debug("%s: %s\n", __func__, fw_name);
1258
1259 ret = request_firmware(&fw, fw_name, NULL);
1260 if (!ret)
1261 kfree(fw);
1262
1263 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1264
1265 return ret;
1266}
1267
Ming Lei6a2c1232013-06-26 09:28:17 +08001268static struct firmware_buf *fw_lookup_buf(const char *fw_name)
1269{
1270 struct firmware_buf *tmp;
1271 struct firmware_cache *fwc = &fw_cache;
1272
1273 spin_lock(&fwc->lock);
1274 tmp = __fw_lookup_buf(fw_name);
1275 spin_unlock(&fwc->lock);
1276
1277 return tmp;
1278}
1279
Ming Lei2887b392012-08-04 12:01:22 +08001280/**
1281 * uncache_firmware - remove one cached firmware image
1282 * @fw_name: the firmware image name
1283 *
1284 * Uncache one firmware image which has been cached successfully
1285 * before.
1286 *
1287 * Return 0 if the firmware cache has been removed successfully
1288 * Return !0 otherwise
1289 *
1290 */
Ming Lei93232e42013-06-06 20:01:47 +08001291static int uncache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001292{
1293 struct firmware_buf *buf;
1294 struct firmware fw;
1295
1296 pr_debug("%s: %s\n", __func__, fw_name);
1297
1298 if (fw_get_builtin_firmware(&fw, fw_name))
1299 return 0;
1300
1301 buf = fw_lookup_buf(fw_name);
1302 if (buf) {
1303 fw_free_buf(buf);
1304 return 0;
1305 }
1306
1307 return -EINVAL;
1308}
1309
Ming Lei37276a52012-08-04 12:01:27 +08001310static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1311{
1312 struct fw_cache_entry *fce;
1313
1314 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1315 if (!fce)
1316 goto exit;
1317
1318 strcpy(fce->name, name);
1319exit:
1320 return fce;
1321}
1322
Ming Lei373304f2012-10-09 12:01:01 +08001323static int __fw_entry_found(const char *name)
1324{
1325 struct firmware_cache *fwc = &fw_cache;
1326 struct fw_cache_entry *fce;
1327
1328 list_for_each_entry(fce, &fwc->fw_names, list) {
1329 if (!strcmp(fce->name, name))
1330 return 1;
1331 }
1332 return 0;
1333}
1334
Ming Leiac39b3e2012-08-20 19:04:16 +08001335static int fw_cache_piggyback_on_request(const char *name)
1336{
1337 struct firmware_cache *fwc = &fw_cache;
1338 struct fw_cache_entry *fce;
1339 int ret = 0;
1340
1341 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001342 if (__fw_entry_found(name))
1343 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001344
1345 fce = alloc_fw_cache_entry(name);
1346 if (fce) {
1347 ret = 1;
1348 list_add(&fce->list, &fwc->fw_names);
1349 pr_debug("%s: fw: %s\n", __func__, name);
1350 }
1351found:
1352 spin_unlock(&fwc->name_lock);
1353 return ret;
1354}
1355
Ming Lei37276a52012-08-04 12:01:27 +08001356static void free_fw_cache_entry(struct fw_cache_entry *fce)
1357{
1358 kfree(fce);
1359}
1360
1361static void __async_dev_cache_fw_image(void *fw_entry,
1362 async_cookie_t cookie)
1363{
1364 struct fw_cache_entry *fce = fw_entry;
1365 struct firmware_cache *fwc = &fw_cache;
1366 int ret;
1367
1368 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001369 if (ret) {
1370 spin_lock(&fwc->name_lock);
1371 list_del(&fce->list);
1372 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001373
Ming Leiac39b3e2012-08-20 19:04:16 +08001374 free_fw_cache_entry(fce);
1375 }
Ming Lei37276a52012-08-04 12:01:27 +08001376}
1377
1378/* called with dev->devres_lock held */
1379static void dev_create_fw_entry(struct device *dev, void *res,
1380 void *data)
1381{
1382 struct fw_name_devm *fwn = res;
1383 const char *fw_name = fwn->name;
1384 struct list_head *head = data;
1385 struct fw_cache_entry *fce;
1386
1387 fce = alloc_fw_cache_entry(fw_name);
1388 if (fce)
1389 list_add(&fce->list, head);
1390}
1391
1392static int devm_name_match(struct device *dev, void *res,
1393 void *match_data)
1394{
1395 struct fw_name_devm *fwn = res;
1396 return (fwn->magic == (unsigned long)match_data);
1397}
1398
Ming Leiab6dd8e2012-08-17 22:07:00 +08001399static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001400{
1401 LIST_HEAD(todo);
1402 struct fw_cache_entry *fce;
1403 struct fw_cache_entry *fce_next;
1404 struct firmware_cache *fwc = &fw_cache;
1405
1406 devres_for_each_res(dev, fw_name_devm_release,
1407 devm_name_match, &fw_cache,
1408 dev_create_fw_entry, &todo);
1409
1410 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1411 list_del(&fce->list);
1412
1413 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001414 /* only one cache entry for one firmware */
1415 if (!__fw_entry_found(fce->name)) {
Ming Lei373304f2012-10-09 12:01:01 +08001416 list_add(&fce->list, &fwc->fw_names);
1417 } else {
1418 free_fw_cache_entry(fce);
1419 fce = NULL;
1420 }
Ming Lei37276a52012-08-04 12:01:27 +08001421 spin_unlock(&fwc->name_lock);
1422
Ming Lei373304f2012-10-09 12:01:01 +08001423 if (fce)
Ming Leid28d3882012-10-09 12:01:04 +08001424 async_schedule_domain(__async_dev_cache_fw_image,
1425 (void *)fce,
1426 &fw_cache_domain);
Ming Lei37276a52012-08-04 12:01:27 +08001427 }
1428}
1429
1430static void __device_uncache_fw_images(void)
1431{
1432 struct firmware_cache *fwc = &fw_cache;
1433 struct fw_cache_entry *fce;
1434
1435 spin_lock(&fwc->name_lock);
1436 while (!list_empty(&fwc->fw_names)) {
1437 fce = list_entry(fwc->fw_names.next,
1438 struct fw_cache_entry, list);
1439 list_del(&fce->list);
1440 spin_unlock(&fwc->name_lock);
1441
1442 uncache_firmware(fce->name);
1443 free_fw_cache_entry(fce);
1444
1445 spin_lock(&fwc->name_lock);
1446 }
1447 spin_unlock(&fwc->name_lock);
1448}
1449
1450/**
1451 * device_cache_fw_images - cache devices' firmware
1452 *
1453 * If one device called request_firmware or its nowait version
1454 * successfully before, the firmware names are recored into the
1455 * device's devres link list, so device_cache_fw_images can call
1456 * cache_firmware() to cache these firmwares for the device,
1457 * then the device driver can load its firmwares easily at
1458 * time when system is not ready to complete loading firmware.
1459 */
1460static void device_cache_fw_images(void)
1461{
1462 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001463 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001464 DEFINE_WAIT(wait);
1465
1466 pr_debug("%s\n", __func__);
1467
Ming Lei373304f2012-10-09 12:01:01 +08001468 /* cancel uncache work */
1469 cancel_delayed_work_sync(&fwc->work);
1470
Ming Leiffe53f62012-08-04 12:01:28 +08001471 /*
1472 * use small loading timeout for caching devices' firmware
1473 * because all these firmware images have been loaded
1474 * successfully at lease once, also system is ready for
1475 * completing firmware loading now. The maximum size of
1476 * firmware in current distributions is about 2M bytes,
1477 * so 10 secs should be enough.
1478 */
1479 old_timeout = loading_timeout;
1480 loading_timeout = 10;
1481
Ming Leiac39b3e2012-08-20 19:04:16 +08001482 mutex_lock(&fw_lock);
1483 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001484 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001485 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001486
1487 /* wait for completion of caching firmware for all devices */
Ming Leid28d3882012-10-09 12:01:04 +08001488 async_synchronize_full_domain(&fw_cache_domain);
Ming Leiffe53f62012-08-04 12:01:28 +08001489
1490 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001491}
1492
1493/**
1494 * device_uncache_fw_images - uncache devices' firmware
1495 *
1496 * uncache all firmwares which have been cached successfully
1497 * by device_uncache_fw_images earlier
1498 */
1499static void device_uncache_fw_images(void)
1500{
1501 pr_debug("%s\n", __func__);
1502 __device_uncache_fw_images();
1503}
1504
1505static void device_uncache_fw_images_work(struct work_struct *work)
1506{
1507 device_uncache_fw_images();
1508}
1509
1510/**
1511 * device_uncache_fw_images_delay - uncache devices firmwares
1512 * @delay: number of milliseconds to delay uncache device firmwares
1513 *
1514 * uncache all devices's firmwares which has been cached successfully
1515 * by device_cache_fw_images after @delay milliseconds.
1516 */
1517static void device_uncache_fw_images_delay(unsigned long delay)
1518{
1519 schedule_delayed_work(&fw_cache.work,
1520 msecs_to_jiffies(delay));
1521}
1522
Ming Lei07646d92012-08-04 12:01:29 +08001523static int fw_pm_notify(struct notifier_block *notify_block,
1524 unsigned long mode, void *unused)
1525{
1526 switch (mode) {
1527 case PM_HIBERNATION_PREPARE:
1528 case PM_SUSPEND_PREPARE:
Ming Leiaf5bc112013-05-27 18:30:04 +08001529 kill_requests_without_uevent();
Ming Lei07646d92012-08-04 12:01:29 +08001530 device_cache_fw_images();
1531 break;
1532
1533 case PM_POST_SUSPEND:
1534 case PM_POST_HIBERNATION:
1535 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001536 /*
1537 * In case that system sleep failed and syscore_suspend is
1538 * not called.
1539 */
1540 mutex_lock(&fw_lock);
1541 fw_cache.state = FW_LOADER_NO_CACHE;
1542 mutex_unlock(&fw_lock);
1543
Ming Lei07646d92012-08-04 12:01:29 +08001544 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1545 break;
1546 }
1547
1548 return 0;
1549}
Ming Lei07646d92012-08-04 12:01:29 +08001550
Ming Leiac39b3e2012-08-20 19:04:16 +08001551/* stop caching firmware once syscore_suspend is reached */
1552static int fw_suspend(void)
1553{
1554 fw_cache.state = FW_LOADER_NO_CACHE;
1555 return 0;
1556}
1557
1558static struct syscore_ops fw_syscore_ops = {
1559 .suspend = fw_suspend,
1560};
Ming Leicfe016b2012-09-08 17:32:30 +08001561#else
1562static int fw_cache_piggyback_on_request(const char *name)
1563{
1564 return 0;
1565}
1566#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001567
Ming Lei37276a52012-08-04 12:01:27 +08001568static void __init fw_cache_init(void)
1569{
1570 spin_lock_init(&fw_cache.lock);
1571 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001572 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001573
Ming Leicfe016b2012-09-08 17:32:30 +08001574#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +08001575 spin_lock_init(&fw_cache.name_lock);
1576 INIT_LIST_HEAD(&fw_cache.fw_names);
Ming Lei37276a52012-08-04 12:01:27 +08001577
Ming Lei37276a52012-08-04 12:01:27 +08001578 INIT_DELAYED_WORK(&fw_cache.work,
1579 device_uncache_fw_images_work);
Ming Lei07646d92012-08-04 12:01:29 +08001580
1581 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1582 register_pm_notifier(&fw_cache.pm_notify);
Ming Leiac39b3e2012-08-20 19:04:16 +08001583
1584 register_syscore_ops(&fw_syscore_ops);
Ming Leicfe016b2012-09-08 17:32:30 +08001585#endif
Ming Lei37276a52012-08-04 12:01:27 +08001586}
1587
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001588static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
Ming Lei1f2b7952012-08-04 12:01:21 +08001590 fw_cache_init();
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001591#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaife304142013-05-22 18:28:38 +02001592 register_reboot_notifier(&fw_shutdown_nb);
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001593 return class_register(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001594#else
1595 return 0;
1596#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001598
1599static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
Ming Leicfe016b2012-09-08 17:32:30 +08001601#ifdef CONFIG_PM_SLEEP
Ming Leiac39b3e2012-08-20 19:04:16 +08001602 unregister_syscore_ops(&fw_syscore_ops);
Ming Lei07646d92012-08-04 12:01:29 +08001603 unregister_pm_notifier(&fw_cache.pm_notify);
Ming Leicfe016b2012-09-08 17:32:30 +08001604#endif
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001605#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaife304142013-05-22 18:28:38 +02001606 unregister_reboot_notifier(&fw_shutdown_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 class_unregister(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001608#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609}
1610
Shaohua Lia30a6a22006-09-27 01:50:52 -07001611fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612module_exit(firmware_class_exit);