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> |
Sukadev Bhattiprolu | 563d075 | 2006-09-29 01:59:31 -0700 | [diff] [blame] | 19 | #include <linux/kthread.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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 24 | #define to_dev(obj) container_of(obj, struct device, kobj) |
| 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 | |
| 84 | /* fw_lock could be moved to 'struct firmware_priv' but since it is just |
| 85 | * guarding for corner cases a global lock should be OK */ |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 86 | static DEFINE_MUTEX(fw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
| 88 | struct firmware_priv { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | struct completion completion; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | struct firmware *fw; |
| 91 | unsigned long status; |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 92 | struct page **pages; |
| 93 | int nr_pages; |
| 94 | int page_array_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | struct timer_list timeout; |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 96 | struct device dev; |
Johannes Berg | e9045f9 | 2010-03-29 17:57:20 +0200 | [diff] [blame] | 97 | bool nowait; |
Dmitry Torokhov | e177123 | 2010-03-13 23:49:23 -0800 | [diff] [blame] | 98 | char fw_id[]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 101 | static struct firmware_priv *to_firmware_priv(struct device *dev) |
| 102 | { |
| 103 | return container_of(dev, struct firmware_priv, dev); |
| 104 | } |
| 105 | |
| 106 | static void fw_load_abort(struct firmware_priv *fw_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
| 108 | set_bit(FW_STATUS_ABORT, &fw_priv->status); |
| 109 | wmb(); |
| 110 | complete(&fw_priv->completion); |
| 111 | } |
| 112 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 113 | static ssize_t firmware_timeout_show(struct class *class, |
| 114 | struct class_attribute *attr, |
| 115 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
| 117 | return sprintf(buf, "%d\n", loading_timeout); |
| 118 | } |
| 119 | |
| 120 | /** |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 121 | * firmware_timeout_store - set number of seconds to wait for firmware |
| 122 | * @class: device class pointer |
Randy Dunlap | e59817b | 2010-03-10 11:47:58 -0800 | [diff] [blame] | 123 | * @attr: device attribute pointer |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 124 | * @buf: buffer to scan for timeout value |
| 125 | * @count: number of bytes in @buf |
| 126 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | * Sets the number of seconds to wait for the firmware. Once |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 128 | * this expires an error will be returned to the driver and no |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | * firmware will be provided. |
| 130 | * |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 131 | * Note: zero means 'wait forever'. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | **/ |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 133 | static ssize_t firmware_timeout_store(struct class *class, |
| 134 | struct class_attribute *attr, |
| 135 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
| 137 | loading_timeout = simple_strtol(buf, NULL, 10); |
Stanislaw W. Gruszka | b92eac0 | 2005-06-28 20:44:51 -0700 | [diff] [blame] | 138 | if (loading_timeout < 0) |
| 139 | loading_timeout = 0; |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | return count; |
| 142 | } |
| 143 | |
Dmitry Torokhov | 673fae9 | 2010-03-13 23:49:13 -0800 | [diff] [blame] | 144 | static struct class_attribute firmware_class_attrs[] = { |
| 145 | __ATTR(timeout, S_IWUSR | S_IRUGO, |
| 146 | firmware_timeout_show, firmware_timeout_store), |
| 147 | __ATTR_NULL |
| 148 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
Dmitry Torokhov | 673fae9 | 2010-03-13 23:49:13 -0800 | [diff] [blame] | 150 | static void fw_dev_release(struct device *dev) |
| 151 | { |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 152 | struct firmware_priv *fw_priv = to_firmware_priv(dev); |
Dmitry Torokhov | 673fae9 | 2010-03-13 23:49:13 -0800 | [diff] [blame] | 153 | int i; |
| 154 | |
| 155 | for (i = 0; i < fw_priv->nr_pages; i++) |
| 156 | __free_page(fw_priv->pages[i]); |
| 157 | kfree(fw_priv->pages); |
Dmitry Torokhov | 673fae9 | 2010-03-13 23:49:13 -0800 | [diff] [blame] | 158 | kfree(fw_priv); |
Dmitry Torokhov | 673fae9 | 2010-03-13 23:49:13 -0800 | [diff] [blame] | 159 | |
| 160 | module_put(THIS_MODULE); |
| 161 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 163 | static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | { |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 165 | struct firmware_priv *fw_priv = to_firmware_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 167 | if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | return -ENOMEM; |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 169 | if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout)) |
kay.sievers@vrfy.org | 6897089 | 2005-04-18 21:57:31 -0700 | [diff] [blame] | 170 | return -ENOMEM; |
Johannes Berg | e9045f9 | 2010-03-29 17:57:20 +0200 | [diff] [blame] | 171 | if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait)) |
| 172 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
Adrian Bunk | 1b81d66 | 2006-05-20 15:00:16 -0700 | [diff] [blame] | 177 | static struct class firmware_class = { |
| 178 | .name = "firmware", |
Dmitry Torokhov | 673fae9 | 2010-03-13 23:49:13 -0800 | [diff] [blame] | 179 | .class_attrs = firmware_class_attrs, |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 180 | .dev_uevent = firmware_uevent, |
| 181 | .dev_release = fw_dev_release, |
Adrian Bunk | 1b81d66 | 2006-05-20 15:00:16 -0700 | [diff] [blame] | 182 | }; |
| 183 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 184 | static ssize_t firmware_loading_show(struct device *dev, |
| 185 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | { |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 187 | struct firmware_priv *fw_priv = to_firmware_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status); |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | return sprintf(buf, "%d\n", loading); |
| 191 | } |
| 192 | |
David Woodhouse | dd336c5 | 2010-05-02 11:21:21 +0300 | [diff] [blame] | 193 | static void firmware_free_data(const struct firmware *fw) |
| 194 | { |
| 195 | int i; |
| 196 | vunmap(fw->data); |
| 197 | if (fw->pages) { |
| 198 | for (i = 0; i < PFN_UP(fw->size); i++) |
| 199 | __free_page(fw->pages[i]); |
| 200 | kfree(fw->pages); |
| 201 | } |
| 202 | } |
| 203 | |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 204 | /* Some architectures don't have PAGE_KERNEL_RO */ |
| 205 | #ifndef PAGE_KERNEL_RO |
| 206 | #define PAGE_KERNEL_RO PAGE_KERNEL |
| 207 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | /** |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 209 | * firmware_loading_store - set value in the 'loading' control file |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 210 | * @dev: device pointer |
Randy Dunlap | af9997e | 2006-12-22 01:06:52 -0800 | [diff] [blame] | 211 | * @attr: device attribute pointer |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 212 | * @buf: buffer to scan for loading control value |
| 213 | * @count: number of bytes in @buf |
| 214 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | * The relevant values are: |
| 216 | * |
| 217 | * 1: Start a load, discarding any previous partial load. |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 218 | * 0: Conclude the load and hand the data to the driver code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | * -1: Conclude the load with an error and discard any written data. |
| 220 | **/ |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 221 | static ssize_t firmware_loading_store(struct device *dev, |
| 222 | struct device_attribute *attr, |
| 223 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | { |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 225 | struct firmware_priv *fw_priv = to_firmware_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | int loading = simple_strtol(buf, NULL, 10); |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 227 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
Neil Horman | eea915b | 2012-01-02 15:31:23 -0500 | [diff] [blame] | 229 | mutex_lock(&fw_lock); |
| 230 | |
| 231 | if (!fw_priv->fw) |
| 232 | goto out; |
| 233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | switch (loading) { |
| 235 | case 1: |
David Woodhouse | dd336c5 | 2010-05-02 11:21:21 +0300 | [diff] [blame] | 236 | firmware_free_data(fw_priv->fw); |
| 237 | memset(fw_priv->fw, 0, sizeof(struct firmware)); |
| 238 | /* If the pages are not owned by 'struct firmware' */ |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 239 | for (i = 0; i < fw_priv->nr_pages; i++) |
| 240 | __free_page(fw_priv->pages[i]); |
| 241 | kfree(fw_priv->pages); |
| 242 | fw_priv->pages = NULL; |
| 243 | fw_priv->page_array_size = 0; |
| 244 | fw_priv->nr_pages = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | set_bit(FW_STATUS_LOADING, &fw_priv->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | break; |
| 247 | case 0: |
| 248 | if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) { |
David Woodhouse | dd336c5 | 2010-05-02 11:21:21 +0300 | [diff] [blame] | 249 | vunmap(fw_priv->fw->data); |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 250 | fw_priv->fw->data = vmap(fw_priv->pages, |
| 251 | fw_priv->nr_pages, |
| 252 | 0, PAGE_KERNEL_RO); |
| 253 | if (!fw_priv->fw->data) { |
| 254 | dev_err(dev, "%s: vmap() failed\n", __func__); |
| 255 | goto err; |
| 256 | } |
David Woodhouse | dd336c5 | 2010-05-02 11:21:21 +0300 | [diff] [blame] | 257 | /* Pages are now owned by 'struct firmware' */ |
| 258 | fw_priv->fw->pages = fw_priv->pages; |
| 259 | fw_priv->pages = NULL; |
| 260 | |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 261 | fw_priv->page_array_size = 0; |
| 262 | fw_priv->nr_pages = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | complete(&fw_priv->completion); |
| 264 | clear_bit(FW_STATUS_LOADING, &fw_priv->status); |
| 265 | break; |
| 266 | } |
| 267 | /* fallthrough */ |
| 268 | default: |
Bjorn Helgaas | 266a813 | 2008-10-15 22:04:20 -0700 | [diff] [blame] | 269 | dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | /* fallthrough */ |
| 271 | case -1: |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 272 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | fw_load_abort(fw_priv); |
| 274 | break; |
| 275 | } |
Neil Horman | eea915b | 2012-01-02 15:31:23 -0500 | [diff] [blame] | 276 | out: |
| 277 | mutex_unlock(&fw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | return count; |
| 279 | } |
| 280 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 281 | static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 283 | static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj, |
| 284 | struct bin_attribute *bin_attr, |
| 285 | char *buffer, loff_t offset, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | { |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 287 | struct device *dev = to_dev(kobj); |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 288 | struct firmware_priv *fw_priv = to_firmware_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | struct firmware *fw; |
Akinobu Mita | f37e661 | 2008-07-25 01:48:23 -0700 | [diff] [blame] | 290 | ssize_t ret_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 292 | mutex_lock(&fw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | fw = fw_priv->fw; |
Stanislaw W. Gruszka | b92eac0 | 2005-06-28 20:44:51 -0700 | [diff] [blame] | 294 | if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | ret_count = -ENODEV; |
| 296 | goto out; |
| 297 | } |
Jiri Slaby | 308975f | 2009-06-21 23:57:31 +0200 | [diff] [blame] | 298 | if (offset > fw->size) { |
| 299 | ret_count = 0; |
| 300 | goto out; |
| 301 | } |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 302 | if (count > fw->size - offset) |
| 303 | count = fw->size - offset; |
| 304 | |
| 305 | ret_count = count; |
| 306 | |
| 307 | while (count) { |
| 308 | void *page_data; |
| 309 | int page_nr = offset >> PAGE_SHIFT; |
| 310 | int page_ofs = offset & (PAGE_SIZE-1); |
| 311 | int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count); |
| 312 | |
| 313 | page_data = kmap(fw_priv->pages[page_nr]); |
| 314 | |
| 315 | memcpy(buffer, page_data + page_ofs, page_cnt); |
| 316 | |
| 317 | kunmap(fw_priv->pages[page_nr]); |
| 318 | buffer += page_cnt; |
| 319 | offset += page_cnt; |
| 320 | count -= page_cnt; |
| 321 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | out: |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 323 | mutex_unlock(&fw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | return ret_count; |
| 325 | } |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 326 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 327 | 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] | 328 | { |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 329 | int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 331 | /* If the array of pages is too small, grow it... */ |
| 332 | if (fw_priv->page_array_size < pages_needed) { |
| 333 | int new_array_size = max(pages_needed, |
| 334 | fw_priv->page_array_size * 2); |
| 335 | struct page **new_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 337 | new_pages = kmalloc(new_array_size * sizeof(void *), |
| 338 | GFP_KERNEL); |
| 339 | if (!new_pages) { |
| 340 | fw_load_abort(fw_priv); |
| 341 | return -ENOMEM; |
| 342 | } |
| 343 | memcpy(new_pages, fw_priv->pages, |
| 344 | fw_priv->page_array_size * sizeof(void *)); |
| 345 | memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) * |
| 346 | (new_array_size - fw_priv->page_array_size)); |
| 347 | kfree(fw_priv->pages); |
| 348 | fw_priv->pages = new_pages; |
| 349 | fw_priv->page_array_size = new_array_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | } |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 351 | |
| 352 | while (fw_priv->nr_pages < pages_needed) { |
| 353 | fw_priv->pages[fw_priv->nr_pages] = |
| 354 | alloc_page(GFP_KERNEL | __GFP_HIGHMEM); |
| 355 | |
| 356 | if (!fw_priv->pages[fw_priv->nr_pages]) { |
| 357 | fw_load_abort(fw_priv); |
| 358 | return -ENOMEM; |
| 359 | } |
| 360 | fw_priv->nr_pages++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | /** |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 366 | * firmware_data_write - write method for firmware |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 367 | * @filp: open sysfs file |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 368 | * @kobj: kobject for the device |
Randy Dunlap | 42e61f4 | 2007-07-23 21:42:11 -0700 | [diff] [blame] | 369 | * @bin_attr: bin_attr structure |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 370 | * @buffer: buffer being written |
| 371 | * @offset: buffer offset for write in total data store area |
| 372 | * @count: buffer size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | * |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 374 | * Data written to the 'data' attribute will be later handed to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | * the driver as a firmware image. |
| 376 | **/ |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 377 | static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj, |
| 378 | struct bin_attribute *bin_attr, |
| 379 | char *buffer, loff_t offset, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | { |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 381 | struct device *dev = to_dev(kobj); |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 382 | struct firmware_priv *fw_priv = to_firmware_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | struct firmware *fw; |
| 384 | ssize_t retval; |
| 385 | |
| 386 | if (!capable(CAP_SYS_RAWIO)) |
| 387 | return -EPERM; |
Stanislaw W. Gruszka | b92eac0 | 2005-06-28 20:44:51 -0700 | [diff] [blame] | 388 | |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 389 | mutex_lock(&fw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | fw = fw_priv->fw; |
Stanislaw W. Gruszka | b92eac0 | 2005-06-28 20:44:51 -0700 | [diff] [blame] | 391 | if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | retval = -ENODEV; |
| 393 | goto out; |
| 394 | } |
| 395 | retval = fw_realloc_buffer(fw_priv, offset + count); |
| 396 | if (retval) |
| 397 | goto out; |
| 398 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | retval = count; |
David Woodhouse | 6e03a20 | 2009-04-09 22:04:07 -0700 | [diff] [blame] | 400 | |
| 401 | while (count) { |
| 402 | void *page_data; |
| 403 | int page_nr = offset >> PAGE_SHIFT; |
| 404 | int page_ofs = offset & (PAGE_SIZE - 1); |
| 405 | int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count); |
| 406 | |
| 407 | page_data = kmap(fw_priv->pages[page_nr]); |
| 408 | |
| 409 | memcpy(page_data + page_ofs, buffer, page_cnt); |
| 410 | |
| 411 | kunmap(fw_priv->pages[page_nr]); |
| 412 | buffer += page_cnt; |
| 413 | offset += page_cnt; |
| 414 | count -= page_cnt; |
| 415 | } |
| 416 | |
| 417 | fw->size = max_t(size_t, offset, fw->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | out: |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 419 | mutex_unlock(&fw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | return retval; |
| 421 | } |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 422 | |
Dmitry Torokhov | 0983ca2 | 2010-06-04 00:54:37 -0700 | [diff] [blame] | 423 | static struct bin_attribute firmware_attr_data = { |
| 424 | .attr = { .name = "data", .mode = 0644 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | .size = 0, |
| 426 | .read = firmware_data_read, |
| 427 | .write = firmware_data_write, |
| 428 | }; |
| 429 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 430 | static void firmware_class_timeout(u_long data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | { |
| 432 | struct firmware_priv *fw_priv = (struct firmware_priv *) data; |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 433 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | fw_load_abort(fw_priv); |
| 435 | } |
| 436 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 437 | static struct firmware_priv * |
| 438 | fw_create_instance(struct firmware *firmware, const char *fw_name, |
| 439 | struct device *device, bool uevent, bool nowait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | { |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 441 | struct firmware_priv *fw_priv; |
| 442 | struct device *f_dev; |
| 443 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 445 | fw_priv = kzalloc(sizeof(*fw_priv) + strlen(fw_name) + 1 , GFP_KERNEL); |
| 446 | if (!fw_priv) { |
Bjorn Helgaas | 266a813 | 2008-10-15 22:04:20 -0700 | [diff] [blame] | 447 | dev_err(device, "%s: kmalloc failed\n", __func__); |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 448 | error = -ENOMEM; |
| 449 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 452 | fw_priv->fw = firmware; |
| 453 | fw_priv->nowait = nowait; |
Dmitry Torokhov | e177123 | 2010-03-13 23:49:23 -0800 | [diff] [blame] | 454 | strcpy(fw_priv->fw_id, fw_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | init_completion(&fw_priv->completion); |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 456 | setup_timer(&fw_priv->timeout, |
| 457 | firmware_class_timeout, (u_long) fw_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 459 | f_dev = &fw_priv->dev; |
| 460 | |
| 461 | device_initialize(f_dev); |
Greg Kroah-Hartman | acc0e90 | 2009-06-02 15:39:55 -0700 | [diff] [blame] | 462 | dev_set_name(f_dev, "%s", dev_name(device)); |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 463 | f_dev->parent = device; |
| 464 | f_dev->class = &firmware_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 466 | dev_set_uevent_suppress(f_dev, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | |
| 468 | /* Need to pin this module until class device is destroyed */ |
| 469 | __module_get(THIS_MODULE); |
| 470 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 471 | error = device_add(f_dev); |
| 472 | if (error) { |
| 473 | dev_err(device, "%s: device_register failed\n", __func__); |
| 474 | goto err_put_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 477 | error = device_create_bin_file(f_dev, &firmware_attr_data); |
| 478 | if (error) { |
| 479 | dev_err(device, "%s: sysfs_create_bin_file failed\n", __func__); |
| 480 | goto err_del_dev; |
| 481 | } |
| 482 | |
| 483 | error = device_create_file(f_dev, &dev_attr_loading); |
| 484 | if (error) { |
Bjorn Helgaas | 266a813 | 2008-10-15 22:04:20 -0700 | [diff] [blame] | 485 | dev_err(device, "%s: device_create_file failed\n", __func__); |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 486 | goto err_del_bin_attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 489 | if (uevent) |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 490 | dev_set_uevent_suppress(f_dev, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 492 | return fw_priv; |
| 493 | |
| 494 | err_del_bin_attr: |
| 495 | device_remove_bin_file(f_dev, &firmware_attr_data); |
| 496 | err_del_dev: |
| 497 | device_del(f_dev); |
| 498 | err_put_dev: |
| 499 | put_device(f_dev); |
| 500 | err_out: |
| 501 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 504 | static void fw_destroy_instance(struct firmware_priv *fw_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | { |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 506 | struct device *f_dev = &fw_priv->dev; |
| 507 | |
| 508 | device_remove_file(f_dev, &dev_attr_loading); |
| 509 | device_remove_bin_file(f_dev, &firmware_attr_data); |
| 510 | device_unregister(f_dev); |
| 511 | } |
| 512 | |
| 513 | static int _request_firmware(const struct firmware **firmware_p, |
| 514 | const char *name, struct device *device, |
| 515 | bool uevent, bool nowait) |
| 516 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | struct firmware_priv *fw_priv; |
| 518 | struct firmware *firmware; |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 519 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
| 521 | if (!firmware_p) |
| 522 | return -EINVAL; |
| 523 | |
Jiri Slaby | 4aed064 | 2005-09-13 01:25:01 -0700 | [diff] [blame] | 524 | *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | if (!firmware) { |
Bjorn Helgaas | 266a813 | 2008-10-15 22:04:20 -0700 | [diff] [blame] | 526 | dev_err(device, "%s: kmalloc(struct firmware) failed\n", |
| 527 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | retval = -ENOMEM; |
| 529 | goto out; |
| 530 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | |
Dmitry Torokhov | bcb9bd1 | 2010-03-13 23:49:18 -0800 | [diff] [blame] | 532 | if (fw_get_builtin_firmware(firmware, name)) { |
Rafael J. Wysocki | 6f18ff9 | 2010-02-27 21:43:22 +0100 | [diff] [blame] | 533 | dev_dbg(device, "firmware: using built-in firmware %s\n", name); |
David Woodhouse | 5658c76 | 2008-05-23 13:52:42 +0100 | [diff] [blame] | 534 | return 0; |
| 535 | } |
| 536 | |
Srivatsa S. Bhat | b298d28 | 2011-12-09 23:36:36 +0100 | [diff] [blame] | 537 | read_lock_usermodehelper(); |
| 538 | |
Linus Torvalds | caca951 | 2011-08-24 15:55:30 -0700 | [diff] [blame] | 539 | if (WARN_ON(usermodehelper_is_disabled())) { |
| 540 | dev_err(device, "firmware: %s will not be loaded\n", name); |
| 541 | retval = -EBUSY; |
| 542 | goto out; |
| 543 | } |
| 544 | |
David Woodhouse | 5658c76 | 2008-05-23 13:52:42 +0100 | [diff] [blame] | 545 | if (uevent) |
Rafael J. Wysocki | 6f18ff9 | 2010-02-27 21:43:22 +0100 | [diff] [blame] | 546 | dev_dbg(device, "firmware: requesting %s\n", name); |
David Woodhouse | 5658c76 | 2008-05-23 13:52:42 +0100 | [diff] [blame] | 547 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 548 | fw_priv = fw_create_instance(firmware, name, device, uevent, nowait); |
| 549 | if (IS_ERR(fw_priv)) { |
| 550 | retval = PTR_ERR(fw_priv); |
| 551 | goto out; |
| 552 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 554 | if (uevent) { |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 555 | if (loading_timeout > 0) |
| 556 | mod_timer(&fw_priv->timeout, |
| 557 | round_jiffies_up(jiffies + |
| 558 | loading_timeout * HZ)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 560 | kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD); |
| 561 | } |
| 562 | |
| 563 | wait_for_completion(&fw_priv->completion); |
| 564 | |
| 565 | set_bit(FW_STATUS_DONE, &fw_priv->status); |
| 566 | del_timer_sync(&fw_priv->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 568 | mutex_lock(&fw_lock); |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 569 | if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | retval = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | fw_priv->fw = NULL; |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 572 | mutex_unlock(&fw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 574 | fw_destroy_instance(fw_priv); |
| 575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | out: |
Srivatsa S. Bhat | b298d28 | 2011-12-09 23:36:36 +0100 | [diff] [blame] | 577 | read_unlock_usermodehelper(); |
| 578 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 579 | if (retval) { |
| 580 | release_firmware(firmware); |
Johannes Berg | f45f3c1 | 2010-08-18 17:15:18 +0200 | [diff] [blame] | 581 | *firmware_p = NULL; |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | return retval; |
| 585 | } |
| 586 | |
| 587 | /** |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 588 | * request_firmware: - send firmware request and wait for it |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 589 | * @firmware_p: pointer to firmware image |
| 590 | * @name: name of firmware file |
| 591 | * @device: device for which firmware is being loaded |
| 592 | * |
| 593 | * @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] | 594 | * of @name for device @device. |
| 595 | * |
| 596 | * Should be called from user context where sleeping is allowed. |
| 597 | * |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 598 | * @name will be used as $FIRMWARE in the uevent environment and |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 599 | * should be distinctive enough not to be confused with any other |
| 600 | * firmware image for this or any other device. |
| 601 | **/ |
| 602 | int |
| 603 | request_firmware(const struct firmware **firmware_p, const char *name, |
| 604 | struct device *device) |
| 605 | { |
Bob Liu | 072fc8f | 2011-01-26 18:33:32 +0800 | [diff] [blame] | 606 | return _request_firmware(firmware_p, name, device, true, false); |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | * release_firmware: - release the resource associated with a firmware image |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 611 | * @fw: firmware resource to release |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | **/ |
Dmitry Torokhov | bcb9bd1 | 2010-03-13 23:49:18 -0800 | [diff] [blame] | 613 | void release_firmware(const struct firmware *fw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | { |
| 615 | if (fw) { |
Dmitry Torokhov | bcb9bd1 | 2010-03-13 23:49:18 -0800 | [diff] [blame] | 616 | if (!fw_is_builtin_firmware(fw)) |
| 617 | firmware_free_data(fw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | kfree(fw); |
| 619 | } |
| 620 | } |
| 621 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | /* Async support */ |
| 623 | struct firmware_work { |
| 624 | struct work_struct work; |
| 625 | struct module *module; |
| 626 | const char *name; |
| 627 | struct device *device; |
| 628 | void *context; |
| 629 | void (*cont)(const struct firmware *fw, void *context); |
Bob Liu | 072fc8f | 2011-01-26 18:33:32 +0800 | [diff] [blame] | 630 | bool uevent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | }; |
| 632 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 633 | static int request_firmware_work_func(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | { |
| 635 | struct firmware_work *fw_work = arg; |
| 636 | const struct firmware *fw; |
matthieu castet | 113fab1 | 2005-11-13 16:07:39 -0800 | [diff] [blame] | 637 | int ret; |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | if (!arg) { |
| 640 | WARN_ON(1); |
| 641 | return 0; |
| 642 | } |
Johannes Berg | 9ebfbd4 | 2009-10-29 12:36:02 +0100 | [diff] [blame] | 643 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 644 | ret = _request_firmware(&fw, fw_work->name, fw_work->device, |
| 645 | fw_work->uevent, true); |
Johannes Berg | 9ebfbd4 | 2009-10-29 12:36:02 +0100 | [diff] [blame] | 646 | fw_work->cont(fw, fw_work->context); |
| 647 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | module_put(fw_work->module); |
| 649 | kfree(fw_work); |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 650 | |
matthieu castet | 113fab1 | 2005-11-13 16:07:39 -0800 | [diff] [blame] | 651 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | /** |
Ben Hutchings | 3c31f07 | 2010-02-14 14:18:53 +0000 | [diff] [blame] | 655 | * request_firmware_nowait - asynchronous version of request_firmware |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 656 | * @module: module requesting the firmware |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 657 | * @uevent: sends uevent to copy the firmware image if this flag |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 658 | * is non-zero else the firmware copy must be done manually. |
| 659 | * @name: name of firmware file |
| 660 | * @device: device for which firmware is being loaded |
Johannes Berg | 9ebfbd4 | 2009-10-29 12:36:02 +0100 | [diff] [blame] | 661 | * @gfp: allocation flags |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 662 | * @context: will be passed over to @cont, and |
| 663 | * @fw may be %NULL if firmware request fails. |
| 664 | * @cont: function will be called asynchronously when the firmware |
| 665 | * request is over. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | * |
Ming Lei | 7fcab09 | 2009-05-29 11:33:19 +0800 | [diff] [blame] | 667 | * Asynchronous variant of request_firmware() for user contexts where |
| 668 | * it is not possible to sleep for long time. It can't be called |
| 669 | * in atomic contexts. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | **/ |
| 671 | int |
| 672 | request_firmware_nowait( |
Bob Liu | 072fc8f | 2011-01-26 18:33:32 +0800 | [diff] [blame] | 673 | struct module *module, bool uevent, |
Johannes Berg | 9ebfbd4 | 2009-10-29 12:36:02 +0100 | [diff] [blame] | 674 | const char *name, struct device *device, gfp_t gfp, void *context, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | void (*cont)(const struct firmware *fw, void *context)) |
| 676 | { |
Sukadev Bhattiprolu | 563d075 | 2006-09-29 01:59:31 -0700 | [diff] [blame] | 677 | struct task_struct *task; |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 678 | struct firmware_work *fw_work; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 680 | fw_work = kzalloc(sizeof (struct firmware_work), gfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | if (!fw_work) |
| 682 | return -ENOMEM; |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 683 | |
| 684 | fw_work->module = module; |
| 685 | fw_work->name = name; |
| 686 | fw_work->device = device; |
| 687 | fw_work->context = context; |
| 688 | fw_work->cont = cont; |
| 689 | fw_work->uevent = uevent; |
| 690 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | if (!try_module_get(module)) { |
| 692 | kfree(fw_work); |
| 693 | return -EFAULT; |
| 694 | } |
| 695 | |
Sukadev Bhattiprolu | 563d075 | 2006-09-29 01:59:31 -0700 | [diff] [blame] | 696 | task = kthread_run(request_firmware_work_func, fw_work, |
| 697 | "firmware/%s", name); |
Sukadev Bhattiprolu | 563d075 | 2006-09-29 01:59:31 -0700 | [diff] [blame] | 698 | if (IS_ERR(task)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | fw_work->cont(NULL, fw_work->context); |
matthieu castet | 113fab1 | 2005-11-13 16:07:39 -0800 | [diff] [blame] | 700 | module_put(fw_work->module); |
| 701 | kfree(fw_work); |
Sukadev Bhattiprolu | 563d075 | 2006-09-29 01:59:31 -0700 | [diff] [blame] | 702 | return PTR_ERR(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | } |
Dmitry Torokhov | f8a4bd3 | 2010-06-04 00:54:43 -0700 | [diff] [blame] | 704 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | return 0; |
| 706 | } |
| 707 | |
Dmitry Torokhov | 673fae9 | 2010-03-13 23:49:13 -0800 | [diff] [blame] | 708 | static int __init firmware_class_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | { |
Dmitry Torokhov | 673fae9 | 2010-03-13 23:49:13 -0800 | [diff] [blame] | 710 | return class_register(&firmware_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | } |
Dmitry Torokhov | 673fae9 | 2010-03-13 23:49:13 -0800 | [diff] [blame] | 712 | |
| 713 | static void __exit firmware_class_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | { |
| 715 | class_unregister(&firmware_class); |
| 716 | } |
| 717 | |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 718 | fs_initcall(firmware_class_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | module_exit(firmware_class_exit); |
| 720 | |
| 721 | EXPORT_SYMBOL(release_firmware); |
| 722 | EXPORT_SYMBOL(request_firmware); |
| 723 | EXPORT_SYMBOL(request_firmware_nowait); |