Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * firmware_class.c - Multi purpose firmware loading support |
| 3 | * |
Markus Rechberger | 87d37a4 | 2007-06-04 18:45:44 +0200 | [diff] [blame] | 4 | * Copyright (c) 2003 Manuel Estrada Sainz |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * Please see Documentation/firmware_class/ for more information. |
| 7 | * |
| 8 | */ |
| 9 | |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 10 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #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 Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 18 | #include <linux/mutex.h> |
Stephen Boyd | a36cf84 | 2012-03-28 23:31:00 +0200 | [diff] [blame] | 19 | #include <linux/workqueue.h> |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 20 | #include <linux/highmem.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/firmware.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Stephen Boyd | a36cf84 | 2012-03-28 23:31:00 +0200 | [diff] [blame] | 23 | #include <linux/sched.h> |
Linus Torvalds | abb139e | 2012-10-03 15:58:32 -0700 | [diff] [blame] | 24 | #include <linux/file.h> |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 25 | #include <linux/list.h> |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 26 | #include <linux/async.h> |
| 27 | #include <linux/pm.h> |
Ming Lei | 07646d9 | 2012-08-04 12:01:29 +0800 | [diff] [blame] | 28 | #include <linux/suspend.h> |
Ming Lei | ac39b3e | 2012-08-20 19:04:16 +0800 | [diff] [blame] | 29 | #include <linux/syscore_ops.h> |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 30 | |
Linus Torvalds | abb139e | 2012-10-03 15:58:32 -0700 | [diff] [blame] | 31 | #include <generated/utsrelease.h> |
| 32 | |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 33 | #include "base.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Markus Rechberger | 87d37a4 | 2007-06-04 18:45:44 +0200 | [diff] [blame] | 35 | MODULE_AUTHOR("Manuel Estrada Sainz"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | MODULE_DESCRIPTION("Multi purpose firmware loading support"); |
| 37 | MODULE_LICENSE("GPL"); |
| 38 | |
Dmitry Torokhov | bcb9bd1 | 2010-03-13 23:49:18 -0800 | [diff] [blame] | 39 | /* Builtin firmware support */ |
| 40 | |
| 41 | #ifdef CONFIG_FW_LOADER |
| 42 | |
| 43 | extern struct builtin_fw __start_builtin_fw[]; |
| 44 | extern struct builtin_fw __end_builtin_fw[]; |
| 45 | |
| 46 | static bool fw_get_builtin_firmware(struct firmware *fw, const char *name) |
| 47 | { |
| 48 | struct builtin_fw *b_fw; |
| 49 | |
| 50 | for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) { |
| 51 | if (strcmp(name, b_fw->name) == 0) { |
| 52 | fw->size = b_fw->size; |
| 53 | fw->data = b_fw->data; |
| 54 | return true; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | static bool fw_is_builtin_firmware(const struct firmware *fw) |
| 62 | { |
| 63 | struct builtin_fw *b_fw; |
| 64 | |
| 65 | for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) |
| 66 | if (fw->data == b_fw->data) |
| 67 | return true; |
| 68 | |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | #else /* Module case - no builtin firmware support */ |
| 73 | |
| 74 | static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name) |
| 75 | { |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | static inline bool fw_is_builtin_firmware(const struct firmware *fw) |
| 80 | { |
| 81 | return false; |
| 82 | } |
| 83 | #endif |
| 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | enum { |
| 86 | FW_STATUS_LOADING, |
| 87 | FW_STATUS_DONE, |
| 88 | FW_STATUS_ABORT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
Ming Lei | 746058f | 2012-10-09 12:01:03 +0800 | [diff] [blame] | 91 | enum fw_buf_fmt { |
| 92 | VMALLOC_BUF, /* used in direct loading */ |
| 93 | PAGE_BUF, /* used in loading via userspace */ |
| 94 | }; |
| 95 | |
Dave Jones | 2f65168 | 2007-01-25 15:56:15 -0500 | [diff] [blame] | 96 | static int loading_timeout = 60; /* In seconds */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Rafael J. Wysocki | 9b78c1d | 2012-03-28 23:30:02 +0200 | [diff] [blame] | 98 | static inline long firmware_loading_timeout(void) |
| 99 | { |
| 100 | return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT; |
| 101 | } |
| 102 | |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 103 | struct firmware_cache { |
| 104 | /* firmware_buf instance will be added into the below list */ |
| 105 | spinlock_t lock; |
| 106 | struct list_head head; |
Ming Lei | cfe016b | 2012-09-08 17:32:30 +0800 | [diff] [blame] | 107 | int state; |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 108 | |
Ming Lei | cfe016b | 2012-09-08 17:32:30 +0800 | [diff] [blame] | 109 | #ifdef CONFIG_PM_SLEEP |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 110 | /* |
| 111 | * Names of firmware images which have been cached successfully |
| 112 | * will be added into the below list so that device uncache |
| 113 | * helper can trace which firmware images have been cached |
| 114 | * before. |
| 115 | */ |
| 116 | spinlock_t name_lock; |
| 117 | struct list_head fw_names; |
| 118 | |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 119 | struct delayed_work work; |
Ming Lei | 07646d9 | 2012-08-04 12:01:29 +0800 | [diff] [blame] | 120 | |
| 121 | struct notifier_block pm_notify; |
Ming Lei | cfe016b | 2012-09-08 17:32:30 +0800 | [diff] [blame] | 122 | #endif |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 123 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 125 | struct firmware_buf { |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 126 | struct kref ref; |
| 127 | struct list_head list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | struct completion completion; |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 129 | struct firmware_cache *fwc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | unsigned long status; |
Ming Lei | 746058f | 2012-10-09 12:01:03 +0800 | [diff] [blame] | 131 | enum fw_buf_fmt fmt; |
Ming Lei | 65710cb | 2012-08-04 12:01:16 +0800 | [diff] [blame] | 132 | void *data; |
| 133 | size_t size; |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 134 | struct page **pages; |
| 135 | int nr_pages; |
| 136 | int page_array_size; |
Dmitry Torokhov | e177123 | 2010-03-13 23:49:23 -0800 | [diff] [blame] | 137 | char fw_id[]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 140 | struct fw_cache_entry { |
| 141 | struct list_head list; |
| 142 | char name[]; |
| 143 | }; |
| 144 | |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 145 | struct firmware_priv { |
| 146 | struct timer_list timeout; |
| 147 | bool nowait; |
| 148 | struct device dev; |
| 149 | struct firmware_buf *buf; |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 150 | struct firmware *fw; |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 151 | }; |
| 152 | |
Ming Lei | f531f05a | 2012-08-04 12:01:25 +0800 | [diff] [blame] | 153 | struct fw_name_devm { |
| 154 | unsigned long magic; |
| 155 | char name[]; |
| 156 | }; |
| 157 | |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 158 | #define to_fwbuf(d) container_of(d, struct firmware_buf, ref) |
| 159 | |
Ming Lei | ac39b3e | 2012-08-20 19:04:16 +0800 | [diff] [blame] | 160 | #define FW_LOADER_NO_CACHE 0 |
| 161 | #define FW_LOADER_START_CACHE 1 |
| 162 | |
| 163 | static int fw_cache_piggyback_on_request(const char *name); |
| 164 | |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 165 | /* fw_lock could be moved to 'struct firmware_priv' but since it is just |
| 166 | * guarding for corner cases a global lock should be OK */ |
| 167 | static DEFINE_MUTEX(fw_lock); |
| 168 | |
| 169 | static struct firmware_cache fw_cache; |
| 170 | |
| 171 | static struct firmware_buf *__allocate_fw_buf(const char *fw_name, |
| 172 | struct firmware_cache *fwc) |
| 173 | { |
| 174 | struct firmware_buf *buf; |
| 175 | |
| 176 | buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC); |
| 177 | |
| 178 | if (!buf) |
| 179 | return buf; |
| 180 | |
| 181 | kref_init(&buf->ref); |
| 182 | strcpy(buf->fw_id, fw_name); |
| 183 | buf->fwc = fwc; |
| 184 | init_completion(&buf->completion); |
Ming Lei | 746058f | 2012-10-09 12:01:03 +0800 | [diff] [blame] | 185 | buf->fmt = VMALLOC_BUF; |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 186 | |
| 187 | pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf); |
| 188 | |
| 189 | return buf; |
| 190 | } |
| 191 | |
Ming Lei | 2887b39 | 2012-08-04 12:01:22 +0800 | [diff] [blame] | 192 | static struct firmware_buf *__fw_lookup_buf(const char *fw_name) |
| 193 | { |
| 194 | struct firmware_buf *tmp; |
| 195 | struct firmware_cache *fwc = &fw_cache; |
| 196 | |
| 197 | list_for_each_entry(tmp, &fwc->head, list) |
| 198 | if (!strcmp(tmp->fw_id, fw_name)) |
| 199 | return tmp; |
| 200 | return NULL; |
| 201 | } |
| 202 | |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 203 | static int fw_lookup_and_allocate_buf(const char *fw_name, |
| 204 | struct firmware_cache *fwc, |
| 205 | struct firmware_buf **buf) |
| 206 | { |
| 207 | struct firmware_buf *tmp; |
| 208 | |
| 209 | spin_lock(&fwc->lock); |
Ming Lei | 2887b39 | 2012-08-04 12:01:22 +0800 | [diff] [blame] | 210 | tmp = __fw_lookup_buf(fw_name); |
| 211 | if (tmp) { |
| 212 | kref_get(&tmp->ref); |
| 213 | spin_unlock(&fwc->lock); |
| 214 | *buf = tmp; |
| 215 | return 1; |
| 216 | } |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 217 | tmp = __allocate_fw_buf(fw_name, fwc); |
| 218 | if (tmp) |
| 219 | list_add(&tmp->list, &fwc->head); |
| 220 | spin_unlock(&fwc->lock); |
| 221 | |
| 222 | *buf = tmp; |
| 223 | |
| 224 | return tmp ? 0 : -ENOMEM; |
| 225 | } |
| 226 | |
Ming Lei | 2887b39 | 2012-08-04 12:01:22 +0800 | [diff] [blame] | 227 | static struct firmware_buf *fw_lookup_buf(const char *fw_name) |
| 228 | { |
| 229 | struct firmware_buf *tmp; |
| 230 | struct firmware_cache *fwc = &fw_cache; |
| 231 | |
| 232 | spin_lock(&fwc->lock); |
| 233 | tmp = __fw_lookup_buf(fw_name); |
| 234 | spin_unlock(&fwc->lock); |
| 235 | |
| 236 | return tmp; |
| 237 | } |
| 238 | |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 239 | static void __fw_free_buf(struct kref *ref) |
| 240 | { |
| 241 | struct firmware_buf *buf = to_fwbuf(ref); |
| 242 | struct firmware_cache *fwc = buf->fwc; |
| 243 | int i; |
| 244 | |
| 245 | pr_debug("%s: fw-%s buf=%p data=%p size=%u\n", |
| 246 | __func__, buf->fw_id, buf, buf->data, |
| 247 | (unsigned int)buf->size); |
| 248 | |
| 249 | spin_lock(&fwc->lock); |
| 250 | list_del(&buf->list); |
| 251 | spin_unlock(&fwc->lock); |
| 252 | |
Ming Lei | 746058f | 2012-10-09 12:01:03 +0800 | [diff] [blame] | 253 | |
| 254 | if (buf->fmt == PAGE_BUF) { |
| 255 | vunmap(buf->data); |
| 256 | for (i = 0; i < buf->nr_pages; i++) |
| 257 | __free_page(buf->pages[i]); |
| 258 | kfree(buf->pages); |
| 259 | } else |
| 260 | vfree(buf->data); |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 261 | kfree(buf); |
| 262 | } |
| 263 | |
| 264 | static void fw_free_buf(struct firmware_buf *buf) |
| 265 | { |
| 266 | kref_put(&buf->ref, __fw_free_buf); |
| 267 | } |
| 268 | |
Ming Lei | 746058f | 2012-10-09 12:01:03 +0800 | [diff] [blame] | 269 | /* direct firmware loading support */ |
| 270 | static const char *fw_path[] = { |
| 271 | "/lib/firmware/updates/" UTS_RELEASE, |
| 272 | "/lib/firmware/updates", |
| 273 | "/lib/firmware/" UTS_RELEASE, |
| 274 | "/lib/firmware" |
| 275 | }; |
| 276 | |
| 277 | /* Don't inline this: 'struct kstat' is biggish */ |
| 278 | static noinline long fw_file_size(struct file *file) |
| 279 | { |
| 280 | struct kstat st; |
| 281 | if (vfs_getattr(file->f_path.mnt, file->f_path.dentry, &st)) |
| 282 | return -1; |
| 283 | if (!S_ISREG(st.mode)) |
| 284 | return -1; |
| 285 | if (st.size != (long)st.size) |
| 286 | return -1; |
| 287 | return st.size; |
| 288 | } |
| 289 | |
| 290 | static bool fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf) |
| 291 | { |
| 292 | long size; |
| 293 | char *buf; |
| 294 | |
| 295 | size = fw_file_size(file); |
| 296 | if (size < 0) |
| 297 | return false; |
| 298 | buf = vmalloc(size); |
| 299 | if (!buf) |
| 300 | return false; |
| 301 | if (kernel_read(file, 0, buf, size) != size) { |
| 302 | vfree(buf); |
| 303 | return false; |
| 304 | } |
| 305 | fw_buf->data = buf; |
| 306 | fw_buf->size = size; |
| 307 | return true; |
| 308 | } |
| 309 | |
| 310 | static bool fw_get_filesystem_firmware(struct firmware_buf *buf) |
| 311 | { |
| 312 | int i; |
| 313 | bool success = false; |
| 314 | char *path = __getname(); |
| 315 | |
| 316 | for (i = 0; i < ARRAY_SIZE(fw_path); i++) { |
| 317 | struct file *file; |
| 318 | snprintf(path, PATH_MAX, "%s/%s", fw_path[i], buf->fw_id); |
| 319 | |
| 320 | file = filp_open(path, O_RDONLY, 0); |
| 321 | if (IS_ERR(file)) |
| 322 | continue; |
| 323 | success = fw_read_file_contents(file, buf); |
| 324 | fput(file); |
| 325 | if (success) |
| 326 | break; |
| 327 | } |
| 328 | __putname(path); |
| 329 | return success; |
| 330 | } |
| 331 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 332 | static struct firmware_priv *to_firmware_priv(struct device *dev) |
| 333 | { |
| 334 | return container_of(dev, struct firmware_priv, dev); |
| 335 | } |
| 336 | |
| 337 | static void fw_load_abort(struct firmware_priv *fw_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | { |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 339 | struct firmware_buf *buf = fw_priv->buf; |
| 340 | |
| 341 | set_bit(FW_STATUS_ABORT, &buf->status); |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 342 | complete_all(&buf->completion); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 345 | static ssize_t firmware_timeout_show(struct class *class, |
| 346 | struct class_attribute *attr, |
| 347 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | { |
| 349 | return sprintf(buf, "%d\n", loading_timeout); |
| 350 | } |
| 351 | |
| 352 | /** |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 353 | * firmware_timeout_store - set number of seconds to wait for firmware |
| 354 | * @class: device class pointer |
Randy Dunlap | e59817b | 2010-03-10 11:47:58 -0800 | [diff] [blame] | 355 | * @attr: device attribute pointer |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 356 | * @buf: buffer to scan for timeout value |
| 357 | * @count: number of bytes in @buf |
| 358 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | * Sets the number of seconds to wait for the firmware. Once |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 360 | * this expires an error will be returned to the driver and no |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | * firmware will be provided. |
| 362 | * |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 363 | * Note: zero means 'wait forever'. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | **/ |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 365 | static ssize_t firmware_timeout_store(struct class *class, |
| 366 | struct class_attribute *attr, |
| 367 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | { |
| 369 | loading_timeout = simple_strtol(buf, NULL, 10); |
Stanislaw W. Gruszka | b92eac0 | 2005-06-28 20:44:51 -0700 | [diff] [blame] | 370 | if (loading_timeout < 0) |
| 371 | loading_timeout = 0; |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | return count; |
| 374 | } |
| 375 | |
Dmitry Torokhov | 673fae9 | 2010-03-13 23:49:13 -0800 | [diff] [blame] | 376 | static struct class_attribute firmware_class_attrs[] = { |
| 377 | __ATTR(timeout, S_IWUSR | S_IRUGO, |
| 378 | firmware_timeout_show, firmware_timeout_store), |
| 379 | __ATTR_NULL |
| 380 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
Dmitry Torokhov | 673fae9 | 2010-03-13 23:49:13 -0800 | [diff] [blame] | 382 | static void fw_dev_release(struct device *dev) |
| 383 | { |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 384 | struct firmware_priv *fw_priv = to_firmware_priv(dev); |
Ming Lei | 65710cb | 2012-08-04 12:01:16 +0800 | [diff] [blame] | 385 | |
Dmitry Torokhov | 673fae9 | 2010-03-13 23:49:13 -0800 | [diff] [blame] | 386 | kfree(fw_priv); |
Dmitry Torokhov | 673fae9 | 2010-03-13 23:49:13 -0800 | [diff] [blame] | 387 | |
| 388 | module_put(THIS_MODULE); |
| 389 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 391 | static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | { |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 393 | struct firmware_priv *fw_priv = to_firmware_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 395 | if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | return -ENOMEM; |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 397 | if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout)) |
kay.sievers@vrfy.org | 6897089 | 2005-04-18 21:57:31 -0700 | [diff] [blame] | 398 | return -ENOMEM; |
Johannes Berg | e9045f9 | 2010-03-29 17:57:20 +0200 | [diff] [blame] | 399 | if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait)) |
| 400 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | |
| 402 | return 0; |
| 403 | } |
| 404 | |
Adrian Bunk | 1b81d66 | 2006-05-20 15:00:16 -0700 | [diff] [blame] | 405 | static struct class firmware_class = { |
| 406 | .name = "firmware", |
Dmitry Torokhov | 673fae9 | 2010-03-13 23:49:13 -0800 | [diff] [blame] | 407 | .class_attrs = firmware_class_attrs, |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 408 | .dev_uevent = firmware_uevent, |
| 409 | .dev_release = fw_dev_release, |
Adrian Bunk | 1b81d66 | 2006-05-20 15:00:16 -0700 | [diff] [blame] | 410 | }; |
| 411 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 412 | static ssize_t firmware_loading_show(struct device *dev, |
| 413 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | { |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 415 | struct firmware_priv *fw_priv = to_firmware_priv(dev); |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 416 | int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status); |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 417 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | return sprintf(buf, "%d\n", loading); |
| 419 | } |
| 420 | |
Ming Lei | 65710cb | 2012-08-04 12:01:16 +0800 | [diff] [blame] | 421 | /* firmware holds the ownership of pages */ |
David Woodhouse | dd336c5 | 2010-05-02 11:21:21 +0300 | [diff] [blame] | 422 | static void firmware_free_data(const struct firmware *fw) |
| 423 | { |
Linus Torvalds | abb139e | 2012-10-03 15:58:32 -0700 | [diff] [blame] | 424 | /* Loaded directly? */ |
| 425 | if (!fw->priv) { |
| 426 | vfree(fw->data); |
| 427 | return; |
| 428 | } |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 429 | fw_free_buf(fw->priv); |
David Woodhouse | dd336c5 | 2010-05-02 11:21:21 +0300 | [diff] [blame] | 430 | } |
| 431 | |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 432 | /* Some architectures don't have PAGE_KERNEL_RO */ |
| 433 | #ifndef PAGE_KERNEL_RO |
| 434 | #define PAGE_KERNEL_RO PAGE_KERNEL |
| 435 | #endif |
Ming Lei | 253c924 | 2012-10-09 12:01:02 +0800 | [diff] [blame] | 436 | |
| 437 | /* one pages buffer should be mapped/unmapped only once */ |
| 438 | static int fw_map_pages_buf(struct firmware_buf *buf) |
| 439 | { |
Ming Lei | 746058f | 2012-10-09 12:01:03 +0800 | [diff] [blame] | 440 | if (buf->fmt != PAGE_BUF) |
| 441 | return 0; |
| 442 | |
Ming Lei | 253c924 | 2012-10-09 12:01:02 +0800 | [diff] [blame] | 443 | if (buf->data) |
| 444 | vunmap(buf->data); |
| 445 | buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO); |
| 446 | if (!buf->data) |
| 447 | return -ENOMEM; |
| 448 | return 0; |
| 449 | } |
| 450 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | /** |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 452 | * firmware_loading_store - set value in the 'loading' control file |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 453 | * @dev: device pointer |
Randy Dunlap | af9997e | 2006-12-22 01:06:52 -0800 | [diff] [blame] | 454 | * @attr: device attribute pointer |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 455 | * @buf: buffer to scan for loading control value |
| 456 | * @count: number of bytes in @buf |
| 457 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | * The relevant values are: |
| 459 | * |
| 460 | * 1: Start a load, discarding any previous partial load. |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 461 | * 0: Conclude the load and hand the data to the driver code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | * -1: Conclude the load with an error and discard any written data. |
| 463 | **/ |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 464 | static ssize_t firmware_loading_store(struct device *dev, |
| 465 | struct device_attribute *attr, |
| 466 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | { |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 468 | struct firmware_priv *fw_priv = to_firmware_priv(dev); |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 469 | struct firmware_buf *fw_buf = fw_priv->buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | int loading = simple_strtol(buf, NULL, 10); |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 471 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
Neil Horman | eea915b | 2012-01-02 15:31:23 -0500 | [diff] [blame] | 473 | mutex_lock(&fw_lock); |
| 474 | |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 475 | if (!fw_buf) |
Neil Horman | eea915b | 2012-01-02 15:31:23 -0500 | [diff] [blame] | 476 | goto out; |
| 477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | switch (loading) { |
| 479 | case 1: |
Ming Lei | 65710cb | 2012-08-04 12:01:16 +0800 | [diff] [blame] | 480 | /* discarding any previous partial load */ |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 481 | if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) { |
| 482 | for (i = 0; i < fw_buf->nr_pages; i++) |
| 483 | __free_page(fw_buf->pages[i]); |
| 484 | kfree(fw_buf->pages); |
| 485 | fw_buf->pages = NULL; |
| 486 | fw_buf->page_array_size = 0; |
| 487 | fw_buf->nr_pages = 0; |
| 488 | set_bit(FW_STATUS_LOADING, &fw_buf->status); |
Ming Lei | 28eefa7 | 2012-08-04 12:01:17 +0800 | [diff] [blame] | 489 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | break; |
| 491 | case 0: |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 492 | if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) { |
| 493 | set_bit(FW_STATUS_DONE, &fw_buf->status); |
| 494 | clear_bit(FW_STATUS_LOADING, &fw_buf->status); |
Ming Lei | 253c924 | 2012-10-09 12:01:02 +0800 | [diff] [blame] | 495 | |
| 496 | /* |
| 497 | * Several loading requests may be pending on |
| 498 | * one same firmware buf, so let all requests |
| 499 | * see the mapped 'buf->data' once the loading |
| 500 | * is completed. |
| 501 | * */ |
| 502 | fw_map_pages_buf(fw_buf); |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 503 | complete_all(&fw_buf->completion); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | break; |
| 505 | } |
| 506 | /* fallthrough */ |
| 507 | default: |
Bjorn Helgaas | 266a813 | 2008-10-15 22:04:20 -0700 | [diff] [blame] | 508 | dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | /* fallthrough */ |
| 510 | case -1: |
| 511 | fw_load_abort(fw_priv); |
| 512 | break; |
| 513 | } |
Neil Horman | eea915b | 2012-01-02 15:31:23 -0500 | [diff] [blame] | 514 | out: |
| 515 | mutex_unlock(&fw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | return count; |
| 517 | } |
| 518 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 519 | static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 521 | static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj, |
| 522 | struct bin_attribute *bin_attr, |
| 523 | char *buffer, loff_t offset, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | { |
Lars-Peter Clausen | b0d1f80 | 2012-07-03 18:49:36 +0200 | [diff] [blame] | 525 | struct device *dev = kobj_to_dev(kobj); |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 526 | struct firmware_priv *fw_priv = to_firmware_priv(dev); |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 527 | struct firmware_buf *buf; |
Akinobu Mita | f37e661 | 2008-07-25 01:48:23 -0700 | [diff] [blame] | 528 | ssize_t ret_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 530 | mutex_lock(&fw_lock); |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 531 | buf = fw_priv->buf; |
| 532 | if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | ret_count = -ENODEV; |
| 534 | goto out; |
| 535 | } |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 536 | if (offset > buf->size) { |
Jiri Slaby | 308975f | 2009-06-21 23:57:31 +0200 | [diff] [blame] | 537 | ret_count = 0; |
| 538 | goto out; |
| 539 | } |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 540 | if (count > buf->size - offset) |
| 541 | count = buf->size - offset; |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 542 | |
| 543 | ret_count = count; |
| 544 | |
| 545 | while (count) { |
| 546 | void *page_data; |
| 547 | int page_nr = offset >> PAGE_SHIFT; |
| 548 | int page_ofs = offset & (PAGE_SIZE-1); |
| 549 | int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count); |
| 550 | |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 551 | page_data = kmap(buf->pages[page_nr]); |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 552 | |
| 553 | memcpy(buffer, page_data + page_ofs, page_cnt); |
| 554 | |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 555 | kunmap(buf->pages[page_nr]); |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 556 | buffer += page_cnt; |
| 557 | offset += page_cnt; |
| 558 | count -= page_cnt; |
| 559 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | out: |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 561 | mutex_unlock(&fw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | return ret_count; |
| 563 | } |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 564 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 565 | static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | { |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 567 | struct firmware_buf *buf = fw_priv->buf; |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 568 | int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 570 | /* If the array of pages is too small, grow it... */ |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 571 | if (buf->page_array_size < pages_needed) { |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 572 | int new_array_size = max(pages_needed, |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 573 | buf->page_array_size * 2); |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 574 | struct page **new_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 576 | new_pages = kmalloc(new_array_size * sizeof(void *), |
| 577 | GFP_KERNEL); |
| 578 | if (!new_pages) { |
| 579 | fw_load_abort(fw_priv); |
| 580 | return -ENOMEM; |
| 581 | } |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 582 | memcpy(new_pages, buf->pages, |
| 583 | buf->page_array_size * sizeof(void *)); |
| 584 | memset(&new_pages[buf->page_array_size], 0, sizeof(void *) * |
| 585 | (new_array_size - buf->page_array_size)); |
| 586 | kfree(buf->pages); |
| 587 | buf->pages = new_pages; |
| 588 | buf->page_array_size = new_array_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | } |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 590 | |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 591 | while (buf->nr_pages < pages_needed) { |
| 592 | buf->pages[buf->nr_pages] = |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 593 | alloc_page(GFP_KERNEL | __GFP_HIGHMEM); |
| 594 | |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 595 | if (!buf->pages[buf->nr_pages]) { |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 596 | fw_load_abort(fw_priv); |
| 597 | return -ENOMEM; |
| 598 | } |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 599 | buf->nr_pages++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | /** |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 605 | * firmware_data_write - write method for firmware |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 606 | * @filp: open sysfs file |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 607 | * @kobj: kobject for the device |
Randy Dunlap | 42e61f4 | 2007-07-23 21:42:11 -0700 | [diff] [blame] | 608 | * @bin_attr: bin_attr structure |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 609 | * @buffer: buffer being written |
| 610 | * @offset: buffer offset for write in total data store area |
| 611 | * @count: buffer size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | * |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 613 | * Data written to the 'data' attribute will be later handed to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | * the driver as a firmware image. |
| 615 | **/ |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 616 | static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj, |
| 617 | struct bin_attribute *bin_attr, |
| 618 | char *buffer, loff_t offset, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | { |
Lars-Peter Clausen | b0d1f80 | 2012-07-03 18:49:36 +0200 | [diff] [blame] | 620 | struct device *dev = kobj_to_dev(kobj); |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 621 | struct firmware_priv *fw_priv = to_firmware_priv(dev); |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 622 | struct firmware_buf *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | ssize_t retval; |
| 624 | |
| 625 | if (!capable(CAP_SYS_RAWIO)) |
| 626 | return -EPERM; |
Stanislaw W. Gruszka | b92eac0 | 2005-06-28 20:44:51 -0700 | [diff] [blame] | 627 | |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 628 | mutex_lock(&fw_lock); |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 629 | buf = fw_priv->buf; |
| 630 | if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | retval = -ENODEV; |
| 632 | goto out; |
| 633 | } |
Ming Lei | 65710cb | 2012-08-04 12:01:16 +0800 | [diff] [blame] | 634 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | retval = fw_realloc_buffer(fw_priv, offset + count); |
| 636 | if (retval) |
| 637 | goto out; |
| 638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | retval = count; |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 640 | |
| 641 | while (count) { |
| 642 | void *page_data; |
| 643 | int page_nr = offset >> PAGE_SHIFT; |
| 644 | int page_ofs = offset & (PAGE_SIZE - 1); |
| 645 | int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count); |
| 646 | |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 647 | page_data = kmap(buf->pages[page_nr]); |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 648 | |
| 649 | memcpy(page_data + page_ofs, buffer, page_cnt); |
| 650 | |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 651 | kunmap(buf->pages[page_nr]); |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 652 | buffer += page_cnt; |
| 653 | offset += page_cnt; |
| 654 | count -= page_cnt; |
| 655 | } |
| 656 | |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 657 | buf->size = max_t(size_t, offset, buf->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | out: |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 659 | mutex_unlock(&fw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | return retval; |
| 661 | } |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 662 | |
Dmitry Torokhov | 0983ca2 | 2010-06-04 00:54:37 -0700 | [diff] [blame] | 663 | static struct bin_attribute firmware_attr_data = { |
| 664 | .attr = { .name = "data", .mode = 0644 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | .size = 0, |
| 666 | .read = firmware_data_read, |
| 667 | .write = firmware_data_write, |
| 668 | }; |
| 669 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 670 | static void firmware_class_timeout(u_long data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | { |
| 672 | struct firmware_priv *fw_priv = (struct firmware_priv *) data; |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 673 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | fw_load_abort(fw_priv); |
| 675 | } |
| 676 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 677 | static struct firmware_priv * |
Stephen Boyd | dddb554 | 2012-03-28 23:30:43 +0200 | [diff] [blame] | 678 | fw_create_instance(struct firmware *firmware, const char *fw_name, |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 679 | struct device *device, bool uevent, bool nowait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | { |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 681 | struct firmware_priv *fw_priv; |
| 682 | struct device *f_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 684 | fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL); |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 685 | if (!fw_priv) { |
Bjorn Helgaas | 266a813 | 2008-10-15 22:04:20 -0700 | [diff] [blame] | 686 | dev_err(device, "%s: kmalloc failed\n", __func__); |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 687 | fw_priv = ERR_PTR(-ENOMEM); |
| 688 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 691 | fw_priv->nowait = nowait; |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 692 | fw_priv->fw = firmware; |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 693 | setup_timer(&fw_priv->timeout, |
| 694 | firmware_class_timeout, (u_long) fw_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 696 | f_dev = &fw_priv->dev; |
| 697 | |
| 698 | device_initialize(f_dev); |
Ming Lei | 99c2aa7 | 2012-08-04 12:01:19 +0800 | [diff] [blame] | 699 | dev_set_name(f_dev, "%s", fw_name); |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 700 | f_dev->parent = device; |
| 701 | f_dev->class = &firmware_class; |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 702 | exit: |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 703 | return fw_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | } |
| 705 | |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 706 | /* store the pages buffer info firmware from buf */ |
| 707 | static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw) |
| 708 | { |
| 709 | fw->priv = buf; |
| 710 | fw->pages = buf->pages; |
| 711 | fw->size = buf->size; |
| 712 | fw->data = buf->data; |
| 713 | |
| 714 | pr_debug("%s: fw-%s buf=%p data=%p size=%u\n", |
| 715 | __func__, buf->fw_id, buf, buf->data, |
| 716 | (unsigned int)buf->size); |
| 717 | } |
| 718 | |
Ming Lei | cfe016b | 2012-09-08 17:32:30 +0800 | [diff] [blame] | 719 | #ifdef CONFIG_PM_SLEEP |
Ming Lei | f531f05a | 2012-08-04 12:01:25 +0800 | [diff] [blame] | 720 | static void fw_name_devm_release(struct device *dev, void *res) |
| 721 | { |
| 722 | struct fw_name_devm *fwn = res; |
| 723 | |
| 724 | if (fwn->magic == (unsigned long)&fw_cache) |
| 725 | pr_debug("%s: fw_name-%s devm-%p released\n", |
| 726 | __func__, fwn->name, res); |
| 727 | } |
| 728 | |
| 729 | static int fw_devm_match(struct device *dev, void *res, |
| 730 | void *match_data) |
| 731 | { |
| 732 | struct fw_name_devm *fwn = res; |
| 733 | |
| 734 | return (fwn->magic == (unsigned long)&fw_cache) && |
| 735 | !strcmp(fwn->name, match_data); |
| 736 | } |
| 737 | |
| 738 | static struct fw_name_devm *fw_find_devm_name(struct device *dev, |
| 739 | const char *name) |
| 740 | { |
| 741 | struct fw_name_devm *fwn; |
| 742 | |
| 743 | fwn = devres_find(dev, fw_name_devm_release, |
| 744 | fw_devm_match, (void *)name); |
| 745 | return fwn; |
| 746 | } |
| 747 | |
| 748 | /* add firmware name into devres list */ |
| 749 | static int fw_add_devm_name(struct device *dev, const char *name) |
| 750 | { |
| 751 | struct fw_name_devm *fwn; |
| 752 | |
| 753 | fwn = fw_find_devm_name(dev, name); |
| 754 | if (fwn) |
| 755 | return 1; |
| 756 | |
| 757 | fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) + |
| 758 | strlen(name) + 1, GFP_KERNEL); |
| 759 | if (!fwn) |
| 760 | return -ENOMEM; |
| 761 | |
| 762 | fwn->magic = (unsigned long)&fw_cache; |
| 763 | strcpy(fwn->name, name); |
| 764 | devres_add(dev, fwn); |
| 765 | |
| 766 | return 0; |
| 767 | } |
Ming Lei | cfe016b | 2012-09-08 17:32:30 +0800 | [diff] [blame] | 768 | #else |
| 769 | static int fw_add_devm_name(struct device *dev, const char *name) |
| 770 | { |
| 771 | return 0; |
| 772 | } |
| 773 | #endif |
Ming Lei | f531f05a | 2012-08-04 12:01:25 +0800 | [diff] [blame] | 774 | |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 775 | static void _request_firmware_cleanup(const struct firmware **firmware_p) |
| 776 | { |
| 777 | release_firmware(*firmware_p); |
| 778 | *firmware_p = NULL; |
| 779 | } |
| 780 | |
Stephen Boyd | dddb554 | 2012-03-28 23:30:43 +0200 | [diff] [blame] | 781 | static struct firmware_priv * |
| 782 | _request_firmware_prepare(const struct firmware **firmware_p, const char *name, |
| 783 | struct device *device, bool uevent, bool nowait) |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 784 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | struct firmware *firmware; |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 786 | struct firmware_priv *fw_priv = NULL; |
| 787 | struct firmware_buf *buf; |
| 788 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | |
| 790 | if (!firmware_p) |
Stephen Boyd | dddb554 | 2012-03-28 23:30:43 +0200 | [diff] [blame] | 791 | return ERR_PTR(-EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | |
Jiri Slaby | 4aed064 | 2005-09-13 01:25:01 -0700 | [diff] [blame] | 793 | *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | if (!firmware) { |
Bjorn Helgaas | 266a813 | 2008-10-15 22:04:20 -0700 | [diff] [blame] | 795 | dev_err(device, "%s: kmalloc(struct firmware) failed\n", |
| 796 | __func__); |
Stephen Boyd | dddb554 | 2012-03-28 23:30:43 +0200 | [diff] [blame] | 797 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | |
Dmitry Torokhov | bcb9bd1 | 2010-03-13 23:49:18 -0800 | [diff] [blame] | 800 | if (fw_get_builtin_firmware(firmware, name)) { |
Rafael J. Wysocki | 6f18ff9 | 2010-02-27 21:43:22 +0100 | [diff] [blame] | 801 | dev_dbg(device, "firmware: using built-in firmware %s\n", name); |
Stephen Boyd | dddb554 | 2012-03-28 23:30:43 +0200 | [diff] [blame] | 802 | return NULL; |
David Woodhouse | 5658c76 | 2008-05-23 13:52:42 +0100 | [diff] [blame] | 803 | } |
| 804 | |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 805 | ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf); |
| 806 | if (!ret) |
| 807 | fw_priv = fw_create_instance(firmware, name, device, |
| 808 | uevent, nowait); |
| 809 | |
| 810 | if (IS_ERR(fw_priv) || ret < 0) { |
| 811 | kfree(firmware); |
Stephen Boyd | dddb554 | 2012-03-28 23:30:43 +0200 | [diff] [blame] | 812 | *firmware_p = NULL; |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 813 | return ERR_PTR(-ENOMEM); |
| 814 | } else if (fw_priv) { |
| 815 | fw_priv->buf = buf; |
| 816 | |
| 817 | /* |
| 818 | * bind with 'buf' now to avoid warning in failure path |
| 819 | * of requesting firmware. |
| 820 | */ |
| 821 | firmware->priv = buf; |
| 822 | return fw_priv; |
Stephen Boyd | dddb554 | 2012-03-28 23:30:43 +0200 | [diff] [blame] | 823 | } |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 824 | |
| 825 | /* share the cached buf, which is inprogessing or completed */ |
| 826 | check_status: |
| 827 | mutex_lock(&fw_lock); |
| 828 | if (test_bit(FW_STATUS_ABORT, &buf->status)) { |
| 829 | fw_priv = ERR_PTR(-ENOENT); |
Ming Lei | ef40bb1 | 2012-08-20 19:04:15 +0800 | [diff] [blame] | 830 | firmware->priv = buf; |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 831 | _request_firmware_cleanup(firmware_p); |
| 832 | goto exit; |
| 833 | } else if (test_bit(FW_STATUS_DONE, &buf->status)) { |
| 834 | fw_priv = NULL; |
| 835 | fw_set_page_data(buf, firmware); |
| 836 | goto exit; |
| 837 | } |
| 838 | mutex_unlock(&fw_lock); |
| 839 | wait_for_completion(&buf->completion); |
| 840 | goto check_status; |
| 841 | |
| 842 | exit: |
| 843 | mutex_unlock(&fw_lock); |
Stephen Boyd | dddb554 | 2012-03-28 23:30:43 +0200 | [diff] [blame] | 844 | return fw_priv; |
Rafael J. Wysocki | 811fa40 | 2012-03-28 23:29:55 +0200 | [diff] [blame] | 845 | } |
| 846 | |
Stephen Boyd | dddb554 | 2012-03-28 23:30:43 +0200 | [diff] [blame] | 847 | static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent, |
| 848 | long timeout) |
Rafael J. Wysocki | 811fa40 | 2012-03-28 23:29:55 +0200 | [diff] [blame] | 849 | { |
Rafael J. Wysocki | 9b78c1d | 2012-03-28 23:30:02 +0200 | [diff] [blame] | 850 | int retval = 0; |
Stephen Boyd | dddb554 | 2012-03-28 23:30:43 +0200 | [diff] [blame] | 851 | struct device *f_dev = &fw_priv->dev; |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 852 | struct firmware_buf *buf = fw_priv->buf; |
Ming Lei | ac39b3e | 2012-08-20 19:04:16 +0800 | [diff] [blame] | 853 | struct firmware_cache *fwc = &fw_cache; |
Ming Lei | 746058f | 2012-10-09 12:01:03 +0800 | [diff] [blame] | 854 | int direct_load = 0; |
| 855 | |
| 856 | /* try direct loading from fs first */ |
| 857 | if (fw_get_filesystem_firmware(buf)) { |
| 858 | dev_dbg(f_dev->parent, "firmware: direct-loading" |
| 859 | " firmware %s\n", buf->fw_id); |
| 860 | |
| 861 | set_bit(FW_STATUS_DONE, &buf->status); |
| 862 | complete_all(&buf->completion); |
| 863 | direct_load = 1; |
| 864 | goto handle_fw; |
| 865 | } |
| 866 | |
| 867 | /* fall back on userspace loading */ |
| 868 | buf->fmt = PAGE_BUF; |
Linus Torvalds | caca951 | 2011-08-24 15:55:30 -0700 | [diff] [blame] | 869 | |
Stephen Boyd | dddb554 | 2012-03-28 23:30:43 +0200 | [diff] [blame] | 870 | dev_set_uevent_suppress(f_dev, true); |
David Woodhouse | 5658c76 | 2008-05-23 13:52:42 +0100 | [diff] [blame] | 871 | |
Stephen Boyd | dddb554 | 2012-03-28 23:30:43 +0200 | [diff] [blame] | 872 | /* Need to pin this module until class device is destroyed */ |
| 873 | __module_get(THIS_MODULE); |
| 874 | |
| 875 | retval = device_add(f_dev); |
| 876 | if (retval) { |
| 877 | dev_err(f_dev, "%s: device_register failed\n", __func__); |
| 878 | goto err_put_dev; |
| 879 | } |
| 880 | |
| 881 | retval = device_create_bin_file(f_dev, &firmware_attr_data); |
| 882 | if (retval) { |
| 883 | dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__); |
| 884 | goto err_del_dev; |
| 885 | } |
| 886 | |
| 887 | retval = device_create_file(f_dev, &dev_attr_loading); |
| 888 | if (retval) { |
| 889 | dev_err(f_dev, "%s: device_create_file failed\n", __func__); |
| 890 | goto err_del_bin_attr; |
| 891 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 893 | if (uevent) { |
Stephen Boyd | dddb554 | 2012-03-28 23:30:43 +0200 | [diff] [blame] | 894 | dev_set_uevent_suppress(f_dev, false); |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 895 | dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id); |
Rafael J. Wysocki | 9b78c1d | 2012-03-28 23:30:02 +0200 | [diff] [blame] | 896 | if (timeout != MAX_SCHEDULE_TIMEOUT) |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 897 | mod_timer(&fw_priv->timeout, |
Rafael J. Wysocki | 9b78c1d | 2012-03-28 23:30:02 +0200 | [diff] [blame] | 898 | round_jiffies_up(jiffies + timeout)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 900 | kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD); |
| 901 | } |
| 902 | |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 903 | wait_for_completion(&buf->completion); |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 904 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 905 | del_timer_sync(&fw_priv->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | |
Ming Lei | 746058f | 2012-10-09 12:01:03 +0800 | [diff] [blame] | 907 | handle_fw: |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 908 | mutex_lock(&fw_lock); |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 909 | if (!buf->size || test_bit(FW_STATUS_ABORT, &buf->status)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | retval = -ENOENT; |
Ming Lei | 65710cb | 2012-08-04 12:01:16 +0800 | [diff] [blame] | 911 | |
Ming Lei | f531f05a | 2012-08-04 12:01:25 +0800 | [diff] [blame] | 912 | /* |
| 913 | * add firmware name into devres list so that we can auto cache |
| 914 | * and uncache firmware for device. |
| 915 | * |
| 916 | * f_dev->parent may has been deleted already, but the problem |
| 917 | * should be fixed in devres or driver core. |
| 918 | */ |
| 919 | if (!retval && f_dev->parent) |
| 920 | fw_add_devm_name(f_dev->parent, buf->fw_id); |
| 921 | |
Ming Lei | ac39b3e | 2012-08-20 19:04:16 +0800 | [diff] [blame] | 922 | /* |
| 923 | * After caching firmware image is started, let it piggyback |
| 924 | * on request firmware. |
| 925 | */ |
| 926 | if (!retval && fwc->state == FW_LOADER_START_CACHE) { |
| 927 | if (fw_cache_piggyback_on_request(buf->fw_id)) |
| 928 | kref_get(&buf->ref); |
| 929 | } |
| 930 | |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 931 | /* pass the pages buffer to driver at the last minute */ |
| 932 | fw_set_page_data(buf, fw_priv->fw); |
| 933 | |
Ming Lei | 1244691 | 2012-08-04 12:01:20 +0800 | [diff] [blame] | 934 | fw_priv->buf = NULL; |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 935 | mutex_unlock(&fw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | |
Ming Lei | 746058f | 2012-10-09 12:01:03 +0800 | [diff] [blame] | 937 | if (direct_load) |
| 938 | goto err_put_dev; |
| 939 | |
Stephen Boyd | dddb554 | 2012-03-28 23:30:43 +0200 | [diff] [blame] | 940 | device_remove_file(f_dev, &dev_attr_loading); |
| 941 | err_del_bin_attr: |
| 942 | device_remove_bin_file(f_dev, &firmware_attr_data); |
| 943 | err_del_dev: |
| 944 | device_del(f_dev); |
| 945 | err_put_dev: |
| 946 | put_device(f_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | return retval; |
| 948 | } |
| 949 | |
| 950 | /** |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 951 | * request_firmware: - send firmware request and wait for it |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 952 | * @firmware_p: pointer to firmware image |
| 953 | * @name: name of firmware file |
| 954 | * @device: device for which firmware is being loaded |
| 955 | * |
| 956 | * @firmware_p will be used to return a firmware image by the name |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 957 | * of @name for device @device. |
| 958 | * |
| 959 | * Should be called from user context where sleeping is allowed. |
| 960 | * |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 961 | * @name will be used as $FIRMWARE in the uevent environment and |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 962 | * should be distinctive enough not to be confused with any other |
| 963 | * firmware image for this or any other device. |
Ming Lei | 0cfc1e1 | 2012-08-04 12:01:23 +0800 | [diff] [blame] | 964 | * |
| 965 | * Caller must hold the reference count of @device. |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 966 | **/ |
| 967 | int |
| 968 | request_firmware(const struct firmware **firmware_p, const char *name, |
| 969 | struct device *device) |
| 970 | { |
Stephen Boyd | dddb554 | 2012-03-28 23:30:43 +0200 | [diff] [blame] | 971 | struct firmware_priv *fw_priv; |
Rafael J. Wysocki | 811fa40 | 2012-03-28 23:29:55 +0200 | [diff] [blame] | 972 | int ret; |
| 973 | |
Stephen Boyd | dddb554 | 2012-03-28 23:30:43 +0200 | [diff] [blame] | 974 | fw_priv = _request_firmware_prepare(firmware_p, name, device, true, |
| 975 | false); |
| 976 | if (IS_ERR_OR_NULL(fw_priv)) |
| 977 | return PTR_RET(fw_priv); |
Rafael J. Wysocki | 811fa40 | 2012-03-28 23:29:55 +0200 | [diff] [blame] | 978 | |
Rafael J. Wysocki | 9b78c1d | 2012-03-28 23:30:02 +0200 | [diff] [blame] | 979 | ret = usermodehelper_read_trylock(); |
| 980 | if (WARN_ON(ret)) { |
| 981 | dev_err(device, "firmware: %s will not be loaded\n", name); |
| 982 | } else { |
Stephen Boyd | dddb554 | 2012-03-28 23:30:43 +0200 | [diff] [blame] | 983 | ret = _request_firmware_load(fw_priv, true, |
Rafael J. Wysocki | 9b78c1d | 2012-03-28 23:30:02 +0200 | [diff] [blame] | 984 | firmware_loading_timeout()); |
| 985 | usermodehelper_read_unlock(); |
| 986 | } |
Rafael J. Wysocki | 811fa40 | 2012-03-28 23:29:55 +0200 | [diff] [blame] | 987 | if (ret) |
| 988 | _request_firmware_cleanup(firmware_p); |
| 989 | |
| 990 | return ret; |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | * release_firmware: - release the resource associated with a firmware image |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 995 | * @fw: firmware resource to release |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | **/ |
Dmitry Torokhov | bcb9bd1 | 2010-03-13 23:49:18 -0800 | [diff] [blame] | 997 | void release_firmware(const struct firmware *fw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | { |
| 999 | if (fw) { |
Dmitry Torokhov | bcb9bd1 | 2010-03-13 23:49:18 -0800 | [diff] [blame] | 1000 | if (!fw_is_builtin_firmware(fw)) |
| 1001 | firmware_free_data(fw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | kfree(fw); |
| 1003 | } |
| 1004 | } |
| 1005 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | /* Async support */ |
| 1007 | struct firmware_work { |
| 1008 | struct work_struct work; |
| 1009 | struct module *module; |
| 1010 | const char *name; |
| 1011 | struct device *device; |
| 1012 | void *context; |
| 1013 | void (*cont)(const struct firmware *fw, void *context); |
Bob Liu | 072fc8f | 2011-01-26 18:33:32 +0800 | [diff] [blame] | 1014 | bool uevent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | }; |
| 1016 | |
Stephen Boyd | a36cf84 | 2012-03-28 23:31:00 +0200 | [diff] [blame] | 1017 | static void request_firmware_work_func(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | { |
Stephen Boyd | a36cf84 | 2012-03-28 23:31:00 +0200 | [diff] [blame] | 1019 | struct firmware_work *fw_work; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | const struct firmware *fw; |
Stephen Boyd | dddb554 | 2012-03-28 23:30:43 +0200 | [diff] [blame] | 1021 | struct firmware_priv *fw_priv; |
Rafael J. Wysocki | 9b78c1d | 2012-03-28 23:30:02 +0200 | [diff] [blame] | 1022 | long timeout; |
matthieu castet | 113fab1 | 2005-11-13 16:07:39 -0800 | [diff] [blame] | 1023 | int ret; |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 1024 | |
Stephen Boyd | a36cf84 | 2012-03-28 23:31:00 +0200 | [diff] [blame] | 1025 | fw_work = container_of(work, struct firmware_work, work); |
Stephen Boyd | dddb554 | 2012-03-28 23:30:43 +0200 | [diff] [blame] | 1026 | fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device, |
| 1027 | fw_work->uevent, true); |
| 1028 | if (IS_ERR_OR_NULL(fw_priv)) { |
| 1029 | ret = PTR_RET(fw_priv); |
Rafael J. Wysocki | 811fa40 | 2012-03-28 23:29:55 +0200 | [diff] [blame] | 1030 | goto out; |
Stephen Boyd | dddb554 | 2012-03-28 23:30:43 +0200 | [diff] [blame] | 1031 | } |
Rafael J. Wysocki | 811fa40 | 2012-03-28 23:29:55 +0200 | [diff] [blame] | 1032 | |
Rafael J. Wysocki | 9b78c1d | 2012-03-28 23:30:02 +0200 | [diff] [blame] | 1033 | timeout = usermodehelper_read_lock_wait(firmware_loading_timeout()); |
| 1034 | if (timeout) { |
Stephen Boyd | dddb554 | 2012-03-28 23:30:43 +0200 | [diff] [blame] | 1035 | ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout); |
Rafael J. Wysocki | 9b78c1d | 2012-03-28 23:30:02 +0200 | [diff] [blame] | 1036 | usermodehelper_read_unlock(); |
| 1037 | } else { |
| 1038 | dev_dbg(fw_work->device, "firmware: %s loading timed out\n", |
| 1039 | fw_work->name); |
| 1040 | ret = -EAGAIN; |
| 1041 | } |
Rafael J. Wysocki | 811fa40 | 2012-03-28 23:29:55 +0200 | [diff] [blame] | 1042 | if (ret) |
| 1043 | _request_firmware_cleanup(&fw); |
| 1044 | |
| 1045 | out: |
Johannes Berg | 9ebfbd4 | 2009-10-29 12:36:02 +0100 | [diff] [blame] | 1046 | fw_work->cont(fw, fw_work->context); |
Ming Lei | 0cfc1e1 | 2012-08-04 12:01:23 +0800 | [diff] [blame] | 1047 | put_device(fw_work->device); |
Johannes Berg | 9ebfbd4 | 2009-10-29 12:36:02 +0100 | [diff] [blame] | 1048 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | module_put(fw_work->module); |
| 1050 | kfree(fw_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | /** |
Ben Hutchings | 3c31f07 | 2010-02-14 14:18:53 +0000 | [diff] [blame] | 1054 | * request_firmware_nowait - asynchronous version of request_firmware |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 1055 | * @module: module requesting the firmware |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 1056 | * @uevent: sends uevent to copy the firmware image if this flag |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 1057 | * is non-zero else the firmware copy must be done manually. |
| 1058 | * @name: name of firmware file |
| 1059 | * @device: device for which firmware is being loaded |
Johannes Berg | 9ebfbd4 | 2009-10-29 12:36:02 +0100 | [diff] [blame] | 1060 | * @gfp: allocation flags |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 1061 | * @context: will be passed over to @cont, and |
| 1062 | * @fw may be %NULL if firmware request fails. |
| 1063 | * @cont: function will be called asynchronously when the firmware |
| 1064 | * request is over. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | * |
Ming Lei | 0cfc1e1 | 2012-08-04 12:01:23 +0800 | [diff] [blame] | 1066 | * Caller must hold the reference count of @device. |
| 1067 | * |
Ming Lei | 6f21a62 | 2012-08-04 12:01:24 +0800 | [diff] [blame] | 1068 | * Asynchronous variant of request_firmware() for user contexts: |
| 1069 | * - sleep for as small periods as possible since it may |
| 1070 | * increase kernel boot time of built-in device drivers |
| 1071 | * requesting firmware in their ->probe() methods, if |
| 1072 | * @gfp is GFP_KERNEL. |
| 1073 | * |
| 1074 | * - can't sleep at all if @gfp is GFP_ATOMIC. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | **/ |
| 1076 | int |
| 1077 | request_firmware_nowait( |
Bob Liu | 072fc8f | 2011-01-26 18:33:32 +0800 | [diff] [blame] | 1078 | struct module *module, bool uevent, |
Johannes Berg | 9ebfbd4 | 2009-10-29 12:36:02 +0100 | [diff] [blame] | 1079 | const char *name, struct device *device, gfp_t gfp, void *context, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | void (*cont)(const struct firmware *fw, void *context)) |
| 1081 | { |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 1082 | struct firmware_work *fw_work; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 1084 | fw_work = kzalloc(sizeof (struct firmware_work), gfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | if (!fw_work) |
| 1086 | return -ENOMEM; |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 1087 | |
| 1088 | fw_work->module = module; |
| 1089 | fw_work->name = name; |
| 1090 | fw_work->device = device; |
| 1091 | fw_work->context = context; |
| 1092 | fw_work->cont = cont; |
| 1093 | fw_work->uevent = uevent; |
| 1094 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | if (!try_module_get(module)) { |
| 1096 | kfree(fw_work); |
| 1097 | return -EFAULT; |
| 1098 | } |
| 1099 | |
Ming Lei | 0cfc1e1 | 2012-08-04 12:01:23 +0800 | [diff] [blame] | 1100 | get_device(fw_work->device); |
Stephen Boyd | a36cf84 | 2012-03-28 23:31:00 +0200 | [diff] [blame] | 1101 | INIT_WORK(&fw_work->work, request_firmware_work_func); |
| 1102 | schedule_work(&fw_work->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | return 0; |
| 1104 | } |
| 1105 | |
Ming Lei | 2887b39 | 2012-08-04 12:01:22 +0800 | [diff] [blame] | 1106 | /** |
| 1107 | * cache_firmware - cache one firmware image in kernel memory space |
| 1108 | * @fw_name: the firmware image name |
| 1109 | * |
| 1110 | * Cache firmware in kernel memory so that drivers can use it when |
| 1111 | * system isn't ready for them to request firmware image from userspace. |
| 1112 | * Once it returns successfully, driver can use request_firmware or its |
| 1113 | * nowait version to get the cached firmware without any interacting |
| 1114 | * with userspace |
| 1115 | * |
| 1116 | * Return 0 if the firmware image has been cached successfully |
| 1117 | * Return !0 otherwise |
| 1118 | * |
| 1119 | */ |
| 1120 | int cache_firmware(const char *fw_name) |
| 1121 | { |
| 1122 | int ret; |
| 1123 | const struct firmware *fw; |
| 1124 | |
| 1125 | pr_debug("%s: %s\n", __func__, fw_name); |
| 1126 | |
| 1127 | ret = request_firmware(&fw, fw_name, NULL); |
| 1128 | if (!ret) |
| 1129 | kfree(fw); |
| 1130 | |
| 1131 | pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret); |
| 1132 | |
| 1133 | return ret; |
| 1134 | } |
| 1135 | |
| 1136 | /** |
| 1137 | * uncache_firmware - remove one cached firmware image |
| 1138 | * @fw_name: the firmware image name |
| 1139 | * |
| 1140 | * Uncache one firmware image which has been cached successfully |
| 1141 | * before. |
| 1142 | * |
| 1143 | * Return 0 if the firmware cache has been removed successfully |
| 1144 | * Return !0 otherwise |
| 1145 | * |
| 1146 | */ |
| 1147 | int uncache_firmware(const char *fw_name) |
| 1148 | { |
| 1149 | struct firmware_buf *buf; |
| 1150 | struct firmware fw; |
| 1151 | |
| 1152 | pr_debug("%s: %s\n", __func__, fw_name); |
| 1153 | |
| 1154 | if (fw_get_builtin_firmware(&fw, fw_name)) |
| 1155 | return 0; |
| 1156 | |
| 1157 | buf = fw_lookup_buf(fw_name); |
| 1158 | if (buf) { |
| 1159 | fw_free_buf(buf); |
| 1160 | return 0; |
| 1161 | } |
| 1162 | |
| 1163 | return -EINVAL; |
| 1164 | } |
| 1165 | |
Ming Lei | cfe016b | 2012-09-08 17:32:30 +0800 | [diff] [blame] | 1166 | #ifdef CONFIG_PM_SLEEP |
Ming Lei | d28d388 | 2012-10-09 12:01:04 +0800 | [diff] [blame] | 1167 | static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain); |
| 1168 | |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1169 | static struct fw_cache_entry *alloc_fw_cache_entry(const char *name) |
| 1170 | { |
| 1171 | struct fw_cache_entry *fce; |
| 1172 | |
| 1173 | fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC); |
| 1174 | if (!fce) |
| 1175 | goto exit; |
| 1176 | |
| 1177 | strcpy(fce->name, name); |
| 1178 | exit: |
| 1179 | return fce; |
| 1180 | } |
| 1181 | |
Ming Lei | 373304f | 2012-10-09 12:01:01 +0800 | [diff] [blame] | 1182 | static int __fw_entry_found(const char *name) |
| 1183 | { |
| 1184 | struct firmware_cache *fwc = &fw_cache; |
| 1185 | struct fw_cache_entry *fce; |
| 1186 | |
| 1187 | list_for_each_entry(fce, &fwc->fw_names, list) { |
| 1188 | if (!strcmp(fce->name, name)) |
| 1189 | return 1; |
| 1190 | } |
| 1191 | return 0; |
| 1192 | } |
| 1193 | |
Ming Lei | ac39b3e | 2012-08-20 19:04:16 +0800 | [diff] [blame] | 1194 | static int fw_cache_piggyback_on_request(const char *name) |
| 1195 | { |
| 1196 | struct firmware_cache *fwc = &fw_cache; |
| 1197 | struct fw_cache_entry *fce; |
| 1198 | int ret = 0; |
| 1199 | |
| 1200 | spin_lock(&fwc->name_lock); |
Ming Lei | 373304f | 2012-10-09 12:01:01 +0800 | [diff] [blame] | 1201 | if (__fw_entry_found(name)) |
| 1202 | goto found; |
Ming Lei | ac39b3e | 2012-08-20 19:04:16 +0800 | [diff] [blame] | 1203 | |
| 1204 | fce = alloc_fw_cache_entry(name); |
| 1205 | if (fce) { |
| 1206 | ret = 1; |
| 1207 | list_add(&fce->list, &fwc->fw_names); |
| 1208 | pr_debug("%s: fw: %s\n", __func__, name); |
| 1209 | } |
| 1210 | found: |
| 1211 | spin_unlock(&fwc->name_lock); |
| 1212 | return ret; |
| 1213 | } |
| 1214 | |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1215 | static void free_fw_cache_entry(struct fw_cache_entry *fce) |
| 1216 | { |
| 1217 | kfree(fce); |
| 1218 | } |
| 1219 | |
| 1220 | static void __async_dev_cache_fw_image(void *fw_entry, |
| 1221 | async_cookie_t cookie) |
| 1222 | { |
| 1223 | struct fw_cache_entry *fce = fw_entry; |
| 1224 | struct firmware_cache *fwc = &fw_cache; |
| 1225 | int ret; |
| 1226 | |
| 1227 | ret = cache_firmware(fce->name); |
Ming Lei | ac39b3e | 2012-08-20 19:04:16 +0800 | [diff] [blame] | 1228 | if (ret) { |
| 1229 | spin_lock(&fwc->name_lock); |
| 1230 | list_del(&fce->list); |
| 1231 | spin_unlock(&fwc->name_lock); |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1232 | |
Ming Lei | ac39b3e | 2012-08-20 19:04:16 +0800 | [diff] [blame] | 1233 | free_fw_cache_entry(fce); |
| 1234 | } |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | /* called with dev->devres_lock held */ |
| 1238 | static void dev_create_fw_entry(struct device *dev, void *res, |
| 1239 | void *data) |
| 1240 | { |
| 1241 | struct fw_name_devm *fwn = res; |
| 1242 | const char *fw_name = fwn->name; |
| 1243 | struct list_head *head = data; |
| 1244 | struct fw_cache_entry *fce; |
| 1245 | |
| 1246 | fce = alloc_fw_cache_entry(fw_name); |
| 1247 | if (fce) |
| 1248 | list_add(&fce->list, head); |
| 1249 | } |
| 1250 | |
| 1251 | static int devm_name_match(struct device *dev, void *res, |
| 1252 | void *match_data) |
| 1253 | { |
| 1254 | struct fw_name_devm *fwn = res; |
| 1255 | return (fwn->magic == (unsigned long)match_data); |
| 1256 | } |
| 1257 | |
Ming Lei | ab6dd8e | 2012-08-17 22:07:00 +0800 | [diff] [blame] | 1258 | static void dev_cache_fw_image(struct device *dev, void *data) |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1259 | { |
| 1260 | LIST_HEAD(todo); |
| 1261 | struct fw_cache_entry *fce; |
| 1262 | struct fw_cache_entry *fce_next; |
| 1263 | struct firmware_cache *fwc = &fw_cache; |
| 1264 | |
| 1265 | devres_for_each_res(dev, fw_name_devm_release, |
| 1266 | devm_name_match, &fw_cache, |
| 1267 | dev_create_fw_entry, &todo); |
| 1268 | |
| 1269 | list_for_each_entry_safe(fce, fce_next, &todo, list) { |
| 1270 | list_del(&fce->list); |
| 1271 | |
| 1272 | spin_lock(&fwc->name_lock); |
Ming Lei | 373304f | 2012-10-09 12:01:01 +0800 | [diff] [blame] | 1273 | /* only one cache entry for one firmware */ |
| 1274 | if (!__fw_entry_found(fce->name)) { |
Ming Lei | 373304f | 2012-10-09 12:01:01 +0800 | [diff] [blame] | 1275 | list_add(&fce->list, &fwc->fw_names); |
| 1276 | } else { |
| 1277 | free_fw_cache_entry(fce); |
| 1278 | fce = NULL; |
| 1279 | } |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1280 | spin_unlock(&fwc->name_lock); |
| 1281 | |
Ming Lei | 373304f | 2012-10-09 12:01:01 +0800 | [diff] [blame] | 1282 | if (fce) |
Ming Lei | d28d388 | 2012-10-09 12:01:04 +0800 | [diff] [blame] | 1283 | async_schedule_domain(__async_dev_cache_fw_image, |
| 1284 | (void *)fce, |
| 1285 | &fw_cache_domain); |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | static void __device_uncache_fw_images(void) |
| 1290 | { |
| 1291 | struct firmware_cache *fwc = &fw_cache; |
| 1292 | struct fw_cache_entry *fce; |
| 1293 | |
| 1294 | spin_lock(&fwc->name_lock); |
| 1295 | while (!list_empty(&fwc->fw_names)) { |
| 1296 | fce = list_entry(fwc->fw_names.next, |
| 1297 | struct fw_cache_entry, list); |
| 1298 | list_del(&fce->list); |
| 1299 | spin_unlock(&fwc->name_lock); |
| 1300 | |
| 1301 | uncache_firmware(fce->name); |
| 1302 | free_fw_cache_entry(fce); |
| 1303 | |
| 1304 | spin_lock(&fwc->name_lock); |
| 1305 | } |
| 1306 | spin_unlock(&fwc->name_lock); |
| 1307 | } |
| 1308 | |
| 1309 | /** |
| 1310 | * device_cache_fw_images - cache devices' firmware |
| 1311 | * |
| 1312 | * If one device called request_firmware or its nowait version |
| 1313 | * successfully before, the firmware names are recored into the |
| 1314 | * device's devres link list, so device_cache_fw_images can call |
| 1315 | * cache_firmware() to cache these firmwares for the device, |
| 1316 | * then the device driver can load its firmwares easily at |
| 1317 | * time when system is not ready to complete loading firmware. |
| 1318 | */ |
| 1319 | static void device_cache_fw_images(void) |
| 1320 | { |
| 1321 | struct firmware_cache *fwc = &fw_cache; |
Ming Lei | ffe53f6 | 2012-08-04 12:01:28 +0800 | [diff] [blame] | 1322 | int old_timeout; |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1323 | DEFINE_WAIT(wait); |
| 1324 | |
| 1325 | pr_debug("%s\n", __func__); |
| 1326 | |
Ming Lei | 373304f | 2012-10-09 12:01:01 +0800 | [diff] [blame] | 1327 | /* cancel uncache work */ |
| 1328 | cancel_delayed_work_sync(&fwc->work); |
| 1329 | |
Ming Lei | ffe53f6 | 2012-08-04 12:01:28 +0800 | [diff] [blame] | 1330 | /* |
| 1331 | * use small loading timeout for caching devices' firmware |
| 1332 | * because all these firmware images have been loaded |
| 1333 | * successfully at lease once, also system is ready for |
| 1334 | * completing firmware loading now. The maximum size of |
| 1335 | * firmware in current distributions is about 2M bytes, |
| 1336 | * so 10 secs should be enough. |
| 1337 | */ |
| 1338 | old_timeout = loading_timeout; |
| 1339 | loading_timeout = 10; |
| 1340 | |
Ming Lei | ac39b3e | 2012-08-20 19:04:16 +0800 | [diff] [blame] | 1341 | mutex_lock(&fw_lock); |
| 1342 | fwc->state = FW_LOADER_START_CACHE; |
Ming Lei | ab6dd8e | 2012-08-17 22:07:00 +0800 | [diff] [blame] | 1343 | dpm_for_each_dev(NULL, dev_cache_fw_image); |
Ming Lei | ac39b3e | 2012-08-20 19:04:16 +0800 | [diff] [blame] | 1344 | mutex_unlock(&fw_lock); |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1345 | |
| 1346 | /* wait for completion of caching firmware for all devices */ |
Ming Lei | d28d388 | 2012-10-09 12:01:04 +0800 | [diff] [blame] | 1347 | async_synchronize_full_domain(&fw_cache_domain); |
Ming Lei | ffe53f6 | 2012-08-04 12:01:28 +0800 | [diff] [blame] | 1348 | |
| 1349 | loading_timeout = old_timeout; |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | /** |
| 1353 | * device_uncache_fw_images - uncache devices' firmware |
| 1354 | * |
| 1355 | * uncache all firmwares which have been cached successfully |
| 1356 | * by device_uncache_fw_images earlier |
| 1357 | */ |
| 1358 | static void device_uncache_fw_images(void) |
| 1359 | { |
| 1360 | pr_debug("%s\n", __func__); |
| 1361 | __device_uncache_fw_images(); |
| 1362 | } |
| 1363 | |
| 1364 | static void device_uncache_fw_images_work(struct work_struct *work) |
| 1365 | { |
| 1366 | device_uncache_fw_images(); |
| 1367 | } |
| 1368 | |
| 1369 | /** |
| 1370 | * device_uncache_fw_images_delay - uncache devices firmwares |
| 1371 | * @delay: number of milliseconds to delay uncache device firmwares |
| 1372 | * |
| 1373 | * uncache all devices's firmwares which has been cached successfully |
| 1374 | * by device_cache_fw_images after @delay milliseconds. |
| 1375 | */ |
| 1376 | static void device_uncache_fw_images_delay(unsigned long delay) |
| 1377 | { |
| 1378 | schedule_delayed_work(&fw_cache.work, |
| 1379 | msecs_to_jiffies(delay)); |
| 1380 | } |
| 1381 | |
Ming Lei | 07646d9 | 2012-08-04 12:01:29 +0800 | [diff] [blame] | 1382 | static int fw_pm_notify(struct notifier_block *notify_block, |
| 1383 | unsigned long mode, void *unused) |
| 1384 | { |
| 1385 | switch (mode) { |
| 1386 | case PM_HIBERNATION_PREPARE: |
| 1387 | case PM_SUSPEND_PREPARE: |
| 1388 | device_cache_fw_images(); |
| 1389 | break; |
| 1390 | |
| 1391 | case PM_POST_SUSPEND: |
| 1392 | case PM_POST_HIBERNATION: |
| 1393 | case PM_POST_RESTORE: |
Ming Lei | ac39b3e | 2012-08-20 19:04:16 +0800 | [diff] [blame] | 1394 | /* |
| 1395 | * In case that system sleep failed and syscore_suspend is |
| 1396 | * not called. |
| 1397 | */ |
| 1398 | mutex_lock(&fw_lock); |
| 1399 | fw_cache.state = FW_LOADER_NO_CACHE; |
| 1400 | mutex_unlock(&fw_lock); |
| 1401 | |
Ming Lei | 07646d9 | 2012-08-04 12:01:29 +0800 | [diff] [blame] | 1402 | device_uncache_fw_images_delay(10 * MSEC_PER_SEC); |
| 1403 | break; |
| 1404 | } |
| 1405 | |
| 1406 | return 0; |
| 1407 | } |
Ming Lei | 07646d9 | 2012-08-04 12:01:29 +0800 | [diff] [blame] | 1408 | |
Ming Lei | ac39b3e | 2012-08-20 19:04:16 +0800 | [diff] [blame] | 1409 | /* stop caching firmware once syscore_suspend is reached */ |
| 1410 | static int fw_suspend(void) |
| 1411 | { |
| 1412 | fw_cache.state = FW_LOADER_NO_CACHE; |
| 1413 | return 0; |
| 1414 | } |
| 1415 | |
| 1416 | static struct syscore_ops fw_syscore_ops = { |
| 1417 | .suspend = fw_suspend, |
| 1418 | }; |
Ming Lei | cfe016b | 2012-09-08 17:32:30 +0800 | [diff] [blame] | 1419 | #else |
| 1420 | static int fw_cache_piggyback_on_request(const char *name) |
| 1421 | { |
| 1422 | return 0; |
| 1423 | } |
| 1424 | #endif |
Ming Lei | ac39b3e | 2012-08-20 19:04:16 +0800 | [diff] [blame] | 1425 | |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1426 | static void __init fw_cache_init(void) |
| 1427 | { |
| 1428 | spin_lock_init(&fw_cache.lock); |
| 1429 | INIT_LIST_HEAD(&fw_cache.head); |
Ming Lei | cfe016b | 2012-09-08 17:32:30 +0800 | [diff] [blame] | 1430 | fw_cache.state = FW_LOADER_NO_CACHE; |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1431 | |
Ming Lei | cfe016b | 2012-09-08 17:32:30 +0800 | [diff] [blame] | 1432 | #ifdef CONFIG_PM_SLEEP |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1433 | spin_lock_init(&fw_cache.name_lock); |
| 1434 | INIT_LIST_HEAD(&fw_cache.fw_names); |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1435 | |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1436 | INIT_DELAYED_WORK(&fw_cache.work, |
| 1437 | device_uncache_fw_images_work); |
Ming Lei | 07646d9 | 2012-08-04 12:01:29 +0800 | [diff] [blame] | 1438 | |
| 1439 | fw_cache.pm_notify.notifier_call = fw_pm_notify; |
| 1440 | register_pm_notifier(&fw_cache.pm_notify); |
Ming Lei | ac39b3e | 2012-08-20 19:04:16 +0800 | [diff] [blame] | 1441 | |
| 1442 | register_syscore_ops(&fw_syscore_ops); |
Ming Lei | cfe016b | 2012-09-08 17:32:30 +0800 | [diff] [blame] | 1443 | #endif |
Ming Lei | 37276a5 | 2012-08-04 12:01:27 +0800 | [diff] [blame] | 1444 | } |
| 1445 | |
Dmitry Torokhov | 673fae9 | 2010-03-13 23:49:13 -0800 | [diff] [blame] | 1446 | static int __init firmware_class_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | { |
Ming Lei | 1f2b795 | 2012-08-04 12:01:21 +0800 | [diff] [blame] | 1448 | fw_cache_init(); |
Dmitry Torokhov | 673fae9 | 2010-03-13 23:49:13 -0800 | [diff] [blame] | 1449 | return class_register(&firmware_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | } |
Dmitry Torokhov | 673fae9 | 2010-03-13 23:49:13 -0800 | [diff] [blame] | 1451 | |
| 1452 | static void __exit firmware_class_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | { |
Ming Lei | cfe016b | 2012-09-08 17:32:30 +0800 | [diff] [blame] | 1454 | #ifdef CONFIG_PM_SLEEP |
Ming Lei | ac39b3e | 2012-08-20 19:04:16 +0800 | [diff] [blame] | 1455 | unregister_syscore_ops(&fw_syscore_ops); |
Ming Lei | 07646d9 | 2012-08-04 12:01:29 +0800 | [diff] [blame] | 1456 | unregister_pm_notifier(&fw_cache.pm_notify); |
Ming Lei | cfe016b | 2012-09-08 17:32:30 +0800 | [diff] [blame] | 1457 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | class_unregister(&firmware_class); |
| 1459 | } |
| 1460 | |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 1461 | fs_initcall(firmware_class_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | module_exit(firmware_class_exit); |
| 1463 | |
| 1464 | EXPORT_SYMBOL(release_firmware); |
| 1465 | EXPORT_SYMBOL(request_firmware); |
| 1466 | EXPORT_SYMBOL(request_firmware_nowait); |
Ming Lei | 2887b39 | 2012-08-04 12:01:22 +0800 | [diff] [blame] | 1467 | EXPORT_SYMBOL_GPL(cache_firmware); |
| 1468 | EXPORT_SYMBOL_GPL(uncache_firmware); |