blob: 95f566c10a5526b9f9005bbd630da913e1e0e329 [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>
Mimi Zohare40ba6d2015-11-19 12:39:22 -050026#include <linux/fs.h>
Ming Lei37276a52012-08-04 12:01:27 +080027#include <linux/async.h>
28#include <linux/pm.h>
Ming Lei07646d92012-08-04 12:01:29 +080029#include <linux/suspend.h>
Ming Leiac39b3e2012-08-20 19:04:16 +080030#include <linux/syscore_ops.h>
Takashi Iwaife304142013-05-22 18:28:38 +020031#include <linux/reboot.h>
Kees Cook6593d922014-02-25 13:06:00 -080032#include <linux/security.h>
Daniel Wagner5b029622016-11-17 11:00:50 +010033#include <linux/swait.h>
Ming Lei37276a52012-08-04 12:01:27 +080034
Linus Torvaldsabb139e2012-10-03 15:58:32 -070035#include <generated/utsrelease.h>
36
Ming Lei37276a52012-08-04 12:01:27 +080037#include "base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Markus Rechberger87d37a42007-06-04 18:45:44 +020039MODULE_AUTHOR("Manuel Estrada Sainz");
Linus Torvalds1da177e2005-04-16 15:20:36 -070040MODULE_DESCRIPTION("Multi purpose firmware loading support");
41MODULE_LICENSE("GPL");
42
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080043/* Builtin firmware support */
44
45#ifdef CONFIG_FW_LOADER
46
47extern struct builtin_fw __start_builtin_fw[];
48extern struct builtin_fw __end_builtin_fw[];
49
Stephen Boyda098ecd2016-08-02 14:04:28 -070050static bool fw_get_builtin_firmware(struct firmware *fw, const char *name,
51 void *buf, size_t size)
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080052{
53 struct builtin_fw *b_fw;
54
55 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
56 if (strcmp(name, b_fw->name) == 0) {
57 fw->size = b_fw->size;
58 fw->data = b_fw->data;
Stephen Boyda098ecd2016-08-02 14:04:28 -070059
60 if (buf && fw->size <= size)
61 memcpy(buf, fw->data, fw->size);
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080062 return true;
63 }
64 }
65
66 return false;
67}
68
69static bool fw_is_builtin_firmware(const struct firmware *fw)
70{
71 struct builtin_fw *b_fw;
72
73 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
74 if (fw->data == b_fw->data)
75 return true;
76
77 return false;
78}
79
80#else /* Module case - no builtin firmware support */
81
Stephen Boyda098ecd2016-08-02 14:04:28 -070082static inline bool fw_get_builtin_firmware(struct firmware *fw,
83 const char *name, void *buf,
84 size_t size)
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080085{
86 return false;
87}
88
89static inline bool fw_is_builtin_firmware(const struct firmware *fw)
90{
91 return false;
92}
93#endif
94
Daniel Wagnerf52cc3792016-11-17 11:00:48 +010095enum fw_status {
96 FW_STATUS_UNKNOWN,
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 FW_STATUS_LOADING,
98 FW_STATUS_DONE,
Daniel Wagnerf52cc3792016-11-17 11:00:48 +010099 FW_STATUS_ABORTED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100};
101
Dave Jones2f651682007-01-25 15:56:15 -0500102static int loading_timeout = 60; /* In seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200104static inline long firmware_loading_timeout(void)
105{
Ming Lei68ff2a02015-01-13 00:02:01 +0800106 return loading_timeout > 0 ? loading_timeout * HZ : MAX_JIFFY_OFFSET;
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200107}
108
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100109/*
110 * Concurrent request_firmware() for the same firmware need to be
111 * serialized. struct fw_state is simple state machine which hold the
112 * state of the firmware loading.
113 */
114struct fw_state {
Daniel Wagner5b029622016-11-17 11:00:50 +0100115 struct swait_queue_head wq;
Daniel Wagner0430caf2016-11-17 11:00:49 +0100116 enum fw_status status;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100117};
118
119static void fw_state_init(struct fw_state *fw_st)
120{
Daniel Wagner5b029622016-11-17 11:00:50 +0100121 init_swait_queue_head(&fw_st->wq);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100122 fw_st->status = FW_STATUS_UNKNOWN;
123}
124
125static int __fw_state_check(struct fw_state *fw_st, enum fw_status status)
126{
Daniel Wagner0430caf2016-11-17 11:00:49 +0100127 return fw_st->status == status;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100128}
129
Daniel Wagner5b029622016-11-17 11:00:50 +0100130static inline bool __fw_state_is_done(enum fw_status status)
131{
132 return status == FW_STATUS_DONE || status == FW_STATUS_ABORTED;
133}
134
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100135static long __fw_state_wait_common(struct fw_state *fw_st, long timeout)
136{
137 long ret;
138
Daniel Wagner5b029622016-11-17 11:00:50 +0100139 ret = swait_event_interruptible_timeout(fw_st->wq,
140 __fw_state_is_done(READ_ONCE(fw_st->status)),
141 timeout);
142 if (ret != 0 && fw_st->status == FW_STATUS_ABORTED)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100143 return -ENOENT;
144
145 return ret;
146}
147
148static void __fw_state_set(struct fw_state *fw_st,
149 enum fw_status status)
150{
Daniel Wagner0430caf2016-11-17 11:00:49 +0100151 WRITE_ONCE(fw_st->status, status);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100152
Daniel Wagner0430caf2016-11-17 11:00:49 +0100153 if (status == FW_STATUS_DONE || status == FW_STATUS_ABORTED)
Daniel Wagner5b029622016-11-17 11:00:50 +0100154 swake_up(&fw_st->wq);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100155}
156
157#define fw_state_start(fw_st) \
158 __fw_state_set(fw_st, FW_STATUS_LOADING)
159#define fw_state_done(fw_st) \
160 __fw_state_set(fw_st, FW_STATUS_DONE)
161#define fw_state_is_done(fw_st) \
162 __fw_state_check(fw_st, FW_STATUS_DONE)
163#define fw_state_wait(fw_st) \
164 __fw_state_wait_common(fw_st, MAX_SCHEDULE_TIMEOUT)
165
166#ifndef CONFIG_FW_LOADER_USER_HELPER
167
168#define fw_state_is_aborted(fw_st) false
169
170#else /* CONFIG_FW_LOADER_USER_HELPER */
171
172#define fw_state_aborted(fw_st) \
173 __fw_state_set(fw_st, FW_STATUS_ABORTED)
174#define fw_state_is_loading(fw_st) \
175 __fw_state_check(fw_st, FW_STATUS_LOADING)
176#define fw_state_is_aborted(fw_st) \
177 __fw_state_check(fw_st, FW_STATUS_ABORTED)
178#define fw_state_wait_timeout(fw_st, timeout) \
179 __fw_state_wait_common(fw_st, timeout)
180
181#endif /* CONFIG_FW_LOADER_USER_HELPER */
182
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100183/* firmware behavior options */
184#define FW_OPT_UEVENT (1U << 0)
185#define FW_OPT_NOWAIT (1U << 1)
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100186#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwai5a1379e2014-06-04 17:48:15 +0200187#define FW_OPT_USERHELPER (1U << 2)
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100188#else
Takashi Iwai5a1379e2014-06-04 17:48:15 +0200189#define FW_OPT_USERHELPER 0
190#endif
191#ifdef CONFIG_FW_LOADER_USER_HELPER_FALLBACK
192#define FW_OPT_FALLBACK FW_OPT_USERHELPER
193#else
194#define FW_OPT_FALLBACK 0
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100195#endif
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -0700196#define FW_OPT_NO_WARN (1U << 3)
Vikram Mulukutla0e742e92016-08-02 14:04:25 -0700197#define FW_OPT_NOCACHE (1U << 4)
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100198
Ming Lei1f2b7952012-08-04 12:01:21 +0800199struct firmware_cache {
200 /* firmware_buf instance will be added into the below list */
201 spinlock_t lock;
202 struct list_head head;
Ming Leicfe016b2012-09-08 17:32:30 +0800203 int state;
Ming Lei37276a52012-08-04 12:01:27 +0800204
Ming Leicfe016b2012-09-08 17:32:30 +0800205#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +0800206 /*
207 * Names of firmware images which have been cached successfully
208 * will be added into the below list so that device uncache
209 * helper can trace which firmware images have been cached
210 * before.
211 */
212 spinlock_t name_lock;
213 struct list_head fw_names;
214
Ming Lei37276a52012-08-04 12:01:27 +0800215 struct delayed_work work;
Ming Lei07646d92012-08-04 12:01:29 +0800216
217 struct notifier_block pm_notify;
Ming Leicfe016b2012-09-08 17:32:30 +0800218#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800219};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Ming Lei12446912012-08-04 12:01:20 +0800221struct firmware_buf {
Ming Lei1f2b7952012-08-04 12:01:21 +0800222 struct kref ref;
223 struct list_head list;
Ming Lei1f2b7952012-08-04 12:01:21 +0800224 struct firmware_cache *fwc;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100225 struct fw_state fw_st;
Ming Lei65710cb2012-08-04 12:01:16 +0800226 void *data;
227 size_t size;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700228 size_t allocated_size;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100229#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100230 bool is_paged_buf;
Ming Leiaf5bc112013-05-27 18:30:04 +0800231 bool need_uevent;
David Woodhouse6e03a202009-04-09 22:04:07 -0700232 struct page **pages;
233 int nr_pages;
234 int page_array_size;
Takashi Iwaife304142013-05-22 18:28:38 +0200235 struct list_head pending_list;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100236#endif
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700237 const char *fw_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238};
239
Ming Lei37276a52012-08-04 12:01:27 +0800240struct fw_cache_entry {
241 struct list_head list;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700242 const char *name;
Ming Lei37276a52012-08-04 12:01:27 +0800243};
244
Ming Leif531f05a2012-08-04 12:01:25 +0800245struct fw_name_devm {
246 unsigned long magic;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700247 const char *name;
Ming Leif531f05a2012-08-04 12:01:25 +0800248};
249
Ming Lei1f2b7952012-08-04 12:01:21 +0800250#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
251
Ming Leiac39b3e2012-08-20 19:04:16 +0800252#define FW_LOADER_NO_CACHE 0
253#define FW_LOADER_START_CACHE 1
254
255static int fw_cache_piggyback_on_request(const char *name);
256
Ming Lei1f2b7952012-08-04 12:01:21 +0800257/* fw_lock could be moved to 'struct firmware_priv' but since it is just
258 * guarding for corner cases a global lock should be OK */
259static DEFINE_MUTEX(fw_lock);
260
261static struct firmware_cache fw_cache;
262
263static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
Stephen Boyda098ecd2016-08-02 14:04:28 -0700264 struct firmware_cache *fwc,
265 void *dbuf, size_t size)
Ming Lei1f2b7952012-08-04 12:01:21 +0800266{
267 struct firmware_buf *buf;
268
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700269 buf = kzalloc(sizeof(*buf), GFP_ATOMIC);
Ming Lei1f2b7952012-08-04 12:01:21 +0800270 if (!buf)
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700271 return NULL;
272
273 buf->fw_id = kstrdup_const(fw_name, GFP_ATOMIC);
274 if (!buf->fw_id) {
275 kfree(buf);
276 return NULL;
277 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800278
279 kref_init(&buf->ref);
Ming Lei1f2b7952012-08-04 12:01:21 +0800280 buf->fwc = fwc;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700281 buf->data = dbuf;
282 buf->allocated_size = size;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100283 fw_state_init(&buf->fw_st);
Takashi Iwaife304142013-05-22 18:28:38 +0200284#ifdef CONFIG_FW_LOADER_USER_HELPER
285 INIT_LIST_HEAD(&buf->pending_list);
286#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800287
288 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
289
290 return buf;
291}
292
Ming Lei2887b392012-08-04 12:01:22 +0800293static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
294{
295 struct firmware_buf *tmp;
296 struct firmware_cache *fwc = &fw_cache;
297
298 list_for_each_entry(tmp, &fwc->head, list)
299 if (!strcmp(tmp->fw_id, fw_name))
300 return tmp;
301 return NULL;
302}
303
Ming Lei1f2b7952012-08-04 12:01:21 +0800304static int fw_lookup_and_allocate_buf(const char *fw_name,
305 struct firmware_cache *fwc,
Stephen Boyda098ecd2016-08-02 14:04:28 -0700306 struct firmware_buf **buf, void *dbuf,
307 size_t size)
Ming Lei1f2b7952012-08-04 12:01:21 +0800308{
309 struct firmware_buf *tmp;
310
311 spin_lock(&fwc->lock);
Ming Lei2887b392012-08-04 12:01:22 +0800312 tmp = __fw_lookup_buf(fw_name);
313 if (tmp) {
314 kref_get(&tmp->ref);
315 spin_unlock(&fwc->lock);
316 *buf = tmp;
317 return 1;
318 }
Stephen Boyda098ecd2016-08-02 14:04:28 -0700319 tmp = __allocate_fw_buf(fw_name, fwc, dbuf, size);
Ming Lei1f2b7952012-08-04 12:01:21 +0800320 if (tmp)
321 list_add(&tmp->list, &fwc->head);
322 spin_unlock(&fwc->lock);
323
324 *buf = tmp;
325
326 return tmp ? 0 : -ENOMEM;
327}
328
329static void __fw_free_buf(struct kref *ref)
Bart Van Assche98233b22014-01-04 14:20:36 +0100330 __releases(&fwc->lock)
Ming Lei1f2b7952012-08-04 12:01:21 +0800331{
332 struct firmware_buf *buf = to_fwbuf(ref);
333 struct firmware_cache *fwc = buf->fwc;
Ming Lei1f2b7952012-08-04 12:01:21 +0800334
335 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
336 __func__, buf->fw_id, buf, buf->data,
337 (unsigned int)buf->size);
338
Ming Lei1f2b7952012-08-04 12:01:21 +0800339 list_del(&buf->list);
340 spin_unlock(&fwc->lock);
341
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100342#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100343 if (buf->is_paged_buf) {
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100344 int i;
Ming Lei746058f2012-10-09 12:01:03 +0800345 vunmap(buf->data);
346 for (i = 0; i < buf->nr_pages; i++)
347 __free_page(buf->pages[i]);
Chen Feng10a3fbf2015-12-16 17:03:15 +0800348 vfree(buf->pages);
Ming Lei746058f2012-10-09 12:01:03 +0800349 } else
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100350#endif
Stephen Boyda098ecd2016-08-02 14:04:28 -0700351 if (!buf->allocated_size)
Ming Lei746058f2012-10-09 12:01:03 +0800352 vfree(buf->data);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700353 kfree_const(buf->fw_id);
Ming Lei1f2b7952012-08-04 12:01:21 +0800354 kfree(buf);
355}
356
357static void fw_free_buf(struct firmware_buf *buf)
358{
Chuansheng Liubd9eb7f2012-11-10 01:27:22 +0800359 struct firmware_cache *fwc = buf->fwc;
360 spin_lock(&fwc->lock);
361 if (!kref_put(&buf->ref, __fw_free_buf))
362 spin_unlock(&fwc->lock);
Ming Lei1f2b7952012-08-04 12:01:21 +0800363}
364
Ming Lei746058f2012-10-09 12:01:03 +0800365/* direct firmware loading support */
Ming Lei27602842012-11-03 17:47:58 +0800366static char fw_path_para[256];
367static const char * const fw_path[] = {
368 fw_path_para,
Ming Lei746058f2012-10-09 12:01:03 +0800369 "/lib/firmware/updates/" UTS_RELEASE,
370 "/lib/firmware/updates",
371 "/lib/firmware/" UTS_RELEASE,
372 "/lib/firmware"
373};
374
Ming Lei27602842012-11-03 17:47:58 +0800375/*
376 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
377 * from kernel command line because firmware_class is generally built in
378 * kernel instead of module.
379 */
380module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
381MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
382
Stephen Boyda098ecd2016-08-02 14:04:28 -0700383static int
384fw_get_filesystem_firmware(struct device *device, struct firmware_buf *buf)
Ming Lei746058f2012-10-09 12:01:03 +0800385{
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500386 loff_t size;
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700387 int i, len;
Neil Horman3e358ac2013-09-06 15:36:08 -0400388 int rc = -ENOENT;
Luis R. Rodriguezf5727b02015-05-12 14:49:40 -0700389 char *path;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700390 enum kernel_read_file_id id = READING_FIRMWARE;
391 size_t msize = INT_MAX;
392
393 /* Already populated data member means we're loading into a buffer */
394 if (buf->data) {
395 id = READING_FIRMWARE_PREALLOC_BUFFER;
396 msize = buf->allocated_size;
397 }
Luis R. Rodriguezf5727b02015-05-12 14:49:40 -0700398
399 path = __getname();
400 if (!path)
401 return -ENOMEM;
Ming Lei746058f2012-10-09 12:01:03 +0800402
403 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
Ming Lei27602842012-11-03 17:47:58 +0800404 /* skip the unset customized path */
405 if (!fw_path[i][0])
406 continue;
407
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700408 len = snprintf(path, PATH_MAX, "%s/%s",
409 fw_path[i], buf->fw_id);
410 if (len >= PATH_MAX) {
411 rc = -ENAMETOOLONG;
412 break;
413 }
Ming Lei746058f2012-10-09 12:01:03 +0800414
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500415 buf->size = 0;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700416 rc = kernel_read_file_from_path(path, &buf->data, &size, msize,
417 id);
Kees Cook4b2530d2016-02-04 13:15:02 -0800418 if (rc) {
Luis R. Rodriguez8e516aa52016-02-28 21:57:55 +0100419 if (rc == -ENOENT)
420 dev_dbg(device, "loading %s failed with error %d\n",
421 path, rc);
422 else
423 dev_warn(device, "loading %s failed with error %d\n",
424 path, rc);
Kees Cook4b2530d2016-02-04 13:15:02 -0800425 continue;
426 }
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500427 dev_dbg(device, "direct-loading %s\n", buf->fw_id);
428 buf->size = size;
Daniel Wagner5b029622016-11-17 11:00:50 +0100429 fw_state_done(&buf->fw_st);
Kees Cook4b2530d2016-02-04 13:15:02 -0800430 break;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100431 }
Kees Cook4b2530d2016-02-04 13:15:02 -0800432 __putname(path);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100433
Neil Horman3e358ac2013-09-06 15:36:08 -0400434 return rc;
Ming Lei746058f2012-10-09 12:01:03 +0800435}
436
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100437/* firmware holds the ownership of pages */
438static void firmware_free_data(const struct firmware *fw)
439{
440 /* Loaded directly? */
441 if (!fw->priv) {
442 vfree(fw->data);
443 return;
444 }
445 fw_free_buf(fw->priv);
446}
447
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100448/* store the pages buffer info firmware from buf */
449static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
450{
451 fw->priv = buf;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100452#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100453 fw->pages = buf->pages;
454#endif
455 fw->size = buf->size;
456 fw->data = buf->data;
457
458 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
459 __func__, buf->fw_id, buf, buf->data,
460 (unsigned int)buf->size);
461}
462
463#ifdef CONFIG_PM_SLEEP
464static void fw_name_devm_release(struct device *dev, void *res)
465{
466 struct fw_name_devm *fwn = res;
467
468 if (fwn->magic == (unsigned long)&fw_cache)
469 pr_debug("%s: fw_name-%s devm-%p released\n",
470 __func__, fwn->name, res);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700471 kfree_const(fwn->name);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100472}
473
474static int fw_devm_match(struct device *dev, void *res,
475 void *match_data)
476{
477 struct fw_name_devm *fwn = res;
478
479 return (fwn->magic == (unsigned long)&fw_cache) &&
480 !strcmp(fwn->name, match_data);
481}
482
483static struct fw_name_devm *fw_find_devm_name(struct device *dev,
484 const char *name)
485{
486 struct fw_name_devm *fwn;
487
488 fwn = devres_find(dev, fw_name_devm_release,
489 fw_devm_match, (void *)name);
490 return fwn;
491}
492
493/* add firmware name into devres list */
494static int fw_add_devm_name(struct device *dev, const char *name)
495{
496 struct fw_name_devm *fwn;
497
498 fwn = fw_find_devm_name(dev, name);
499 if (fwn)
500 return 1;
501
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700502 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
503 GFP_KERNEL);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100504 if (!fwn)
505 return -ENOMEM;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700506 fwn->name = kstrdup_const(name, GFP_KERNEL);
507 if (!fwn->name) {
Vladimir Zapolskiya885de62015-07-29 23:26:28 +0300508 devres_free(fwn);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700509 return -ENOMEM;
510 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100511
512 fwn->magic = (unsigned long)&fw_cache;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100513 devres_add(dev, fwn);
514
515 return 0;
516}
517#else
518static int fw_add_devm_name(struct device *dev, const char *name)
519{
520 return 0;
521}
522#endif
523
524
525/*
526 * user-mode helper code
527 */
528#ifdef CONFIG_FW_LOADER_USER_HELPER
529struct firmware_priv {
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100530 bool nowait;
531 struct device dev;
532 struct firmware_buf *buf;
533 struct firmware *fw;
534};
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100535
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700536static struct firmware_priv *to_firmware_priv(struct device *dev)
537{
538 return container_of(dev, struct firmware_priv, dev);
539}
540
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700541static void __fw_load_abort(struct firmware_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Ming Lei87597932013-06-15 16:36:38 +0800543 /*
544 * There is a small window in which user can write to 'loading'
545 * between loading done and disappearance of 'loading'
546 */
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100547 if (fw_state_is_done(&buf->fw_st))
Ming Lei87597932013-06-15 16:36:38 +0800548 return;
549
Takashi Iwaife304142013-05-22 18:28:38 +0200550 list_del_init(&buf->pending_list);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100551 fw_state_aborted(&buf->fw_st);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700554static void fw_load_abort(struct firmware_priv *fw_priv)
555{
556 struct firmware_buf *buf = fw_priv->buf;
557
558 __fw_load_abort(buf);
Ming Lei87597932013-06-15 16:36:38 +0800559
560 /* avoid user action after loading abort */
561 fw_priv->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
Takashi Iwaife304142013-05-22 18:28:38 +0200564static LIST_HEAD(pending_fw_head);
565
566/* reboot notifier for avoid deadlock with usermode_lock */
567static int fw_shutdown_notify(struct notifier_block *unused1,
568 unsigned long unused2, void *unused3)
569{
570 mutex_lock(&fw_lock);
571 while (!list_empty(&pending_fw_head))
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700572 __fw_load_abort(list_first_entry(&pending_fw_head,
Takashi Iwaife304142013-05-22 18:28:38 +0200573 struct firmware_buf,
574 pending_list));
575 mutex_unlock(&fw_lock);
576 return NOTIFY_DONE;
577}
578
579static struct notifier_block fw_shutdown_nb = {
580 .notifier_call = fw_shutdown_notify,
581};
582
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700583static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
584 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 return sprintf(buf, "%d\n", loading_timeout);
587}
588
589/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800590 * firmware_timeout_store - set number of seconds to wait for firmware
591 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800592 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800593 * @buf: buffer to scan for timeout value
594 * @count: number of bytes in @buf
595 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800597 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 * firmware will be provided.
599 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800600 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 **/
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700602static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
603 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
605 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700606 if (loading_timeout < 0)
607 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return count;
610}
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100611static CLASS_ATTR_RW(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100613static struct attribute *firmware_class_attrs[] = {
614 &class_attr_timeout.attr,
615 NULL,
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800616};
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100617ATTRIBUTE_GROUPS(firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800619static void fw_dev_release(struct device *dev)
620{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700621 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800622
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800623 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800624}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Linus Torvalds6f957722015-07-09 11:20:01 -0700626static int do_firmware_uevent(struct firmware_priv *fw_priv, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Ming Lei12446912012-08-04 12:01:20 +0800628 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200630 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700631 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200632 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
633 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 return 0;
636}
637
Linus Torvalds6f957722015-07-09 11:20:01 -0700638static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
639{
640 struct firmware_priv *fw_priv = to_firmware_priv(dev);
641 int err = 0;
642
643 mutex_lock(&fw_lock);
644 if (fw_priv->buf)
645 err = do_firmware_uevent(fw_priv, env);
646 mutex_unlock(&fw_lock);
647 return err;
648}
649
Adrian Bunk1b81d662006-05-20 15:00:16 -0700650static struct class firmware_class = {
651 .name = "firmware",
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100652 .class_groups = firmware_class_groups,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700653 .dev_uevent = firmware_uevent,
654 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700655};
656
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700657static ssize_t firmware_loading_show(struct device *dev,
658 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700660 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800661 int loading = 0;
662
663 mutex_lock(&fw_lock);
664 if (fw_priv->buf)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100665 loading = fw_state_is_loading(&fw_priv->buf->fw_st);
Ming Lei87597932013-06-15 16:36:38 +0800666 mutex_unlock(&fw_lock);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return sprintf(buf, "%d\n", loading);
669}
670
David Woodhouse6e03a202009-04-09 22:04:07 -0700671/* Some architectures don't have PAGE_KERNEL_RO */
672#ifndef PAGE_KERNEL_RO
673#define PAGE_KERNEL_RO PAGE_KERNEL
674#endif
Ming Lei253c9242012-10-09 12:01:02 +0800675
676/* one pages buffer should be mapped/unmapped only once */
677static int fw_map_pages_buf(struct firmware_buf *buf)
678{
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100679 if (!buf->is_paged_buf)
Ming Lei746058f2012-10-09 12:01:03 +0800680 return 0;
681
Markus Elfringdaa3d672014-11-19 11:38:38 +0100682 vunmap(buf->data);
Ming Lei253c9242012-10-09 12:01:02 +0800683 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
684 if (!buf->data)
685 return -ENOMEM;
686 return 0;
687}
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800690 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700691 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800692 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800693 * @buf: buffer to scan for loading control value
694 * @count: number of bytes in @buf
695 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 * The relevant values are:
697 *
698 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800699 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 * -1: Conclude the load with an error and discard any written data.
701 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700702static ssize_t firmware_loading_store(struct device *dev,
703 struct device_attribute *attr,
704 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700706 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800707 struct firmware_buf *fw_buf;
Kees Cook6593d922014-02-25 13:06:00 -0800708 ssize_t written = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700710 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Neil Hormaneea915b2012-01-02 15:31:23 -0500712 mutex_lock(&fw_lock);
Ming Lei87597932013-06-15 16:36:38 +0800713 fw_buf = fw_priv->buf;
Ming Lei12446912012-08-04 12:01:20 +0800714 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500715 goto out;
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 switch (loading) {
718 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800719 /* discarding any previous partial load */
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100720 if (!fw_state_is_done(&fw_buf->fw_st)) {
Ming Lei12446912012-08-04 12:01:20 +0800721 for (i = 0; i < fw_buf->nr_pages; i++)
722 __free_page(fw_buf->pages[i]);
Chen Feng10a3fbf2015-12-16 17:03:15 +0800723 vfree(fw_buf->pages);
Ming Lei12446912012-08-04 12:01:20 +0800724 fw_buf->pages = NULL;
725 fw_buf->page_array_size = 0;
726 fw_buf->nr_pages = 0;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100727 fw_state_start(&fw_buf->fw_st);
Ming Lei28eefa72012-08-04 12:01:17 +0800728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 break;
730 case 0:
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100731 if (fw_state_is_loading(&fw_buf->fw_st)) {
Kees Cook6593d922014-02-25 13:06:00 -0800732 int rc;
733
Ming Lei253c9242012-10-09 12:01:02 +0800734 /*
735 * Several loading requests may be pending on
736 * one same firmware buf, so let all requests
737 * see the mapped 'buf->data' once the loading
738 * is completed.
739 * */
Kees Cook6593d922014-02-25 13:06:00 -0800740 rc = fw_map_pages_buf(fw_buf);
741 if (rc)
zhang jun2b1278c2014-02-13 15:18:47 +0800742 dev_err(dev, "%s: map pages failed\n",
743 __func__);
Kees Cook6593d922014-02-25 13:06:00 -0800744 else
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500745 rc = security_kernel_post_read_file(NULL,
746 fw_buf->data, fw_buf->size,
747 READING_FIRMWARE);
Kees Cook6593d922014-02-25 13:06:00 -0800748
749 /*
750 * Same logic as fw_load_abort, only the DONE bit
751 * is ignored and we set ABORT only on failure.
752 */
Takashi Iwaife304142013-05-22 18:28:38 +0200753 list_del_init(&fw_buf->pending_list);
Kees Cook6593d922014-02-25 13:06:00 -0800754 if (rc) {
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100755 fw_state_aborted(&fw_buf->fw_st);
Kees Cook6593d922014-02-25 13:06:00 -0800756 written = rc;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100757 } else {
758 fw_state_done(&fw_buf->fw_st);
Kees Cook6593d922014-02-25 13:06:00 -0800759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 break;
761 }
762 /* fallthrough */
763 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700764 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 /* fallthrough */
766 case -1:
767 fw_load_abort(fw_priv);
768 break;
769 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500770out:
771 mutex_unlock(&fw_lock);
Kees Cook6593d922014-02-25 13:06:00 -0800772 return written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700775static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Stephen Boyda098ecd2016-08-02 14:04:28 -0700777static void firmware_rw_buf(struct firmware_buf *buf, char *buffer,
778 loff_t offset, size_t count, bool read)
779{
780 if (read)
781 memcpy(buffer, buf->data + offset, count);
782 else
783 memcpy(buf->data + offset, buffer, count);
784}
785
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700786static void firmware_rw(struct firmware_buf *buf, char *buffer,
787 loff_t offset, size_t count, bool read)
788{
789 while (count) {
790 void *page_data;
791 int page_nr = offset >> PAGE_SHIFT;
792 int page_ofs = offset & (PAGE_SIZE-1);
793 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
794
795 page_data = kmap(buf->pages[page_nr]);
796
797 if (read)
798 memcpy(buffer, page_data + page_ofs, page_cnt);
799 else
800 memcpy(page_data + page_ofs, buffer, page_cnt);
801
802 kunmap(buf->pages[page_nr]);
803 buffer += page_cnt;
804 offset += page_cnt;
805 count -= page_cnt;
806 }
807}
808
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700809static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
810 struct bin_attribute *bin_attr,
811 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200813 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700814 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800815 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700816 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Laura Garciacad1e552006-05-23 23:22:38 +0200818 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800819 buf = fw_priv->buf;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100820 if (!buf || fw_state_is_done(&buf->fw_st)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 ret_count = -ENODEV;
822 goto out;
823 }
Ming Lei12446912012-08-04 12:01:20 +0800824 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200825 ret_count = 0;
826 goto out;
827 }
Ming Lei12446912012-08-04 12:01:20 +0800828 if (count > buf->size - offset)
829 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700830
831 ret_count = count;
832
Stephen Boyda098ecd2016-08-02 14:04:28 -0700833 if (buf->data)
834 firmware_rw_buf(buf, buffer, offset, count, true);
835 else
836 firmware_rw(buf, buffer, offset, count, true);
David Woodhouse6e03a202009-04-09 22:04:07 -0700837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838out:
Laura Garciacad1e552006-05-23 23:22:38 +0200839 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return ret_count;
841}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800842
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700843static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Ming Lei12446912012-08-04 12:01:20 +0800845 struct firmware_buf *buf = fw_priv->buf;
Fabian Fredericka76040d2014-07-01 21:13:30 +0200846 int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
David Woodhouse6e03a202009-04-09 22:04:07 -0700848 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800849 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700850 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800851 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700852 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Chen Feng10a3fbf2015-12-16 17:03:15 +0800854 new_pages = vmalloc(new_array_size * sizeof(void *));
David Woodhouse6e03a202009-04-09 22:04:07 -0700855 if (!new_pages) {
856 fw_load_abort(fw_priv);
857 return -ENOMEM;
858 }
Ming Lei12446912012-08-04 12:01:20 +0800859 memcpy(new_pages, buf->pages,
860 buf->page_array_size * sizeof(void *));
861 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
862 (new_array_size - buf->page_array_size));
Chen Feng10a3fbf2015-12-16 17:03:15 +0800863 vfree(buf->pages);
Ming Lei12446912012-08-04 12:01:20 +0800864 buf->pages = new_pages;
865 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700867
Ming Lei12446912012-08-04 12:01:20 +0800868 while (buf->nr_pages < pages_needed) {
869 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700870 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
871
Ming Lei12446912012-08-04 12:01:20 +0800872 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700873 fw_load_abort(fw_priv);
874 return -ENOMEM;
875 }
Ming Lei12446912012-08-04 12:01:20 +0800876 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return 0;
879}
880
881/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800882 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700883 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700884 * @kobj: kobject for the device
Randy Dunlap42e61f4a2007-07-23 21:42:11 -0700885 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800886 * @buffer: buffer being written
887 * @offset: buffer offset for write in total data store area
888 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800890 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 * the driver as a firmware image.
892 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700893static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
894 struct bin_attribute *bin_attr,
895 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200897 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700898 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800899 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 ssize_t retval;
901
902 if (!capable(CAP_SYS_RAWIO))
903 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700904
Laura Garciacad1e552006-05-23 23:22:38 +0200905 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800906 buf = fw_priv->buf;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100907 if (!buf || fw_state_is_done(&buf->fw_st)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 retval = -ENODEV;
909 goto out;
910 }
Ming Lei65710cb2012-08-04 12:01:16 +0800911
Stephen Boyda098ecd2016-08-02 14:04:28 -0700912 if (buf->data) {
913 if (offset + count > buf->allocated_size) {
914 retval = -ENOMEM;
915 goto out;
916 }
917 firmware_rw_buf(buf, buffer, offset, count, false);
918 retval = count;
919 } else {
920 retval = fw_realloc_buffer(fw_priv, offset + count);
921 if (retval)
922 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Stephen Boyda098ecd2016-08-02 14:04:28 -0700924 retval = count;
925 firmware_rw(buf, buffer, offset, count, false);
926 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700927
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700928 buf->size = max_t(size_t, offset + count, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929out:
Laura Garciacad1e552006-05-23 23:22:38 +0200930 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return retval;
932}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800933
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700934static struct bin_attribute firmware_attr_data = {
935 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 .size = 0,
937 .read = firmware_data_read,
938 .write = firmware_data_write,
939};
940
Takashi Iwai46239902015-02-04 15:18:07 +0100941static struct attribute *fw_dev_attrs[] = {
942 &dev_attr_loading.attr,
943 NULL
944};
945
946static struct bin_attribute *fw_dev_bin_attrs[] = {
947 &firmware_attr_data,
948 NULL
949};
950
951static const struct attribute_group fw_dev_attr_group = {
952 .attrs = fw_dev_attrs,
953 .bin_attrs = fw_dev_bin_attrs,
954};
955
956static const struct attribute_group *fw_dev_attr_groups[] = {
957 &fw_dev_attr_group,
958 NULL
959};
960
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700961static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200962fw_create_instance(struct firmware *firmware, const char *fw_name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100963 struct device *device, unsigned int opt_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700965 struct firmware_priv *fw_priv;
966 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Ming Lei12446912012-08-04 12:01:20 +0800968 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700969 if (!fw_priv) {
Ming Lei12446912012-08-04 12:01:20 +0800970 fw_priv = ERR_PTR(-ENOMEM);
971 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100974 fw_priv->nowait = !!(opt_flags & FW_OPT_NOWAIT);
Ming Lei1f2b7952012-08-04 12:01:21 +0800975 fw_priv->fw = firmware;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700976 f_dev = &fw_priv->dev;
977
978 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800979 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700980 f_dev->parent = device;
981 f_dev->class = &firmware_class;
Takashi Iwai46239902015-02-04 15:18:07 +0100982 f_dev->groups = fw_dev_attr_groups;
Ming Lei12446912012-08-04 12:01:20 +0800983exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700984 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100986
987/* load a firmware via user helper */
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100988static int _request_firmware_load(struct firmware_priv *fw_priv,
989 unsigned int opt_flags, long timeout)
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100990{
991 int retval = 0;
992 struct device *f_dev = &fw_priv->dev;
993 struct firmware_buf *buf = fw_priv->buf;
994
995 /* fall back on userspace loading */
Stephen Boyda098ecd2016-08-02 14:04:28 -0700996 if (!buf->data)
997 buf->is_paged_buf = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100998
999 dev_set_uevent_suppress(f_dev, true);
1000
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001001 retval = device_add(f_dev);
1002 if (retval) {
1003 dev_err(f_dev, "%s: device_register failed\n", __func__);
1004 goto err_put_dev;
1005 }
1006
Maxime Bizon1eeeef12013-08-29 20:28:13 +02001007 mutex_lock(&fw_lock);
1008 list_add(&buf->pending_list, &pending_fw_head);
1009 mutex_unlock(&fw_lock);
1010
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001011 if (opt_flags & FW_OPT_UEVENT) {
Ming Leiaf5bc112013-05-27 18:30:04 +08001012 buf->need_uevent = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001013 dev_set_uevent_suppress(f_dev, false);
1014 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001015 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
Ming Lei68ff2a02015-01-13 00:02:01 +08001016 } else {
1017 timeout = MAX_JIFFY_OFFSET;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001018 }
1019
Daniel Wagnerf52cc3792016-11-17 11:00:48 +01001020 timeout = fw_state_wait_timeout(&buf->fw_st, timeout);
Yves-Alexis Perez2e700f82016-11-11 11:28:40 -08001021 if (timeout == -ERESTARTSYS || !timeout) {
1022 retval = timeout;
Ming Lei0cb64242015-01-13 00:01:35 +08001023 mutex_lock(&fw_lock);
1024 fw_load_abort(fw_priv);
1025 mutex_unlock(&fw_lock);
Yves-Alexis Perez2e700f82016-11-11 11:28:40 -08001026 } else if (timeout > 0) {
Zahari Doychevef518cc2015-03-10 10:45:40 +01001027 retval = 0;
Ming Lei0cb64242015-01-13 00:01:35 +08001028 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001029
Daniel Wagnerf52cc3792016-11-17 11:00:48 +01001030 if (fw_state_is_aborted(&buf->fw_st))
Shuah Khan0542ad82014-07-22 18:10:38 -06001031 retval = -EAGAIN;
Stephen Boyda098ecd2016-08-02 14:04:28 -07001032 else if (buf->is_paged_buf && !buf->data)
zhang jun2b1278c2014-02-13 15:18:47 +08001033 retval = -ENOMEM;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001034
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001035 device_del(f_dev);
1036err_put_dev:
1037 put_device(f_dev);
1038 return retval;
1039}
1040
1041static int fw_load_from_user_helper(struct firmware *firmware,
1042 const char *name, struct device *device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001043 unsigned int opt_flags, long timeout)
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001044{
1045 struct firmware_priv *fw_priv;
1046
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001047 fw_priv = fw_create_instance(firmware, name, device, opt_flags);
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001048 if (IS_ERR(fw_priv))
1049 return PTR_ERR(fw_priv);
1050
1051 fw_priv->buf = firmware->priv;
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001052 return _request_firmware_load(fw_priv, opt_flags, timeout);
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001053}
Ming Leiddf1f062013-06-04 10:01:13 +08001054
Ming Lei5b7cb7a2013-06-06 19:52:54 +08001055#ifdef CONFIG_PM_SLEEP
Ming Leiddf1f062013-06-04 10:01:13 +08001056/* kill pending requests without uevent to avoid blocking suspend */
1057static void kill_requests_without_uevent(void)
1058{
1059 struct firmware_buf *buf;
1060 struct firmware_buf *next;
1061
1062 mutex_lock(&fw_lock);
1063 list_for_each_entry_safe(buf, next, &pending_fw_head, pending_list) {
1064 if (!buf->need_uevent)
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -07001065 __fw_load_abort(buf);
Ming Leiddf1f062013-06-04 10:01:13 +08001066 }
1067 mutex_unlock(&fw_lock);
1068}
Ming Lei5b7cb7a2013-06-06 19:52:54 +08001069#endif
Ming Leiddf1f062013-06-04 10:01:13 +08001070
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001071#else /* CONFIG_FW_LOADER_USER_HELPER */
1072static inline int
1073fw_load_from_user_helper(struct firmware *firmware, const char *name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001074 struct device *device, unsigned int opt_flags,
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001075 long timeout)
1076{
1077 return -ENOENT;
1078}
Takashi Iwai807be032013-01-31 11:13:57 +01001079
Ming Lei5b7cb7a2013-06-06 19:52:54 +08001080#ifdef CONFIG_PM_SLEEP
Ming Leiddf1f062013-06-04 10:01:13 +08001081static inline void kill_requests_without_uevent(void) { }
Ming Lei5b7cb7a2013-06-06 19:52:54 +08001082#endif
Ming Leiddf1f062013-06-04 10:01:13 +08001083
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001084#endif /* CONFIG_FW_LOADER_USER_HELPER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001086/* prepare firmware and firmware_buf structs;
1087 * return 0 if a firmware is already assigned, 1 if need to load one,
1088 * or a negative error code
1089 */
1090static int
1091_request_firmware_prepare(struct firmware **firmware_p, const char *name,
Stephen Boyda098ecd2016-08-02 14:04:28 -07001092 struct device *device, void *dbuf, size_t size)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001093{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +08001095 struct firmware_buf *buf;
1096 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Jiri Slaby4aed0642005-09-13 01:25:01 -07001098 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -07001100 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
1101 __func__);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001102 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Stephen Boyda098ecd2016-08-02 14:04:28 -07001105 if (fw_get_builtin_firmware(firmware, name, dbuf, size)) {
Luis R. Rodriguezed046302015-04-29 16:30:43 -07001106 dev_dbg(device, "using built-in %s\n", name);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001107 return 0; /* assigned */
David Woodhouse5658c762008-05-23 13:52:42 +01001108 }
1109
Stephen Boyda098ecd2016-08-02 14:04:28 -07001110 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf, dbuf, size);
Ming Lei1f2b7952012-08-04 12:01:21 +08001111
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001112 /*
1113 * bind with 'buf' now to avoid warning in failure path
1114 * of requesting firmware.
1115 */
1116 firmware->priv = buf;
Ming Lei1f2b7952012-08-04 12:01:21 +08001117
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001118 if (ret > 0) {
Daniel Wagner5b029622016-11-17 11:00:50 +01001119 ret = fw_state_wait(&buf->fw_st);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001120 if (!ret) {
1121 fw_set_page_data(buf, firmware);
1122 return 0; /* assigned */
1123 }
Stephen Boyddddb5542012-03-28 23:30:43 +02001124 }
Ming Lei1f2b7952012-08-04 12:01:21 +08001125
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001126 if (ret < 0)
1127 return ret;
1128 return 1; /* need to load */
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001129}
1130
Ming Leie771d1a2013-05-27 18:30:03 +08001131static int assign_firmware_buf(struct firmware *fw, struct device *device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001132 unsigned int opt_flags)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001133{
1134 struct firmware_buf *buf = fw->priv;
1135
1136 mutex_lock(&fw_lock);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +01001137 if (!buf->size || fw_state_is_aborted(&buf->fw_st)) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001138 mutex_unlock(&fw_lock);
1139 return -ENOENT;
1140 }
1141
1142 /*
1143 * add firmware name into devres list so that we can auto cache
1144 * and uncache firmware for device.
1145 *
1146 * device may has been deleted already, but the problem
1147 * should be fixed in devres or driver core.
1148 */
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001149 /* don't cache firmware handled without uevent */
Vikram Mulukutla0e742e92016-08-02 14:04:25 -07001150 if (device && (opt_flags & FW_OPT_UEVENT) &&
1151 !(opt_flags & FW_OPT_NOCACHE))
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001152 fw_add_devm_name(device, buf->fw_id);
1153
1154 /*
1155 * After caching firmware image is started, let it piggyback
1156 * on request firmware.
1157 */
Vikram Mulukutla0e742e92016-08-02 14:04:25 -07001158 if (!(opt_flags & FW_OPT_NOCACHE) &&
1159 buf->fwc->state == FW_LOADER_START_CACHE) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001160 if (fw_cache_piggyback_on_request(buf->fw_id))
1161 kref_get(&buf->ref);
1162 }
1163
1164 /* pass the pages buffer to driver at the last minute */
1165 fw_set_page_data(buf, fw);
1166 mutex_unlock(&fw_lock);
1167 return 0;
1168}
1169
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001170/* called from request_firmware() and request_firmware_work_func() */
1171static int
1172_request_firmware(const struct firmware **firmware_p, const char *name,
Stephen Boyda098ecd2016-08-02 14:04:28 -07001173 struct device *device, void *buf, size_t size,
1174 unsigned int opt_flags)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001175{
Brian Norris715780a2015-12-09 14:50:28 -08001176 struct firmware *fw = NULL;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001177 long timeout;
1178 int ret;
1179
1180 if (!firmware_p)
1181 return -EINVAL;
1182
Brian Norris715780a2015-12-09 14:50:28 -08001183 if (!name || name[0] == '\0') {
1184 ret = -EINVAL;
1185 goto out;
1186 }
Kees Cook471b0952014-09-18 11:25:37 -07001187
Stephen Boyda098ecd2016-08-02 14:04:28 -07001188 ret = _request_firmware_prepare(&fw, name, device, buf, size);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001189 if (ret <= 0) /* error or already assigned */
1190 goto out;
1191
1192 ret = 0;
1193 timeout = firmware_loading_timeout();
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001194 if (opt_flags & FW_OPT_NOWAIT) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001195 timeout = usermodehelper_read_lock_wait(timeout);
1196 if (!timeout) {
1197 dev_dbg(device, "firmware: %s loading timed out\n",
1198 name);
1199 ret = -EBUSY;
1200 goto out;
1201 }
1202 } else {
1203 ret = usermodehelper_read_trylock();
1204 if (WARN_ON(ret)) {
1205 dev_err(device, "firmware: %s will not be loaded\n",
1206 name);
1207 goto out;
1208 }
1209 }
1210
Neil Horman3e358ac2013-09-06 15:36:08 -04001211 ret = fw_get_filesystem_firmware(device, fw->priv);
1212 if (ret) {
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001213 if (!(opt_flags & FW_OPT_NO_WARN))
Takashi Iwaibba3a872013-12-02 15:38:16 +01001214 dev_warn(device,
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001215 "Direct firmware load for %s failed with error %d\n",
1216 name, ret);
1217 if (opt_flags & FW_OPT_USERHELPER) {
Takashi Iwaibba3a872013-12-02 15:38:16 +01001218 dev_warn(device, "Falling back to user helper\n");
1219 ret = fw_load_from_user_helper(fw, name, device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001220 opt_flags, timeout);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001221 }
Neil Horman3e358ac2013-09-06 15:36:08 -04001222 }
Ming Leie771d1a2013-05-27 18:30:03 +08001223
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001224 if (!ret)
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001225 ret = assign_firmware_buf(fw, device, opt_flags);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001226
1227 usermodehelper_read_unlock();
1228
1229 out:
1230 if (ret < 0) {
1231 release_firmware(fw);
1232 fw = NULL;
1233 }
1234
1235 *firmware_p = fw;
1236 return ret;
1237}
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239/**
Kay Sievers312c0042005-11-16 09:00:00 +01001240 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001241 * @firmware_p: pointer to firmware image
1242 * @name: name of firmware file
1243 * @device: device for which firmware is being loaded
1244 *
1245 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001246 * of @name for device @device.
1247 *
1248 * Should be called from user context where sleeping is allowed.
1249 *
Kay Sievers312c0042005-11-16 09:00:00 +01001250 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001251 * should be distinctive enough not to be confused with any other
1252 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +08001253 *
1254 * Caller must hold the reference count of @device.
Ming Lei6a927852012-11-03 17:48:16 +08001255 *
1256 * The function can be called safely inside device's suspend and
1257 * resume callback.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001258 **/
1259int
1260request_firmware(const struct firmware **firmware_p, const char *name,
Andrei Opreaea310032015-03-08 12:41:15 +02001261 struct device *device)
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001262{
Ming Leid6c8aa32013-06-06 20:01:48 +08001263 int ret;
1264
1265 /* Need to pin this module until return */
1266 __module_get(THIS_MODULE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001267 ret = _request_firmware(firmware_p, name, device, NULL, 0,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001268 FW_OPT_UEVENT | FW_OPT_FALLBACK);
Ming Leid6c8aa32013-06-06 20:01:48 +08001269 module_put(THIS_MODULE);
1270 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001271}
Daniel Mackf4945132013-05-23 22:17:18 +02001272EXPORT_SYMBOL(request_firmware);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001273
Takashi Iwaibba3a872013-12-02 15:38:16 +01001274/**
Borislav Petkov3c1556b2014-12-03 22:46:37 +01001275 * request_firmware_direct: - load firmware directly without usermode helper
Takashi Iwaibba3a872013-12-02 15:38:16 +01001276 * @firmware_p: pointer to firmware image
1277 * @name: name of firmware file
1278 * @device: device for which firmware is being loaded
1279 *
1280 * This function works pretty much like request_firmware(), but this doesn't
1281 * fall back to usermode helper even if the firmware couldn't be loaded
1282 * directly from fs. Hence it's useful for loading optional firmwares, which
1283 * aren't always present, without extra long timeouts of udev.
1284 **/
1285int request_firmware_direct(const struct firmware **firmware_p,
1286 const char *name, struct device *device)
1287{
1288 int ret;
Andrei Opreaea310032015-03-08 12:41:15 +02001289
Takashi Iwaibba3a872013-12-02 15:38:16 +01001290 __module_get(THIS_MODULE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001291 ret = _request_firmware(firmware_p, name, device, NULL, 0,
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001292 FW_OPT_UEVENT | FW_OPT_NO_WARN);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001293 module_put(THIS_MODULE);
1294 return ret;
1295}
1296EXPORT_SYMBOL_GPL(request_firmware_direct);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001297
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001298/**
Stephen Boyda098ecd2016-08-02 14:04:28 -07001299 * request_firmware_into_buf - load firmware into a previously allocated buffer
1300 * @firmware_p: pointer to firmware image
1301 * @name: name of firmware file
1302 * @device: device for which firmware is being loaded and DMA region allocated
1303 * @buf: address of buffer to load firmware into
1304 * @size: size of buffer
1305 *
1306 * This function works pretty much like request_firmware(), but it doesn't
1307 * allocate a buffer to hold the firmware data. Instead, the firmware
1308 * is loaded directly into the buffer pointed to by @buf and the @firmware_p
1309 * data member is pointed at @buf.
1310 *
1311 * This function doesn't cache firmware either.
1312 */
1313int
1314request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
1315 struct device *device, void *buf, size_t size)
1316{
1317 int ret;
1318
1319 __module_get(THIS_MODULE);
1320 ret = _request_firmware(firmware_p, name, device, buf, size,
1321 FW_OPT_UEVENT | FW_OPT_FALLBACK |
1322 FW_OPT_NOCACHE);
1323 module_put(THIS_MODULE);
1324 return ret;
1325}
1326EXPORT_SYMBOL(request_firmware_into_buf);
1327
1328/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001330 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001332void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001335 if (!fw_is_builtin_firmware(fw))
1336 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 kfree(fw);
1338 }
1339}
Daniel Mackf4945132013-05-23 22:17:18 +02001340EXPORT_SYMBOL(release_firmware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342/* Async support */
1343struct firmware_work {
1344 struct work_struct work;
1345 struct module *module;
1346 const char *name;
1347 struct device *device;
1348 void *context;
1349 void (*cont)(const struct firmware *fw, void *context);
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001350 unsigned int opt_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351};
1352
Stephen Boyda36cf842012-03-28 23:31:00 +02001353static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Stephen Boyda36cf842012-03-28 23:31:00 +02001355 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 const struct firmware *fw;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001357
Stephen Boyda36cf842012-03-28 23:31:00 +02001358 fw_work = container_of(work, struct firmware_work, work);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001359
Stephen Boyda098ecd2016-08-02 14:04:28 -07001360 _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001361 fw_work->opt_flags);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001362 fw_work->cont(fw, fw_work->context);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001363 put_device(fw_work->device); /* taken in request_firmware_nowait() */
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 module_put(fw_work->module);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001366 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369
1370/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001371 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001372 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001373 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001374 * is non-zero else the firmware copy must be done manually.
1375 * @name: name of firmware file
1376 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001377 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001378 * @context: will be passed over to @cont, and
1379 * @fw may be %NULL if firmware request fails.
1380 * @cont: function will be called asynchronously when the firmware
1381 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001383 * Caller must hold the reference count of @device.
1384 *
Ming Lei6f21a622012-08-04 12:01:24 +08001385 * Asynchronous variant of request_firmware() for user contexts:
1386 * - sleep for as small periods as possible since it may
1387 * increase kernel boot time of built-in device drivers
1388 * requesting firmware in their ->probe() methods, if
1389 * @gfp is GFP_KERNEL.
1390 *
1391 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 **/
1393int
1394request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001395 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001396 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 void (*cont)(const struct firmware *fw, void *context))
1398{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001399 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Andrei Opreaea310032015-03-08 12:41:15 +02001401 fw_work = kzalloc(sizeof(struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 if (!fw_work)
1403 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001404
1405 fw_work->module = module;
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001406 fw_work->name = kstrdup_const(name, gfp);
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001407 if (!fw_work->name) {
1408 kfree(fw_work);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001409 return -ENOMEM;
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001410 }
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001411 fw_work->device = device;
1412 fw_work->context = context;
1413 fw_work->cont = cont;
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001414 fw_work->opt_flags = FW_OPT_NOWAIT | FW_OPT_FALLBACK |
Takashi Iwai5a1379e2014-06-04 17:48:15 +02001415 (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 if (!try_module_get(module)) {
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001418 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 kfree(fw_work);
1420 return -EFAULT;
1421 }
1422
Ming Lei0cfc1e12012-08-04 12:01:23 +08001423 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001424 INIT_WORK(&fw_work->work, request_firmware_work_func);
1425 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return 0;
1427}
Daniel Mackf4945132013-05-23 22:17:18 +02001428EXPORT_SYMBOL(request_firmware_nowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Ming Lei90f890812013-06-20 12:30:16 +08001430#ifdef CONFIG_PM_SLEEP
1431static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1432
Ming Lei2887b392012-08-04 12:01:22 +08001433/**
1434 * cache_firmware - cache one firmware image in kernel memory space
1435 * @fw_name: the firmware image name
1436 *
1437 * Cache firmware in kernel memory so that drivers can use it when
1438 * system isn't ready for them to request firmware image from userspace.
1439 * Once it returns successfully, driver can use request_firmware or its
1440 * nowait version to get the cached firmware without any interacting
1441 * with userspace
1442 *
1443 * Return 0 if the firmware image has been cached successfully
1444 * Return !0 otherwise
1445 *
1446 */
Ming Lei93232e42013-06-06 20:01:47 +08001447static int cache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001448{
1449 int ret;
1450 const struct firmware *fw;
1451
1452 pr_debug("%s: %s\n", __func__, fw_name);
1453
1454 ret = request_firmware(&fw, fw_name, NULL);
1455 if (!ret)
1456 kfree(fw);
1457
1458 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1459
1460 return ret;
1461}
1462
Ming Lei6a2c1232013-06-26 09:28:17 +08001463static struct firmware_buf *fw_lookup_buf(const char *fw_name)
1464{
1465 struct firmware_buf *tmp;
1466 struct firmware_cache *fwc = &fw_cache;
1467
1468 spin_lock(&fwc->lock);
1469 tmp = __fw_lookup_buf(fw_name);
1470 spin_unlock(&fwc->lock);
1471
1472 return tmp;
1473}
1474
Ming Lei2887b392012-08-04 12:01:22 +08001475/**
1476 * uncache_firmware - remove one cached firmware image
1477 * @fw_name: the firmware image name
1478 *
1479 * Uncache one firmware image which has been cached successfully
1480 * before.
1481 *
1482 * Return 0 if the firmware cache has been removed successfully
1483 * Return !0 otherwise
1484 *
1485 */
Ming Lei93232e42013-06-06 20:01:47 +08001486static int uncache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001487{
1488 struct firmware_buf *buf;
1489 struct firmware fw;
1490
1491 pr_debug("%s: %s\n", __func__, fw_name);
1492
Stephen Boyda098ecd2016-08-02 14:04:28 -07001493 if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0))
Ming Lei2887b392012-08-04 12:01:22 +08001494 return 0;
1495
1496 buf = fw_lookup_buf(fw_name);
1497 if (buf) {
1498 fw_free_buf(buf);
1499 return 0;
1500 }
1501
1502 return -EINVAL;
1503}
1504
Ming Lei37276a52012-08-04 12:01:27 +08001505static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1506{
1507 struct fw_cache_entry *fce;
1508
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001509 fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
Ming Lei37276a52012-08-04 12:01:27 +08001510 if (!fce)
1511 goto exit;
1512
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001513 fce->name = kstrdup_const(name, GFP_ATOMIC);
1514 if (!fce->name) {
1515 kfree(fce);
1516 fce = NULL;
1517 goto exit;
1518 }
Ming Lei37276a52012-08-04 12:01:27 +08001519exit:
1520 return fce;
1521}
1522
Ming Lei373304f2012-10-09 12:01:01 +08001523static int __fw_entry_found(const char *name)
1524{
1525 struct firmware_cache *fwc = &fw_cache;
1526 struct fw_cache_entry *fce;
1527
1528 list_for_each_entry(fce, &fwc->fw_names, list) {
1529 if (!strcmp(fce->name, name))
1530 return 1;
1531 }
1532 return 0;
1533}
1534
Ming Leiac39b3e2012-08-20 19:04:16 +08001535static int fw_cache_piggyback_on_request(const char *name)
1536{
1537 struct firmware_cache *fwc = &fw_cache;
1538 struct fw_cache_entry *fce;
1539 int ret = 0;
1540
1541 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001542 if (__fw_entry_found(name))
1543 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001544
1545 fce = alloc_fw_cache_entry(name);
1546 if (fce) {
1547 ret = 1;
1548 list_add(&fce->list, &fwc->fw_names);
1549 pr_debug("%s: fw: %s\n", __func__, name);
1550 }
1551found:
1552 spin_unlock(&fwc->name_lock);
1553 return ret;
1554}
1555
Ming Lei37276a52012-08-04 12:01:27 +08001556static void free_fw_cache_entry(struct fw_cache_entry *fce)
1557{
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001558 kfree_const(fce->name);
Ming Lei37276a52012-08-04 12:01:27 +08001559 kfree(fce);
1560}
1561
1562static void __async_dev_cache_fw_image(void *fw_entry,
1563 async_cookie_t cookie)
1564{
1565 struct fw_cache_entry *fce = fw_entry;
1566 struct firmware_cache *fwc = &fw_cache;
1567 int ret;
1568
1569 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001570 if (ret) {
1571 spin_lock(&fwc->name_lock);
1572 list_del(&fce->list);
1573 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001574
Ming Leiac39b3e2012-08-20 19:04:16 +08001575 free_fw_cache_entry(fce);
1576 }
Ming Lei37276a52012-08-04 12:01:27 +08001577}
1578
1579/* called with dev->devres_lock held */
1580static void dev_create_fw_entry(struct device *dev, void *res,
1581 void *data)
1582{
1583 struct fw_name_devm *fwn = res;
1584 const char *fw_name = fwn->name;
1585 struct list_head *head = data;
1586 struct fw_cache_entry *fce;
1587
1588 fce = alloc_fw_cache_entry(fw_name);
1589 if (fce)
1590 list_add(&fce->list, head);
1591}
1592
1593static int devm_name_match(struct device *dev, void *res,
1594 void *match_data)
1595{
1596 struct fw_name_devm *fwn = res;
1597 return (fwn->magic == (unsigned long)match_data);
1598}
1599
Ming Leiab6dd8e2012-08-17 22:07:00 +08001600static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001601{
1602 LIST_HEAD(todo);
1603 struct fw_cache_entry *fce;
1604 struct fw_cache_entry *fce_next;
1605 struct firmware_cache *fwc = &fw_cache;
1606
1607 devres_for_each_res(dev, fw_name_devm_release,
1608 devm_name_match, &fw_cache,
1609 dev_create_fw_entry, &todo);
1610
1611 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1612 list_del(&fce->list);
1613
1614 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001615 /* only one cache entry for one firmware */
1616 if (!__fw_entry_found(fce->name)) {
Ming Lei373304f2012-10-09 12:01:01 +08001617 list_add(&fce->list, &fwc->fw_names);
1618 } else {
1619 free_fw_cache_entry(fce);
1620 fce = NULL;
1621 }
Ming Lei37276a52012-08-04 12:01:27 +08001622 spin_unlock(&fwc->name_lock);
1623
Ming Lei373304f2012-10-09 12:01:01 +08001624 if (fce)
Ming Leid28d3882012-10-09 12:01:04 +08001625 async_schedule_domain(__async_dev_cache_fw_image,
1626 (void *)fce,
1627 &fw_cache_domain);
Ming Lei37276a52012-08-04 12:01:27 +08001628 }
1629}
1630
1631static void __device_uncache_fw_images(void)
1632{
1633 struct firmware_cache *fwc = &fw_cache;
1634 struct fw_cache_entry *fce;
1635
1636 spin_lock(&fwc->name_lock);
1637 while (!list_empty(&fwc->fw_names)) {
1638 fce = list_entry(fwc->fw_names.next,
1639 struct fw_cache_entry, list);
1640 list_del(&fce->list);
1641 spin_unlock(&fwc->name_lock);
1642
1643 uncache_firmware(fce->name);
1644 free_fw_cache_entry(fce);
1645
1646 spin_lock(&fwc->name_lock);
1647 }
1648 spin_unlock(&fwc->name_lock);
1649}
1650
1651/**
1652 * device_cache_fw_images - cache devices' firmware
1653 *
1654 * If one device called request_firmware or its nowait version
1655 * successfully before, the firmware names are recored into the
1656 * device's devres link list, so device_cache_fw_images can call
1657 * cache_firmware() to cache these firmwares for the device,
1658 * then the device driver can load its firmwares easily at
1659 * time when system is not ready to complete loading firmware.
1660 */
1661static void device_cache_fw_images(void)
1662{
1663 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001664 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001665 DEFINE_WAIT(wait);
1666
1667 pr_debug("%s\n", __func__);
1668
Ming Lei373304f2012-10-09 12:01:01 +08001669 /* cancel uncache work */
1670 cancel_delayed_work_sync(&fwc->work);
1671
Ming Leiffe53f62012-08-04 12:01:28 +08001672 /*
1673 * use small loading timeout for caching devices' firmware
1674 * because all these firmware images have been loaded
1675 * successfully at lease once, also system is ready for
1676 * completing firmware loading now. The maximum size of
1677 * firmware in current distributions is about 2M bytes,
1678 * so 10 secs should be enough.
1679 */
1680 old_timeout = loading_timeout;
1681 loading_timeout = 10;
1682
Ming Leiac39b3e2012-08-20 19:04:16 +08001683 mutex_lock(&fw_lock);
1684 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001685 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001686 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001687
1688 /* wait for completion of caching firmware for all devices */
Ming Leid28d3882012-10-09 12:01:04 +08001689 async_synchronize_full_domain(&fw_cache_domain);
Ming Leiffe53f62012-08-04 12:01:28 +08001690
1691 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001692}
1693
1694/**
1695 * device_uncache_fw_images - uncache devices' firmware
1696 *
1697 * uncache all firmwares which have been cached successfully
1698 * by device_uncache_fw_images earlier
1699 */
1700static void device_uncache_fw_images(void)
1701{
1702 pr_debug("%s\n", __func__);
1703 __device_uncache_fw_images();
1704}
1705
1706static void device_uncache_fw_images_work(struct work_struct *work)
1707{
1708 device_uncache_fw_images();
1709}
1710
1711/**
1712 * device_uncache_fw_images_delay - uncache devices firmwares
1713 * @delay: number of milliseconds to delay uncache device firmwares
1714 *
1715 * uncache all devices's firmwares which has been cached successfully
1716 * by device_cache_fw_images after @delay milliseconds.
1717 */
1718static void device_uncache_fw_images_delay(unsigned long delay)
1719{
Shaibal Duttabce66182014-01-31 15:44:58 -08001720 queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
1721 msecs_to_jiffies(delay));
Ming Lei37276a52012-08-04 12:01:27 +08001722}
1723
Ming Lei07646d92012-08-04 12:01:29 +08001724static int fw_pm_notify(struct notifier_block *notify_block,
1725 unsigned long mode, void *unused)
1726{
1727 switch (mode) {
1728 case PM_HIBERNATION_PREPARE:
1729 case PM_SUSPEND_PREPARE:
Sebastian Capellaf8d5b9e2014-02-18 17:52:08 -08001730 case PM_RESTORE_PREPARE:
Ming Leiaf5bc112013-05-27 18:30:04 +08001731 kill_requests_without_uevent();
Ming Lei07646d92012-08-04 12:01:29 +08001732 device_cache_fw_images();
1733 break;
1734
1735 case PM_POST_SUSPEND:
1736 case PM_POST_HIBERNATION:
1737 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001738 /*
1739 * In case that system sleep failed and syscore_suspend is
1740 * not called.
1741 */
1742 mutex_lock(&fw_lock);
1743 fw_cache.state = FW_LOADER_NO_CACHE;
1744 mutex_unlock(&fw_lock);
1745
Ming Lei07646d92012-08-04 12:01:29 +08001746 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1747 break;
1748 }
1749
1750 return 0;
1751}
Ming Lei07646d92012-08-04 12:01:29 +08001752
Ming Leiac39b3e2012-08-20 19:04:16 +08001753/* stop caching firmware once syscore_suspend is reached */
1754static int fw_suspend(void)
1755{
1756 fw_cache.state = FW_LOADER_NO_CACHE;
1757 return 0;
1758}
1759
1760static struct syscore_ops fw_syscore_ops = {
1761 .suspend = fw_suspend,
1762};
Ming Leicfe016b2012-09-08 17:32:30 +08001763#else
1764static int fw_cache_piggyback_on_request(const char *name)
1765{
1766 return 0;
1767}
1768#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001769
Ming Lei37276a52012-08-04 12:01:27 +08001770static void __init fw_cache_init(void)
1771{
1772 spin_lock_init(&fw_cache.lock);
1773 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001774 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001775
Ming Leicfe016b2012-09-08 17:32:30 +08001776#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +08001777 spin_lock_init(&fw_cache.name_lock);
1778 INIT_LIST_HEAD(&fw_cache.fw_names);
Ming Lei37276a52012-08-04 12:01:27 +08001779
Ming Lei37276a52012-08-04 12:01:27 +08001780 INIT_DELAYED_WORK(&fw_cache.work,
1781 device_uncache_fw_images_work);
Ming Lei07646d92012-08-04 12:01:29 +08001782
1783 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1784 register_pm_notifier(&fw_cache.pm_notify);
Ming Leiac39b3e2012-08-20 19:04:16 +08001785
1786 register_syscore_ops(&fw_syscore_ops);
Ming Leicfe016b2012-09-08 17:32:30 +08001787#endif
Ming Lei37276a52012-08-04 12:01:27 +08001788}
1789
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001790static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
Ming Lei1f2b7952012-08-04 12:01:21 +08001792 fw_cache_init();
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001793#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaife304142013-05-22 18:28:38 +02001794 register_reboot_notifier(&fw_shutdown_nb);
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001795 return class_register(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001796#else
1797 return 0;
1798#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001800
1801static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
Ming Leicfe016b2012-09-08 17:32:30 +08001803#ifdef CONFIG_PM_SLEEP
Ming Leiac39b3e2012-08-20 19:04:16 +08001804 unregister_syscore_ops(&fw_syscore_ops);
Ming Lei07646d92012-08-04 12:01:29 +08001805 unregister_pm_notifier(&fw_cache.pm_notify);
Ming Leicfe016b2012-09-08 17:32:30 +08001806#endif
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001807#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaife304142013-05-22 18:28:38 +02001808 unregister_reboot_notifier(&fw_shutdown_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 class_unregister(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001810#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
1812
Shaohua Lia30a6a22006-09-27 01:50:52 -07001813fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814module_exit(firmware_class_exit);