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