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