Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * firmware_class.c - Multi purpose firmware loading support |
| 3 | * |
| 4 | * Copyright (c) 2003 Manuel Estrada Sainz <ranty@debian.org> |
| 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | MODULE_AUTHOR("Manuel Estrada Sainz <ranty@debian.org>"); |
| 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; |
| 48 | int alloc_size; |
| 49 | struct timer_list timeout; |
| 50 | }; |
| 51 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 52 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | fw_load_abort(struct firmware_priv *fw_priv) |
| 54 | { |
| 55 | set_bit(FW_STATUS_ABORT, &fw_priv->status); |
| 56 | wmb(); |
| 57 | complete(&fw_priv->completion); |
| 58 | } |
| 59 | |
| 60 | static ssize_t |
| 61 | firmware_timeout_show(struct class *class, char *buf) |
| 62 | { |
| 63 | return sprintf(buf, "%d\n", loading_timeout); |
| 64 | } |
| 65 | |
| 66 | /** |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 67 | * firmware_timeout_store - set number of seconds to wait for firmware |
| 68 | * @class: device class pointer |
| 69 | * @buf: buffer to scan for timeout value |
| 70 | * @count: number of bytes in @buf |
| 71 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | * Sets the number of seconds to wait for the firmware. Once |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 73 | * this expires an error will be returned to the driver and no |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | * firmware will be provided. |
| 75 | * |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 76 | * Note: zero means 'wait forever'. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | **/ |
| 78 | static ssize_t |
| 79 | firmware_timeout_store(struct class *class, const char *buf, size_t count) |
| 80 | { |
| 81 | loading_timeout = simple_strtol(buf, NULL, 10); |
Stanislaw W. Gruszka | b92eac0 | 2005-06-28 20:44:51 -0700 | [diff] [blame] | 82 | if (loading_timeout < 0) |
| 83 | loading_timeout = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | return count; |
| 85 | } |
| 86 | |
| 87 | static CLASS_ATTR(timeout, 0644, firmware_timeout_show, firmware_timeout_store); |
| 88 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 89 | static void fw_dev_release(struct device *dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 91 | static int firmware_uevent(struct device *dev, char **envp, int num_envp, |
| 92 | char *buffer, int buffer_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 94 | struct firmware_priv *fw_priv = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | int i = 0, len = 0; |
| 96 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 97 | if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len, |
| 98 | "FIRMWARE=%s", fw_priv->fw_id)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | return -ENOMEM; |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 100 | if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len, |
| 101 | "TIMEOUT=%i", loading_timeout)) |
kay.sievers@vrfy.org | 6897089 | 2005-04-18 21:57:31 -0700 | [diff] [blame] | 102 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | envp[i] = NULL; |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
Adrian Bunk | 1b81d66 | 2006-05-20 15:00:16 -0700 | [diff] [blame] | 108 | static struct class firmware_class = { |
| 109 | .name = "firmware", |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 110 | .dev_uevent = firmware_uevent, |
| 111 | .dev_release = fw_dev_release, |
Adrian Bunk | 1b81d66 | 2006-05-20 15:00:16 -0700 | [diff] [blame] | 112 | }; |
| 113 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 114 | static ssize_t firmware_loading_show(struct device *dev, |
| 115 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 117 | struct firmware_priv *fw_priv = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status); |
| 119 | return sprintf(buf, "%d\n", loading); |
| 120 | } |
| 121 | |
| 122 | /** |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 123 | * firmware_loading_store - set value in the 'loading' control file |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 124 | * @dev: device pointer |
Randy Dunlap | af9997e | 2006-12-22 01:06:52 -0800 | [diff] [blame] | 125 | * @attr: device attribute pointer |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 126 | * @buf: buffer to scan for loading control value |
| 127 | * @count: number of bytes in @buf |
| 128 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | * The relevant values are: |
| 130 | * |
| 131 | * 1: Start a load, discarding any previous partial load. |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 132 | * 0: Conclude the load and hand the data to the driver code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | * -1: Conclude the load with an error and discard any written data. |
| 134 | **/ |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 135 | static ssize_t firmware_loading_store(struct device *dev, |
| 136 | struct device_attribute *attr, |
| 137 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 139 | struct firmware_priv *fw_priv = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | int loading = simple_strtol(buf, NULL, 10); |
| 141 | |
| 142 | switch (loading) { |
| 143 | case 1: |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 144 | mutex_lock(&fw_lock); |
Stanislaw W. Gruszka | b92eac0 | 2005-06-28 20:44:51 -0700 | [diff] [blame] | 145 | if (!fw_priv->fw) { |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 146 | mutex_unlock(&fw_lock); |
Stanislaw W. Gruszka | b92eac0 | 2005-06-28 20:44:51 -0700 | [diff] [blame] | 147 | break; |
| 148 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | vfree(fw_priv->fw->data); |
| 150 | fw_priv->fw->data = NULL; |
| 151 | fw_priv->fw->size = 0; |
| 152 | fw_priv->alloc_size = 0; |
| 153 | set_bit(FW_STATUS_LOADING, &fw_priv->status); |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 154 | mutex_unlock(&fw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | break; |
| 156 | case 0: |
| 157 | if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) { |
| 158 | complete(&fw_priv->completion); |
| 159 | clear_bit(FW_STATUS_LOADING, &fw_priv->status); |
| 160 | break; |
| 161 | } |
| 162 | /* fallthrough */ |
| 163 | default: |
| 164 | printk(KERN_ERR "%s: unexpected value (%d)\n", __FUNCTION__, |
| 165 | loading); |
| 166 | /* fallthrough */ |
| 167 | case -1: |
| 168 | fw_load_abort(fw_priv); |
| 169 | break; |
| 170 | } |
| 171 | |
| 172 | return count; |
| 173 | } |
| 174 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 175 | static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
| 177 | static ssize_t |
| 178 | firmware_data_read(struct kobject *kobj, |
| 179 | char *buffer, loff_t offset, size_t count) |
| 180 | { |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 181 | struct device *dev = to_dev(kobj); |
| 182 | struct firmware_priv *fw_priv = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | struct firmware *fw; |
| 184 | ssize_t ret_count = count; |
| 185 | |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 186 | mutex_lock(&fw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | fw = fw_priv->fw; |
Stanislaw W. Gruszka | b92eac0 | 2005-06-28 20:44:51 -0700 | [diff] [blame] | 188 | if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | ret_count = -ENODEV; |
| 190 | goto out; |
| 191 | } |
| 192 | if (offset > fw->size) { |
| 193 | ret_count = 0; |
| 194 | goto out; |
| 195 | } |
| 196 | if (offset + ret_count > fw->size) |
| 197 | ret_count = fw->size - offset; |
| 198 | |
| 199 | memcpy(buffer, fw->data + offset, ret_count); |
| 200 | out: |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 201 | mutex_unlock(&fw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | return ret_count; |
| 203 | } |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | static int |
| 206 | fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size) |
| 207 | { |
| 208 | u8 *new_data; |
Jeff Moyer | 30560ba | 2006-02-13 14:52:38 -0800 | [diff] [blame] | 209 | int new_size = fw_priv->alloc_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
| 211 | if (min_size <= fw_priv->alloc_size) |
| 212 | return 0; |
| 213 | |
Jeff Moyer | 30560ba | 2006-02-13 14:52:38 -0800 | [diff] [blame] | 214 | new_size = ALIGN(min_size, PAGE_SIZE); |
| 215 | new_data = vmalloc(new_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | if (!new_data) { |
| 217 | printk(KERN_ERR "%s: unable to alloc buffer\n", __FUNCTION__); |
| 218 | /* Make sure that we don't keep incomplete data */ |
| 219 | fw_load_abort(fw_priv); |
| 220 | return -ENOMEM; |
| 221 | } |
Jeff Moyer | 30560ba | 2006-02-13 14:52:38 -0800 | [diff] [blame] | 222 | fw_priv->alloc_size = new_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | if (fw_priv->fw->data) { |
| 224 | memcpy(new_data, fw_priv->fw->data, fw_priv->fw->size); |
| 225 | vfree(fw_priv->fw->data); |
| 226 | } |
| 227 | fw_priv->fw->data = new_data; |
| 228 | BUG_ON(min_size > fw_priv->alloc_size); |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | /** |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 233 | * firmware_data_write - write method for firmware |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 234 | * @kobj: kobject for the device |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 235 | * @buffer: buffer being written |
| 236 | * @offset: buffer offset for write in total data store area |
| 237 | * @count: buffer size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | * |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 239 | * Data written to the 'data' attribute will be later handed to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | * the driver as a firmware image. |
| 241 | **/ |
| 242 | static ssize_t |
| 243 | firmware_data_write(struct kobject *kobj, |
| 244 | char *buffer, loff_t offset, size_t count) |
| 245 | { |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 246 | struct device *dev = to_dev(kobj); |
| 247 | struct firmware_priv *fw_priv = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | struct firmware *fw; |
| 249 | ssize_t retval; |
| 250 | |
| 251 | if (!capable(CAP_SYS_RAWIO)) |
| 252 | return -EPERM; |
Stanislaw W. Gruszka | b92eac0 | 2005-06-28 20:44:51 -0700 | [diff] [blame] | 253 | |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 254 | mutex_lock(&fw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | fw = fw_priv->fw; |
Stanislaw W. Gruszka | b92eac0 | 2005-06-28 20:44:51 -0700 | [diff] [blame] | 256 | if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | retval = -ENODEV; |
| 258 | goto out; |
| 259 | } |
| 260 | retval = fw_realloc_buffer(fw_priv, offset + count); |
| 261 | if (retval) |
| 262 | goto out; |
| 263 | |
| 264 | memcpy(fw->data + offset, buffer, count); |
| 265 | |
| 266 | fw->size = max_t(size_t, offset + count, fw->size); |
| 267 | retval = count; |
| 268 | out: |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 269 | mutex_unlock(&fw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | return retval; |
| 271 | } |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 272 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | static struct bin_attribute firmware_attr_data_tmpl = { |
| 274 | .attr = {.name = "data", .mode = 0644, .owner = THIS_MODULE}, |
| 275 | .size = 0, |
| 276 | .read = firmware_data_read, |
| 277 | .write = firmware_data_write, |
| 278 | }; |
| 279 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 280 | static void fw_dev_release(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | { |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 282 | struct firmware_priv *fw_priv = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
| 284 | kfree(fw_priv); |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 285 | kfree(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
| 287 | module_put(THIS_MODULE); |
| 288 | } |
| 289 | |
| 290 | static void |
| 291 | firmware_class_timeout(u_long data) |
| 292 | { |
| 293 | struct firmware_priv *fw_priv = (struct firmware_priv *) data; |
| 294 | fw_load_abort(fw_priv); |
| 295 | } |
| 296 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 297 | static inline void fw_setup_device_id(struct device *f_dev, struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | { |
| 299 | /* XXX warning we should watch out for name collisions */ |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 300 | strlcpy(f_dev->bus_id, dev->bus_id, BUS_ID_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 303 | static int fw_register_device(struct device **dev_p, const char *fw_name, |
| 304 | struct device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | { |
| 306 | int retval; |
Jiri Slaby | 4aed064 | 2005-09-13 01:25:01 -0700 | [diff] [blame] | 307 | struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | GFP_KERNEL); |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 309 | struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 311 | *dev_p = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 313 | if (!fw_priv || !f_dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | printk(KERN_ERR "%s: kmalloc failed\n", __FUNCTION__); |
| 315 | retval = -ENOMEM; |
| 316 | goto error_kfree; |
| 317 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
| 319 | init_completion(&fw_priv->completion); |
| 320 | fw_priv->attr_data = firmware_attr_data_tmpl; |
| 321 | strlcpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX); |
| 322 | |
| 323 | fw_priv->timeout.function = firmware_class_timeout; |
| 324 | fw_priv->timeout.data = (u_long) fw_priv; |
| 325 | init_timer(&fw_priv->timeout); |
| 326 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 327 | fw_setup_device_id(f_dev, device); |
| 328 | f_dev->parent = device; |
| 329 | f_dev->class = &firmware_class; |
| 330 | dev_set_drvdata(f_dev, fw_priv); |
Cornelia Huck | bdc4960 | 2007-03-29 11:12:14 +0200 | [diff] [blame] | 331 | f_dev->uevent_suppress = 1; |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 332 | retval = device_register(f_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | if (retval) { |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 334 | printk(KERN_ERR "%s: device_register failed\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | __FUNCTION__); |
| 336 | goto error_kfree; |
| 337 | } |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 338 | *dev_p = f_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | return 0; |
| 340 | |
| 341 | error_kfree: |
| 342 | kfree(fw_priv); |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 343 | kfree(f_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | return retval; |
| 345 | } |
| 346 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 347 | static int fw_setup_device(struct firmware *fw, struct device **dev_p, |
| 348 | const char *fw_name, struct device *device, |
| 349 | int uevent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | { |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 351 | struct device *f_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | struct firmware_priv *fw_priv; |
| 353 | int retval; |
| 354 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 355 | *dev_p = NULL; |
| 356 | retval = fw_register_device(&f_dev, fw_name, device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | if (retval) |
| 358 | goto out; |
| 359 | |
| 360 | /* Need to pin this module until class device is destroyed */ |
| 361 | __module_get(THIS_MODULE); |
| 362 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 363 | fw_priv = dev_get_drvdata(f_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
| 365 | fw_priv->fw = fw; |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 366 | retval = sysfs_create_bin_file(&f_dev->kobj, &fw_priv->attr_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | if (retval) { |
| 368 | printk(KERN_ERR "%s: sysfs_create_bin_file failed\n", |
| 369 | __FUNCTION__); |
| 370 | goto error_unreg; |
| 371 | } |
| 372 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 373 | retval = device_create_file(f_dev, &dev_attr_loading); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | if (retval) { |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 375 | printk(KERN_ERR "%s: device_create_file failed\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | __FUNCTION__); |
| 377 | goto error_unreg; |
| 378 | } |
| 379 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 380 | if (uevent) |
Cornelia Huck | bdc4960 | 2007-03-29 11:12:14 +0200 | [diff] [blame] | 381 | f_dev->uevent_suppress = 0; |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 382 | *dev_p = f_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | goto out; |
| 384 | |
| 385 | error_unreg: |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 386 | device_unregister(f_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | out: |
| 388 | return retval; |
| 389 | } |
| 390 | |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 391 | static int |
| 392 | _request_firmware(const struct firmware **firmware_p, const char *name, |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 393 | struct device *device, int uevent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | { |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 395 | struct device *f_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | struct firmware_priv *fw_priv; |
| 397 | struct firmware *firmware; |
| 398 | int retval; |
| 399 | |
| 400 | if (!firmware_p) |
| 401 | return -EINVAL; |
| 402 | |
Jiri Slaby | 4aed064 | 2005-09-13 01:25:01 -0700 | [diff] [blame] | 403 | *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | if (!firmware) { |
| 405 | printk(KERN_ERR "%s: kmalloc(struct firmware) failed\n", |
| 406 | __FUNCTION__); |
| 407 | retval = -ENOMEM; |
| 408 | goto out; |
| 409 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 411 | retval = fw_setup_device(firmware, &f_dev, name, device, uevent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | if (retval) |
| 413 | goto error_kfree_fw; |
| 414 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 415 | fw_priv = dev_get_drvdata(f_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 417 | if (uevent) { |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 418 | if (loading_timeout > 0) { |
| 419 | fw_priv->timeout.expires = jiffies + loading_timeout * HZ; |
| 420 | add_timer(&fw_priv->timeout); |
| 421 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 423 | kobject_uevent(&f_dev->kobj, KOBJ_ADD); |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 424 | wait_for_completion(&fw_priv->completion); |
| 425 | set_bit(FW_STATUS_DONE, &fw_priv->status); |
| 426 | del_timer_sync(&fw_priv->timeout); |
| 427 | } else |
| 428 | wait_for_completion(&fw_priv->completion); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 430 | mutex_lock(&fw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) { |
| 432 | retval = -ENOENT; |
| 433 | release_firmware(fw_priv->fw); |
| 434 | *firmware_p = NULL; |
| 435 | } |
| 436 | fw_priv->fw = NULL; |
Laura Garcia | cad1e55 | 2006-05-23 23:22:38 +0200 | [diff] [blame] | 437 | mutex_unlock(&fw_lock); |
Greg Kroah-Hartman | e55c879 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 438 | device_unregister(f_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | goto out; |
| 440 | |
| 441 | error_kfree_fw: |
| 442 | kfree(firmware); |
| 443 | *firmware_p = NULL; |
| 444 | out: |
| 445 | return retval; |
| 446 | } |
| 447 | |
| 448 | /** |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 449 | * request_firmware: - send firmware request and wait for it |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 450 | * @firmware_p: pointer to firmware image |
| 451 | * @name: name of firmware file |
| 452 | * @device: device for which firmware is being loaded |
| 453 | * |
| 454 | * @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] | 455 | * of @name for device @device. |
| 456 | * |
| 457 | * Should be called from user context where sleeping is allowed. |
| 458 | * |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 459 | * @name will be used as $FIRMWARE in the uevent environment and |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 460 | * should be distinctive enough not to be confused with any other |
| 461 | * firmware image for this or any other device. |
| 462 | **/ |
| 463 | int |
| 464 | request_firmware(const struct firmware **firmware_p, const char *name, |
| 465 | struct device *device) |
| 466 | { |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 467 | int uevent = 1; |
| 468 | return _request_firmware(firmware_p, name, device, uevent); |
Abhay Salunke | 6e3eaab | 2005-09-06 15:17:13 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | * release_firmware: - release the resource associated with a firmware image |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 473 | * @fw: firmware resource to release |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | **/ |
| 475 | void |
| 476 | release_firmware(const struct firmware *fw) |
| 477 | { |
| 478 | if (fw) { |
| 479 | vfree(fw->data); |
| 480 | kfree(fw); |
| 481 | } |
| 482 | } |
| 483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | /* Async support */ |
| 485 | struct firmware_work { |
| 486 | struct work_struct work; |
| 487 | struct module *module; |
| 488 | const char *name; |
| 489 | struct device *device; |
| 490 | void *context; |
| 491 | void (*cont)(const struct firmware *fw, void *context); |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 492 | int uevent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | }; |
| 494 | |
| 495 | static int |
| 496 | request_firmware_work_func(void *arg) |
| 497 | { |
| 498 | struct firmware_work *fw_work = arg; |
| 499 | const struct firmware *fw; |
matthieu castet | 113fab1 | 2005-11-13 16:07:39 -0800 | [diff] [blame] | 500 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | if (!arg) { |
| 502 | WARN_ON(1); |
| 503 | return 0; |
| 504 | } |
matthieu castet | 113fab1 | 2005-11-13 16:07:39 -0800 | [diff] [blame] | 505 | ret = _request_firmware(&fw, fw_work->name, fw_work->device, |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 506 | fw_work->uevent); |
matthieu castet | 113fab1 | 2005-11-13 16:07:39 -0800 | [diff] [blame] | 507 | if (ret < 0) |
| 508 | fw_work->cont(NULL, fw_work->context); |
| 509 | else { |
| 510 | fw_work->cont(fw, fw_work->context); |
| 511 | release_firmware(fw); |
| 512 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | module_put(fw_work->module); |
| 514 | kfree(fw_work); |
matthieu castet | 113fab1 | 2005-11-13 16:07:39 -0800 | [diff] [blame] | 515 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /** |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 519 | * request_firmware_nowait: asynchronous version of request_firmware |
| 520 | * @module: module requesting the firmware |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 521 | * @uevent: sends uevent to copy the firmware image if this flag |
Randy Dunlap | eb8e317 | 2005-10-30 15:03:01 -0800 | [diff] [blame] | 522 | * is non-zero else the firmware copy must be done manually. |
| 523 | * @name: name of firmware file |
| 524 | * @device: device for which firmware is being loaded |
| 525 | * @context: will be passed over to @cont, and |
| 526 | * @fw may be %NULL if firmware request fails. |
| 527 | * @cont: function will be called asynchronously when the firmware |
| 528 | * request is over. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | * Asynchronous variant of request_firmware() for contexts where |
| 531 | * it is not possible to sleep. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | **/ |
| 533 | int |
| 534 | request_firmware_nowait( |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 535 | struct module *module, int uevent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | const char *name, struct device *device, void *context, |
| 537 | void (*cont)(const struct firmware *fw, void *context)) |
| 538 | { |
Sukadev Bhattiprolu | 563d075 | 2006-09-29 01:59:31 -0700 | [diff] [blame] | 539 | struct task_struct *task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work), |
| 541 | GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | |
| 543 | if (!fw_work) |
| 544 | return -ENOMEM; |
| 545 | if (!try_module_get(module)) { |
| 546 | kfree(fw_work); |
| 547 | return -EFAULT; |
| 548 | } |
| 549 | |
| 550 | *fw_work = (struct firmware_work) { |
| 551 | .module = module, |
| 552 | .name = name, |
| 553 | .device = device, |
| 554 | .context = context, |
| 555 | .cont = cont, |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 556 | .uevent = uevent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | }; |
| 558 | |
Sukadev Bhattiprolu | 563d075 | 2006-09-29 01:59:31 -0700 | [diff] [blame] | 559 | task = kthread_run(request_firmware_work_func, fw_work, |
| 560 | "firmware/%s", name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
Sukadev Bhattiprolu | 563d075 | 2006-09-29 01:59:31 -0700 | [diff] [blame] | 562 | if (IS_ERR(task)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | fw_work->cont(NULL, fw_work->context); |
matthieu castet | 113fab1 | 2005-11-13 16:07:39 -0800 | [diff] [blame] | 564 | module_put(fw_work->module); |
| 565 | kfree(fw_work); |
Sukadev Bhattiprolu | 563d075 | 2006-09-29 01:59:31 -0700 | [diff] [blame] | 566 | return PTR_ERR(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | } |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | static int __init |
| 572 | firmware_class_init(void) |
| 573 | { |
| 574 | int error; |
| 575 | error = class_register(&firmware_class); |
| 576 | if (error) { |
| 577 | printk(KERN_ERR "%s: class_register failed\n", __FUNCTION__); |
| 578 | return error; |
| 579 | } |
| 580 | error = class_create_file(&firmware_class, &class_attr_timeout); |
| 581 | if (error) { |
| 582 | printk(KERN_ERR "%s: class_create_file failed\n", |
| 583 | __FUNCTION__); |
| 584 | class_unregister(&firmware_class); |
| 585 | } |
| 586 | return error; |
| 587 | |
| 588 | } |
| 589 | static void __exit |
| 590 | firmware_class_exit(void) |
| 591 | { |
| 592 | class_unregister(&firmware_class); |
| 593 | } |
| 594 | |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 595 | fs_initcall(firmware_class_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | module_exit(firmware_class_exit); |
| 597 | |
| 598 | EXPORT_SYMBOL(release_firmware); |
| 599 | EXPORT_SYMBOL(request_firmware); |
| 600 | EXPORT_SYMBOL(request_firmware_nowait); |