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