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