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