Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / drivers / base / firmware_class.c
index 97fe13f..e4c716c 100644 (file)
@@ -7,6 +7,7 @@
  *
  */
 
+#include <linux/capability.h>
 #include <linux/device.h>
 #include <linux/module.h>
 #include <linux/init.h>
@@ -28,9 +29,10 @@ enum {
        FW_STATUS_DONE,
        FW_STATUS_ABORT,
        FW_STATUS_READY,
+       FW_STATUS_READY_NOHOTPLUG,
 };
 
-static int loading_timeout = 10;       /* In seconds */
+static int loading_timeout = 60;       /* In seconds */
 
 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
  * guarding for corner cases a global lock should be OK */
@@ -46,7 +48,7 @@ struct firmware_priv {
        struct timer_list timeout;
 };
 
-static inline void
+static void
 fw_load_abort(struct firmware_priv *fw_priv)
 {
        set_bit(FW_STATUS_ABORT, &fw_priv->status);
@@ -61,37 +63,32 @@ firmware_timeout_show(struct class *class, char *buf)
 }
 
 /**
- * firmware_timeout_store:
- * Description:
+ * firmware_timeout_store - set number of seconds to wait for firmware
+ * @class: device class pointer
+ * @buf: buffer to scan for timeout value
+ * @count: number of bytes in @buf
+ *
  *     Sets the number of seconds to wait for the firmware.  Once
- *     this expires an error will be return to the driver and no
+ *     this expires an error will be returned to the driver and no
  *     firmware will be provided.
  *
- *     Note: zero means 'wait for ever'
- *
+ *     Note: zero means 'wait forever'.
  **/
 static ssize_t
 firmware_timeout_store(struct class *class, const char *buf, size_t count)
 {
        loading_timeout = simple_strtol(buf, NULL, 10);
+       if (loading_timeout < 0)
+               loading_timeout = 0;
        return count;
 }
 
 static CLASS_ATTR(timeout, 0644, firmware_timeout_show, firmware_timeout_store);
 
 static void  fw_class_dev_release(struct class_device *class_dev);
-int firmware_class_hotplug(struct class_device *dev, char **envp,
-                          int num_envp, char *buffer, int buffer_size);
-
-static struct class firmware_class = {
-       .name           = "firmware",
-       .hotplug        = firmware_class_hotplug,
-       .release        = fw_class_dev_release,
-};
 
-int
-firmware_class_hotplug(struct class_device *class_dev, char **envp,
-                      int num_envp, char *buffer, int buffer_size)
+static int firmware_class_uevent(struct class_device *class_dev, char **envp,
+                                int num_envp, char *buffer, int buffer_size)
 {
        struct firmware_priv *fw_priv = class_get_devdata(class_dev);
        int i = 0, len = 0;
@@ -99,18 +96,23 @@ firmware_class_hotplug(struct class_device *class_dev, char **envp,
        if (!test_bit(FW_STATUS_READY, &fw_priv->status))
                return -ENODEV;
 
-       if (add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size, &len,
-                       "FIRMWARE=%s", fw_priv->fw_id))
+       if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
+                          "FIRMWARE=%s", fw_priv->fw_id))
                return -ENOMEM;
-       if (add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size, &len,
-                       "TIMEOUT=%i", loading_timeout))
+       if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
+                          "TIMEOUT=%i", loading_timeout))
                return -ENOMEM;
-
        envp[i] = NULL;
 
        return 0;
 }
 
+static struct class firmware_class = {
+       .name           = "firmware",
+       .uevent         = firmware_class_uevent,
+       .release        = fw_class_dev_release,
+};
+
 static ssize_t
 firmware_loading_show(struct class_device *class_dev, char *buf)
 {
@@ -120,12 +122,15 @@ firmware_loading_show(struct class_device *class_dev, char *buf)
 }
 
 /**
- * firmware_loading_store: - loading control file
- * Description:
+ * firmware_loading_store - set value in the 'loading' control file
+ * @class_dev: class_device pointer
+ * @buf: buffer to scan for loading control value
+ * @count: number of bytes in @buf
+ *
  *     The relevant values are:
  *
  *      1: Start a load, discarding any previous partial load.
- *      0: Conclude the load and handle the data to the driver code.
+ *      0: Conclude the load and hand the data to the driver code.
  *     -1: Conclude the load with an error and discard any written data.
  **/
 static ssize_t
