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