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