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