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