firmware loader: embed device into firmware_priv structure

Both these structures have the same lifetime rules so instead of allocating
and managing them separately embed struct device into struct firmware_priv.
Also make sure to delete sysfs attributes ourselves instead of expecting
sysfs to clean up our mess.

Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
This commit is contained in:
Dmitry Torokhov 2010-06-04 00:54:43 -07:00 committed by Greg Kroah-Hartman
parent 0983ca2d0f
commit f8a4bd3456

View File

@ -93,22 +93,26 @@ struct firmware_priv {
int nr_pages; int nr_pages;
int page_array_size; int page_array_size;
struct timer_list timeout; struct timer_list timeout;
struct device dev;
bool nowait; bool nowait;
char fw_id[]; char fw_id[];
}; };
static void static struct firmware_priv *to_firmware_priv(struct device *dev)
fw_load_abort(struct firmware_priv *fw_priv) {
return container_of(dev, struct firmware_priv, dev);
}
static void fw_load_abort(struct firmware_priv *fw_priv)
{ {
set_bit(FW_STATUS_ABORT, &fw_priv->status); set_bit(FW_STATUS_ABORT, &fw_priv->status);
wmb(); wmb();
complete(&fw_priv->completion); complete(&fw_priv->completion);
} }
static ssize_t static ssize_t firmware_timeout_show(struct class *class,
firmware_timeout_show(struct class *class, struct class_attribute *attr,
struct class_attribute *attr, char *buf)
char *buf)
{ {
return sprintf(buf, "%d\n", loading_timeout); return sprintf(buf, "%d\n", loading_timeout);
} }
@ -126,14 +130,14 @@ firmware_timeout_show(struct class *class,
* *
* Note: zero means 'wait forever'. * Note: zero means 'wait forever'.
**/ **/
static ssize_t static ssize_t firmware_timeout_store(struct class *class,
firmware_timeout_store(struct class *class, struct class_attribute *attr,
struct class_attribute *attr, const char *buf, size_t count)
const char *buf, size_t count)
{ {
loading_timeout = simple_strtol(buf, NULL, 10); loading_timeout = simple_strtol(buf, NULL, 10);
if (loading_timeout < 0) if (loading_timeout < 0)
loading_timeout = 0; loading_timeout = 0;
return count; return count;
} }
@ -145,21 +149,20 @@ static struct class_attribute firmware_class_attrs[] = {
static void fw_dev_release(struct device *dev) static void fw_dev_release(struct device *dev)
{ {
struct firmware_priv *fw_priv = dev_get_drvdata(dev); struct firmware_priv *fw_priv = to_firmware_priv(dev);
int i; int i;
for (i = 0; i < fw_priv->nr_pages; i++) for (i = 0; i < fw_priv->nr_pages; i++)
__free_page(fw_priv->pages[i]); __free_page(fw_priv->pages[i]);
kfree(fw_priv->pages); kfree(fw_priv->pages);
kfree(fw_priv); kfree(fw_priv);
kfree(dev);
module_put(THIS_MODULE); module_put(THIS_MODULE);
} }
static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env) static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
{ {
struct firmware_priv *fw_priv = dev_get_drvdata(dev); struct firmware_priv *fw_priv = to_firmware_priv(dev);
if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id)) if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id))
return -ENOMEM; return -ENOMEM;
@ -181,8 +184,9 @@ static struct class firmware_class = {
static ssize_t firmware_loading_show(struct device *dev, static ssize_t firmware_loading_show(struct device *dev,
struct device_attribute *attr, char *buf) struct device_attribute *attr, char *buf)
{ {
struct firmware_priv *fw_priv = dev_get_drvdata(dev); struct firmware_priv *fw_priv = to_firmware_priv(dev);
int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status); int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
return sprintf(buf, "%d\n", loading); return sprintf(buf, "%d\n", loading);
} }
@ -218,7 +222,7 @@ static ssize_t firmware_loading_store(struct device *dev,
struct device_attribute *attr, struct device_attribute *attr,
const char *buf, size_t count) const char *buf, size_t count)
{ {
struct firmware_priv *fw_priv = dev_get_drvdata(dev); struct firmware_priv *fw_priv = to_firmware_priv(dev);
int loading = simple_strtol(buf, NULL, 10); int loading = simple_strtol(buf, NULL, 10);
int i; int i;
@ -276,13 +280,12 @@ static ssize_t firmware_loading_store(struct device *dev,
static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store); static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
static ssize_t static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
firmware_data_read(struct file *filp, struct kobject *kobj, struct bin_attribute *bin_attr,
struct bin_attribute *bin_attr, char *buffer, loff_t offset, char *buffer, loff_t offset, size_t count)
size_t count)
{ {
struct device *dev = to_dev(kobj); struct device *dev = to_dev(kobj);
struct firmware_priv *fw_priv = dev_get_drvdata(dev); struct firmware_priv *fw_priv = to_firmware_priv(dev);
struct firmware *fw; struct firmware *fw;
ssize_t ret_count; ssize_t ret_count;
@ -321,8 +324,7 @@ firmware_data_read(struct file *filp, struct kobject *kobj,
return ret_count; return ret_count;
} }
static int static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
{ {
int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT; int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
@ -372,13 +374,12 @@ fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
* Data written to the 'data' attribute will be later handed to * Data written to the 'data' attribute will be later handed to
* the driver as a firmware image. * the driver as a firmware image.
**/ **/
static ssize_t static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
firmware_data_write(struct file* filp, struct kobject *kobj, struct bin_attribute *bin_attr,
struct bin_attribute *bin_attr, char *buffer, char *buffer, loff_t offset, size_t count)
loff_t offset, size_t count)
{ {
struct device *dev = to_dev(kobj); struct device *dev = to_dev(kobj);
struct firmware_priv *fw_priv = dev_get_drvdata(dev); struct firmware_priv *fw_priv = to_firmware_priv(dev);
struct firmware *fw; struct firmware *fw;
ssize_t retval; ssize_t retval;
@ -426,107 +427,96 @@ static struct bin_attribute firmware_attr_data = {
.write = firmware_data_write, .write = firmware_data_write,
}; };
static void static void firmware_class_timeout(u_long data)
firmware_class_timeout(u_long data)
{ {
struct firmware_priv *fw_priv = (struct firmware_priv *) data; struct firmware_priv *fw_priv = (struct firmware_priv *) data;
fw_load_abort(fw_priv); fw_load_abort(fw_priv);
} }
static int fw_register_device(struct device **dev_p, const char *fw_name, static struct firmware_priv *
struct device *device) fw_create_instance(struct firmware *firmware, const char *fw_name,
struct device *device, bool uevent, bool nowait)
{ {
int retval; struct firmware_priv *fw_priv;
struct firmware_priv *fw_priv = struct device *f_dev;
kzalloc(sizeof(*fw_priv) + strlen(fw_name) + 1 , GFP_KERNEL); int error;
struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL);
*dev_p = NULL; fw_priv = kzalloc(sizeof(*fw_priv) + strlen(fw_name) + 1 , GFP_KERNEL);
if (!fw_priv) {
if (!fw_priv || !f_dev) {
dev_err(device, "%s: kmalloc failed\n", __func__); dev_err(device, "%s: kmalloc failed\n", __func__);
retval = -ENOMEM; error = -ENOMEM;
goto error_kfree; goto err_out;
} }
fw_priv->fw = firmware;
fw_priv->nowait = nowait;
strcpy(fw_priv->fw_id, fw_name); strcpy(fw_priv->fw_id, fw_name);
init_completion(&fw_priv->completion); init_completion(&fw_priv->completion);
fw_priv->timeout.function = firmware_class_timeout; setup_timer(&fw_priv->timeout,
fw_priv->timeout.data = (u_long) fw_priv; firmware_class_timeout, (u_long) fw_priv);
init_timer(&fw_priv->timeout);
f_dev = &fw_priv->dev;
device_initialize(f_dev);
dev_set_name(f_dev, "%s", dev_name(device)); dev_set_name(f_dev, "%s", dev_name(device));
f_dev->parent = device; f_dev->parent = device;
f_dev->class = &firmware_class; f_dev->class = &firmware_class;
dev_set_drvdata(f_dev, fw_priv);
dev_set_uevent_suppress(f_dev, 1);
retval = device_register(f_dev);
if (retval) {
dev_err(device, "%s: device_register failed\n", __func__);
put_device(f_dev);
return retval;
}
*dev_p = f_dev;
return 0;
error_kfree: dev_set_uevent_suppress(f_dev, true);
kfree(f_dev);
kfree(fw_priv);
return retval;
}
static int fw_setup_device(struct firmware *fw, struct device **dev_p,
const char *fw_name, struct device *device,
int uevent, bool nowait)
{
struct device *f_dev;
struct firmware_priv *fw_priv;
int retval;
*dev_p = NULL;
retval = fw_register_device(&f_dev, fw_name, device);
if (retval)
goto out;
/* Need to pin this module until class device is destroyed */ /* Need to pin this module until class device is destroyed */
__module_get(THIS_MODULE); __module_get(THIS_MODULE);
fw_priv = dev_get_drvdata(f_dev); error = device_add(f_dev);
if (error) {
fw_priv->nowait = nowait; dev_err(device, "%s: device_register failed\n", __func__);
goto err_put_dev;
fw_priv->fw = fw;
retval = sysfs_create_bin_file(&f_dev->kobj, &firmware_attr_data);
if (retval) {
dev_err(device, "%s: sysfs_create_bin_file failed\n", __func__);
goto error_unreg;
} }
retval = device_create_file(f_dev, &dev_attr_loading); error = device_create_bin_file(f_dev, &firmware_attr_data);
if (retval) { if (error) {
dev_err(device, "%s: sysfs_create_bin_file failed\n", __func__);
goto err_del_dev;
}
error = device_create_file(f_dev, &dev_attr_loading);
if (error) {
dev_err(device, "%s: device_create_file failed\n", __func__); dev_err(device, "%s: device_create_file failed\n", __func__);
goto error_unreg; goto err_del_bin_attr;
} }
if (uevent) if (uevent)
dev_set_uevent_suppress(f_dev, 0); dev_set_uevent_suppress(f_dev, false);
*dev_p = f_dev;
goto out;
error_unreg: return fw_priv;
device_unregister(f_dev);
out: err_del_bin_attr:
return retval; device_remove_bin_file(f_dev, &firmware_attr_data);
err_del_dev:
device_del(f_dev);
err_put_dev:
put_device(f_dev);
err_out:
return ERR_PTR(error);
} }
static int static void fw_destroy_instance(struct firmware_priv *fw_priv)
_request_firmware(const struct firmware **firmware_p, const char *name, {
struct device *device, int uevent, bool nowait) struct device *f_dev = &fw_priv->dev;
device_remove_file(f_dev, &dev_attr_loading);
device_remove_bin_file(f_dev, &firmware_attr_data);
device_unregister(f_dev);
}
static int _request_firmware(const struct firmware **firmware_p,
const char *name, struct device *device,
bool uevent, bool nowait)
{ {
struct device *f_dev;
struct firmware_priv *fw_priv; struct firmware_priv *fw_priv;
struct firmware *firmware; struct firmware *firmware;
int retval; int retval = 0;
if (!firmware_p) if (!firmware_p)
return -EINVAL; return -EINVAL;
@ -547,41 +537,40 @@ _request_firmware(const struct firmware **firmware_p, const char *name,
if (uevent) if (uevent)
dev_dbg(device, "firmware: requesting %s\n", name); dev_dbg(device, "firmware: requesting %s\n", name);
retval = fw_setup_device(firmware, &f_dev, name, device, fw_priv = fw_create_instance(firmware, name, device, uevent, nowait);
uevent, nowait); if (IS_ERR(fw_priv)) {
if (retval) retval = PTR_ERR(fw_priv);
goto error_kfree_fw; goto out;
}
fw_priv = dev_get_drvdata(f_dev);
if (uevent) { if (uevent) {
if (loading_timeout > 0) { if (loading_timeout > 0)
fw_priv->timeout.expires = jiffies + loading_timeout * HZ; mod_timer(&fw_priv->timeout,
add_timer(&fw_priv->timeout); round_jiffies_up(jiffies +
} loading_timeout * HZ));
kobject_uevent(&f_dev->kobj, KOBJ_ADD); kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
wait_for_completion(&fw_priv->completion); }
set_bit(FW_STATUS_DONE, &fw_priv->status);
del_timer_sync(&fw_priv->timeout); wait_for_completion(&fw_priv->completion);
} else
wait_for_completion(&fw_priv->completion); set_bit(FW_STATUS_DONE, &fw_priv->status);
del_timer_sync(&fw_priv->timeout);
mutex_lock(&fw_lock); mutex_lock(&fw_lock);
if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) { if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status))
retval = -ENOENT; retval = -ENOENT;
release_firmware(fw_priv->fw);
*firmware_p = NULL;
}
fw_priv->fw = NULL; fw_priv->fw = NULL;
mutex_unlock(&fw_lock); mutex_unlock(&fw_lock);
device_unregister(f_dev);
goto out;
error_kfree_fw: fw_destroy_instance(fw_priv);
kfree(firmware);
*firmware_p = NULL;
out: out:
if (retval) {
release_firmware(firmware);
firmware_p = NULL;
}
return retval; return retval;
} }
@ -632,23 +621,24 @@ struct firmware_work {
int uevent; int uevent;
}; };
static int static int request_firmware_work_func(void *arg)
request_firmware_work_func(void *arg)
{ {
struct firmware_work *fw_work = arg; struct firmware_work *fw_work = arg;
const struct firmware *fw; const struct firmware *fw;
int ret; int ret;
if (!arg) { if (!arg) {
WARN_ON(1); WARN_ON(1);
return 0; return 0;
} }
ret = _request_firmware(&fw, fw_work->name, fw_work->device,
fw_work->uevent, true);
ret = _request_firmware(&fw, fw_work->name, fw_work->device,
fw_work->uevent, true);
fw_work->cont(fw, fw_work->context); fw_work->cont(fw, fw_work->context);
module_put(fw_work->module); module_put(fw_work->module);
kfree(fw_work); kfree(fw_work);
return ret; return ret;
} }
@ -676,34 +666,33 @@ request_firmware_nowait(
void (*cont)(const struct firmware *fw, void *context)) void (*cont)(const struct firmware *fw, void *context))
{ {
struct task_struct *task; struct task_struct *task;
struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work), struct firmware_work *fw_work;
gfp);
fw_work = kzalloc(sizeof (struct firmware_work), gfp);
if (!fw_work) if (!fw_work)
return -ENOMEM; return -ENOMEM;
fw_work->module = module;
fw_work->name = name;
fw_work->device = device;
fw_work->context = context;
fw_work->cont = cont;
fw_work->uevent = uevent;
if (!try_module_get(module)) { if (!try_module_get(module)) {
kfree(fw_work); kfree(fw_work);
return -EFAULT; return -EFAULT;
} }
*fw_work = (struct firmware_work) {
.module = module,
.name = name,
.device = device,
.context = context,
.cont = cont,
.uevent = uevent,
};
task = kthread_run(request_firmware_work_func, fw_work, task = kthread_run(request_firmware_work_func, fw_work,
"firmware/%s", name); "firmware/%s", name);
if (IS_ERR(task)) { if (IS_ERR(task)) {
fw_work->cont(NULL, fw_work->context); fw_work->cont(NULL, fw_work->context);
module_put(fw_work->module); module_put(fw_work->module);
kfree(fw_work); kfree(fw_work);
return PTR_ERR(task); return PTR_ERR(task);
} }
return 0; return 0;
} }