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