@@ -138,6 +143,10 @@ firmware_loading_store(struct class_device *class_dev,
        switch (loading) {
        case 1:
                down(&fw_lock);
+               if (!fw_priv->fw) {
+                       up(&fw_lock);
+                       break;
+               }
                vfree(fw_priv->fw->data);
                fw_priv->fw->data = NULL;
                fw_priv->fw->size = 0;
@@ -178,7 +187,7 @@ firmware_data_read(struct kobject *kobj,
 
        down(&fw_lock);
        fw = fw_priv->fw;
-       if (test_bit(FW_STATUS_DONE, &fw_priv->status)) {
+       if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
                ret_count = -ENODEV;
                goto out;
        }
@@ -194,22 +203,25 @@ out:
        up(&fw_lock);
        return ret_count;
 }
+
 static int
 fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
 {
        u8 *new_data;
+       int new_size = fw_priv->alloc_size;
 
        if (min_size <= fw_priv->alloc_size)
                return 0;
 
-       new_data = vmalloc(fw_priv->alloc_size + PAGE_SIZE);
+       new_size = ALIGN(min_size, PAGE_SIZE);
+       new_data = vmalloc(new_size);
        if (!new_data) {
                printk(KERN_ERR "%s: unable to alloc buffer\n", __FUNCTION__);
                /* Make sure that we don't keep incomplete data */
                fw_load_abort(fw_priv);
                return -ENOMEM;
        }
-       fw_priv->alloc_size += PAGE_SIZE;
+       fw_priv->alloc_size = new_size;
        if (fw_priv->fw->data) {
                memcpy(new_data, fw_priv->fw->data, fw_priv->fw->size);
                vfree(fw_priv->fw->data);
@@ -220,11 +232,13 @@ fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
 }
 
 /**
- * firmware_data_write:
+ * firmware_data_write - write method for firmware
+ * @kobj: kobject for the class_device
+ * @buffer: buffer being written
+ * @offset: buffer offset for write in total data store area
+ * @count: buffer size
  *
- * Description:
- *
- *     Data written to the 'data' attribute will be later handled to
+ *     Data written to the 'data' attribute will be later handed to
  *     the driver as a firmware image.
  **/
 static ssize_t
@@ -238,9 +252,10 @@ firmware_data_write(struct kobject *kobj,
 
        if (!capable(CAP_SYS_RAWIO))
                return -EPERM;
+
        down(&fw_lock);
        fw = fw_priv->fw;
-       if (test_bit(FW_STATUS_DONE, &fw_priv->status)) {
+       if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
                retval = -ENODEV;
                goto out;
        }
@@ -256,6 +271,7 @@ out:
        up(&fw_lock);
        return retval;
 }
+
 static struct bin_attribute firmware_attr_data_tmpl = {
        .attr = {.name = "data", .mode = 0644, .owner = THIS_MODULE},
        .size = 0,
@@ -293,9 +309,9 @@ fw_register_class_device(struct class_device **class_dev_p,
                         const char *fw_name, struct device *device)
 {
        int retval;
-       struct firmware_priv *fw_priv = kmalloc(sizeof (struct firmware_priv),
+       struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv),
                                                GFP_KERNEL);
-       struct class_device *class_dev = kmalloc(sizeof (struct class_device),
+       struct class_device *class_dev = kzalloc(sizeof(*class_dev),
                                                 GFP_KERNEL);
 
        *class_dev_p = NULL;
@@ -305,8 +321,6 @@ fw_register_class_device(struct class_device **class_dev_p,
                retval = -ENOMEM;
                goto error_kfree;
        }
-       memset(fw_priv, 0, sizeof (*fw_priv));
-       memset(class_dev, 0, sizeof (*class_dev));
 
        init_completion(&fw_priv->completion);
        fw_priv->attr_data = firmware_attr_data_tmpl;
@@ -337,7 +351,7 @@ error_kfree:
 
 static int
 fw_setup_class_device(struct firmware *fw, struct class_device **class_dev_p,
-                     const char *fw_name, struct device *device)
+                     const char *fw_name, struct device *device, int uevent)
 {
        struct class_device *class_dev;
        struct firmware_priv *fw_priv;
@@ -369,7 +383,10 @@ fw_setup_class_device(struct firmware *fw, struct class_device **class_dev_p,
                goto error_unreg;
        }
 
-       set_bit(FW_STATUS_READY, &fw_priv->status);
+       if (uevent)
+                set_bit(FW_STATUS_READY, &fw_priv->status);
+        else
+                set_bit(FW_STATUS_READY_NOHOTPLUG, &fw_priv->status);
        *class_dev_p = class_dev;
        goto out;
 
@@ -379,21 +396,9 @@ out:
        return retval;
 }
 
-/**
- * request_firmware: - request firmware to hotplug and wait for it
- * Description:
- *     @firmware will be used to return a firmware image by the name
- *     of @name for device @device.
- *
- *     Should be called from user context where sleeping is allowed.
- *
- *     @name will be use as $FIRMWARE in the hotplug environment and
- *     should be distinctive enough not to be confused with any other
- *     firmware image for this or any other device.
- **/
-int
-request_firmware(const struct firmware **firmware_p, const char *name,
-                struct device *device)
+static int
+_request_firmware(const struct firmware **firmware_p, const char *name,
+                struct device *device, int uevent)
 {
        struct class_device *class_dev;
        struct firmware_priv *fw_priv;
@@ -403,31 +408,33 @@ request_firmware(const struct firmware **firmware_p, const char *name,
        if (!firmware_p)
                return -EINVAL;
 
-       *firmware_p = firmware = kmalloc(sizeof (struct firmware), GFP_KERNEL);
+       *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
        if (!firmware) {
                printk(KERN_ERR "%s: kmalloc(struct firmware) failed\n",
                       __FUNCTION__);
                retval = -ENOMEM;
                goto out;
        }
-       memset(firmware, 0, sizeof (*firmware));
 
-       retval = fw_setup_class_device(firmware, &class_dev, name, device);
+       retval = fw_setup_class_device(firmware, &class_dev, name, device,
+                                      uevent);
        if (retval)
                goto error_kfree_fw;
 
        fw_priv = class_get_devdata(class_dev);
 
-       if (loading_timeout) {
-               fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
-               add_timer(&fw_priv->timeout);
-       }
-
-       kobject_hotplug(&class_dev->kobj, KOBJ_ADD);
-       wait_for_completion(&fw_priv->completion);
-       set_bit(FW_STATUS_DONE, &fw_priv->status);
+       if (uevent) {
+               if (loading_timeout > 0) {
+                       fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
+                       add_timer(&fw_priv->timeout);
+               }
 
-       del_timer_sync(&fw_priv->timeout);
+               kobject_uevent(&class_dev->kobj, KOBJ_ADD);
+               wait_for_completion(&fw_priv->completion);
+               set_bit(FW_STATUS_DONE, &fw_priv->status);
+               del_timer_sync(&fw_priv->timeout);
+       } else
+               wait_for_completion(&fw_priv->completion);
 
        down(&fw_lock);
        if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
@@ -447,8 +454,32 @@ out:
        return retval;
 }
 
+/**
+ * request_firmware: - send firmware request and wait for it
+ * @firmware_p: pointer to firmware image
+ * @name: name of firmware file
+ * @device: device for which firmware is being loaded
+ *
+ *      @firmware_p will be used to return a firmware image by the name
+ *      of @name for device @device.
+ *
+ *      Should be called from user context where sleeping is allowed.
+ *
+ *      @name will be used as $FIRMWARE in the uevent environment and
+ *      should be distinctive enough not to be confused with any other
+ *      firmware image for this or any other device.
+ **/
+int
+request_firmware(const struct firmware **firmware_p, const char *name,
+                 struct device *device)
+{
+        int uevent = 1;
+        return _request_firmware(firmware_p, name, device, uevent);
+}
+
 /**
  * release_firmware: - release the resource associated with a firmware image
+ * @fw: firmware resource to release
  **/
 void
 release_firmware(const struct firmware *fw)
@@ -459,23 +490,6 @@ release_firmware(const struct firmware *fw)
        }
 }
 
-/**
- * register_firmware: - provide a firmware image for later usage
- *
- * Description:
- *     Make sure that @data will be available by requesting firmware @name.
- *
- *     Note: This will not be possible until some kind of persistence
- *     is available.
- **/
-void
-register_firmware(const char *name, const u8 *data, size_t size)
-{
-       /* This is meaningless without firmware caching, so until we
-        * decide if firmware caching is reasonable just leave it as a
-        * noop */
-}
-
 /* Async support */
 struct firmware_work {
        struct work_struct work;
@@ -484,6 +498,7 @@ struct firmware_work {
        struct device *device;
        void *context;
        void (*cont)(const struct firmware *fw, void *context);
+       int uevent;
 };
 
 static int
@@ -491,36 +506,43 @@ request_firmware_work_func(void *arg)
 {
        struct firmware_work *fw_work = arg;
        const struct firmware *fw;
+       int ret;
        if (!arg) {
                WARN_ON(1);
                return 0;
        }
        daemonize("%s/%s", "firmware", fw_work->name);
-       request_firmware(&fw, fw_work->name, fw_work->device);
-       fw_work->cont(fw, fw_work->context);
-       release_firmware(fw);
+       ret = _request_firmware(&fw, fw_work->name, fw_work->device,
+               fw_work->uevent);
+       if (ret < 0)
+               fw_work->cont(NULL, fw_work->context);
+       else {
+               fw_work->cont(fw, fw_work->context);
+               release_firmware(fw);
+       }
        module_put(fw_work->module);
        kfree(fw_work);
-       return 0;
+       return ret;
 }
 
 /**
- * request_firmware_nowait:
+ * request_firmware_nowait: asynchronous version of request_firmware
+ * @module: module requesting the firmware
+ * @uevent: sends uevent to copy the firmware image if this flag
+ *     is non-zero else the firmware copy must be done manually.
+ * @name: name of firmware file
+ * @device: device for which firmware is being loaded
+ * @context: will be passed over to @cont, and
+ *     @fw may be %NULL if firmware request fails.
+ * @cont: function will be called asynchronously when the firmware
+ *     request is over.
  *
- * Description:
  *     Asynchronous variant of request_firmware() for contexts where
  *     it is not possible to sleep.
- *
- *     @cont will be called asynchronously when the firmware request is over.
- *
- *     @context will be passed over to @cont.
- *
- *     @fw may be %NULL if firmware request fails.
- *
  **/
 int
 request_firmware_nowait(
-       struct module *module,
+       struct module *module, int uevent,
        const char *name, struct device *device, void *context,
        void (*cont)(const struct firmware *fw, void *context))
 {
@@ -541,6 +563,7 @@ request_firmware_nowait(
                .device = device,
                .context = context,
                .cont = cont,
+               .uevent = uevent,
        };
 
        ret = kernel_thread(request_firmware_work_func, fw_work,
@@ -548,6 +571,8 @@ request_firmware_nowait(
 
        if (ret < 0) {
                fw_work->cont(NULL, fw_work->context);
+               module_put(fw_work->module);
+               kfree(fw_work);
                return ret;
        }
        return 0;
@@ -583,4 +608,3 @@ module_exit(firmware_class_exit);
 EXPORT_SYMBOL(release_firmware);
 EXPORT_SYMBOL(request_firmware);
 EXPORT_SYMBOL(request_firmware_nowait);
-EXPORT_SYMBOL(register_firmware);