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