drm/radeon: Fix sparc regression in r300_scratch()
[safe/jmp/linux-2.6] / drivers / base / firmware_class.c
index 44699d9..18518ba 100644 (file)
@@ -17,9 +17,8 @@
 #include <linux/bitops.h>
 #include <linux/mutex.h>
 #include <linux/kthread.h>
-
+#include <linux/highmem.h>
 #include <linux/firmware.h>
-#include "base.h"
 
 #define to_dev(obj) container_of(obj, struct device, kobj)
 
@@ -40,12 +39,15 @@ static int loading_timeout = 60;    /* In seconds */
 static DEFINE_MUTEX(fw_lock);
 
 struct firmware_priv {
-       char fw_id[FIRMWARE_NAME_MAX];
+       char *fw_id;
        struct completion completion;
        struct bin_attribute attr_data;
        struct firmware *fw;
        unsigned long status;
-       int alloc_size;
+       struct page **pages;
+       int nr_pages;
+       int page_array_size;
+       const char *vdata;
        struct timer_list timeout;
 };
 
@@ -66,7 +68,9 @@ fw_load_abort(struct firmware_priv *fw_priv)
 }
 
 static ssize_t
-firmware_timeout_show(struct class *class, char *buf)
+firmware_timeout_show(struct class *class,
+                     struct class_attribute *attr,
+                     char *buf)
 {
        return sprintf(buf, "%d\n", loading_timeout);
 }
@@ -74,6 +78,7 @@ firmware_timeout_show(struct class *class, char *buf)
 /**
  * firmware_timeout_store - set number of seconds to wait for firmware
  * @class: device class pointer
+ * @attr: device attribute pointer
  * @buf: buffer to scan for timeout value
  * @count: number of bytes in @buf
  *
@@ -84,7 +89,9 @@ firmware_timeout_show(struct class *class, char *buf)
  *     Note: zero means 'wait forever'.
  **/
 static ssize_t
-firmware_timeout_store(struct class *class, const char *buf, size_t count)
+firmware_timeout_store(struct class *class,
+                       struct class_attribute *attr,
+                       const char *buf, size_t count)
 {
        loading_timeout = simple_strtol(buf, NULL, 10);
        if (loading_timeout < 0)
@@ -122,6 +129,10 @@ static ssize_t firmware_loading_show(struct device *dev,
        return sprintf(buf, "%d\n", loading);
 }
 
+/* Some architectures don't have PAGE_KERNEL_RO */
+#ifndef PAGE_KERNEL_RO
+#define PAGE_KERNEL_RO PAGE_KERNEL
+#endif
 /**
  * firmware_loading_store - set value in the 'loading' control file
  * @dev: device pointer
@@ -141,6 +152,7 @@ static ssize_t firmware_loading_store(struct device *dev,
 {
        struct firmware_priv *fw_priv = dev_get_drvdata(dev);
        int loading = simple_strtol(buf, NULL, 10);
+       int i;
 
        switch (loading) {
        case 1:
@@ -151,13 +163,29 @@ static ssize_t firmware_loading_store(struct device *dev,
                }
                vfree(fw_priv->fw->data);
                fw_priv->fw->data = NULL;
+               for (i = 0; i < fw_priv->nr_pages; i++)
+                       __free_page(fw_priv->pages[i]);
+               kfree(fw_priv->pages);
+               fw_priv->pages = NULL;
+               fw_priv->page_array_size = 0;
+               fw_priv->nr_pages = 0;
                fw_priv->fw->size = 0;
-               fw_priv->alloc_size = 0;
                set_bit(FW_STATUS_LOADING, &fw_priv->status);
                mutex_unlock(&fw_lock);
                break;
        case 0:
                if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
+                       vfree(fw_priv->fw->data);
+                       fw_priv->fw->data = vmap(fw_priv->pages,
+                                                fw_priv->nr_pages,
+                                                0, PAGE_KERNEL_RO);
+                       if (!fw_priv->fw->data) {
+                               dev_err(dev, "%s: vmap() failed\n", __func__);
+                               goto err;
+                       }
+                       /* Pages will be freed by vfree() */
+                       fw_priv->page_array_size = 0;
+                       fw_priv->nr_pages = 0;
                        complete(&fw_priv->completion);
                        clear_bit(FW_STATUS_LOADING, &fw_priv->status);
                        break;
@@ -167,6 +195,7 @@ static ssize_t firmware_loading_store(struct device *dev,
                dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
                /* fallthrough */
        case -1:
+       err:
                fw_load_abort(fw_priv);
                break;
        }
@@ -191,8 +220,30 @@ firmware_data_read(struct kobject *kobj, struct bin_attribute *bin_attr,
                ret_count = -ENODEV;
                goto out;
        }
-       ret_count = memory_read_from_buffer(buffer, count, &offset,
-                                               fw->data, fw->size);
+       if (offset > fw->size) {
+               ret_count = 0;
+               goto out;
+       }
+       if (count > fw->size - offset)
+               count = fw->size - offset;
+
+       ret_count = count;
+
+       while (count) {
+               void *page_data;
+               int page_nr = offset >> PAGE_SHIFT;
+               int page_ofs = offset & (PAGE_SIZE-1);
+               int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
+
+               page_data = kmap(fw_priv->pages[page_nr]);
+
+               memcpy(buffer, page_data + page_ofs, page_cnt);
+
+               kunmap(fw_priv->pages[page_nr]);
+               buffer += page_cnt;
+               offset += page_cnt;
+               count -= page_cnt;
+       }
 out:
        mutex_unlock(&fw_lock);
        return ret_count;
@@ -201,27 +252,39 @@ out:
 static int
 fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
 {
-       u8 *new_data;
-       int new_size = fw_priv->alloc_size;
+       int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
+
+       /* If the array of pages is too small, grow it... */
+       if (fw_priv->page_array_size < pages_needed) {
+               int new_array_size = max(pages_needed,
+                                        fw_priv->page_array_size * 2);
+               struct page **new_pages;
+
+               new_pages = kmalloc(new_array_size * sizeof(void *),
+                                   GFP_KERNEL);
+               if (!new_pages) {
+                       fw_load_abort(fw_priv);
+                       return -ENOMEM;
+               }
+               memcpy(new_pages, fw_priv->pages,
+                      fw_priv->page_array_size * sizeof(void *));
+               memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
+                      (new_array_size - fw_priv->page_array_size));
+               kfree(fw_priv->pages);
+               fw_priv->pages = new_pages;
+               fw_priv->page_array_size = new_array_size;
+       }
 
-       if (min_size <= fw_priv->alloc_size)
-               return 0;
+       while (fw_priv->nr_pages < pages_needed) {
+               fw_priv->pages[fw_priv->nr_pages] =
+                       alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
 
-       new_size = ALIGN(min_size, PAGE_SIZE);
-       new_data = vmalloc(new_size);
-       if (!new_data) {
-               printk(KERN_ERR "%s: unable to alloc buffer\n", __func__);
-               /* Make sure that we don't keep incomplete data */
-               fw_load_abort(fw_priv);
-               return -ENOMEM;
-       }
-       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);
+               if (!fw_priv->pages[fw_priv->nr_pages]) {
+                       fw_load_abort(fw_priv);
+                       return -ENOMEM;
+               }
+               fw_priv->nr_pages++;
        }
-       fw_priv->fw->data = new_data;
-       BUG_ON(min_size > fw_priv->alloc_size);
        return 0;
 }
 
@@ -258,10 +321,25 @@ firmware_data_write(struct kobject *kobj, struct bin_attribute *bin_attr,
        if (retval)
                goto out;
 
-       memcpy((u8 *)fw->data + offset, buffer, count);
-
-       fw->size = max_t(size_t, offset + count, fw->size);
        retval = count;
+
+       while (count) {
+               void *page_data;
+               int page_nr = offset >> PAGE_SHIFT;
+               int page_ofs = offset & (PAGE_SIZE - 1);
+               int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
+
+               page_data = kmap(fw_priv->pages[page_nr]);
+
+               memcpy(page_data + page_ofs, buffer, page_cnt);
+
+               kunmap(fw_priv->pages[page_nr]);
+               buffer += page_cnt;
+               offset += page_cnt;
+               count -= page_cnt;
+       }
+
+       fw->size = max_t(size_t, offset, fw->size);
 out:
        mutex_unlock(&fw_lock);
        return retval;
@@ -277,7 +355,12 @@ static struct bin_attribute firmware_attr_data_tmpl = {
 static void fw_dev_release(struct device *dev)
 {
        struct firmware_priv *fw_priv = dev_get_drvdata(dev);
+       int i;
 
+       for (i = 0; i < fw_priv->nr_pages; i++)
+               __free_page(fw_priv->pages[i]);
+       kfree(fw_priv->pages);
+       kfree(fw_priv->fw_id);
        kfree(fw_priv);
        kfree(dev);
 
@@ -309,28 +392,35 @@ static int fw_register_device(struct device **dev_p, const char *fw_name,
 
        init_completion(&fw_priv->completion);
        fw_priv->attr_data = firmware_attr_data_tmpl;
-       strlcpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX);
+       fw_priv->fw_id = kstrdup(fw_name, GFP_KERNEL);
+       if (!fw_priv->fw_id) {
+               dev_err(device, "%s: Firmware name allocation failed\n",
+                       __func__);
+               retval = -ENOMEM;
+               goto error_kfree;
+       }
 
        fw_priv->timeout.function = firmware_class_timeout;
        fw_priv->timeout.data = (u_long) fw_priv;
        init_timer(&fw_priv->timeout);
 
-       dev_set_name(f_dev, dev_name(device));
+       dev_set_name(f_dev, "%s", dev_name(device));
        f_dev->parent = device;
        f_dev->class = &firmware_class;
        dev_set_drvdata(f_dev, fw_priv);
-       f_dev->uevent_suppress = 1;
+       dev_set_uevent_suppress(f_dev, 1);
        retval = device_register(f_dev);
        if (retval) {
                dev_err(device, "%s: device_register failed\n", __func__);
-               goto error_kfree;
+               put_device(f_dev);
+               return retval;
        }
        *dev_p = f_dev;
        return 0;
 
 error_kfree:
-       kfree(fw_priv);
        kfree(f_dev);
+       kfree(fw_priv);
        return retval;
 }
 
@@ -353,6 +443,7 @@ static int fw_setup_device(struct firmware *fw, struct device **dev_p,
        fw_priv = dev_get_drvdata(f_dev);
 
        fw_priv->fw = fw;
+       sysfs_bin_attr_init(&fw_priv->attr_data);
        retval = sysfs_create_bin_file(&f_dev->kobj, &fw_priv->attr_data);
        if (retval) {
                dev_err(device, "%s: sysfs_create_bin_file failed\n", __func__);
@@ -366,7 +457,7 @@ static int fw_setup_device(struct firmware *fw, struct device **dev_p,
        }
 
        if (uevent)
-               f_dev->uevent_suppress = 0;
+               dev_set_uevent_suppress(f_dev, 0);
        *dev_p = f_dev;
        goto out;
 
@@ -515,41 +606,40 @@ request_firmware_work_func(void *arg)
        }
        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);
-       }
+
+       fw_work->cont(fw, fw_work->context);
+
        module_put(fw_work->module);
        kfree(fw_work);
        return ret;
 }
 
 /**
- * request_firmware_nowait: asynchronous version of request_firmware
+ * 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
+ * @gfp: allocation flags
  * @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.
  *
- *     Asynchronous variant of request_firmware() for contexts where
- *     it is not possible to sleep.
+ *     Asynchronous variant of request_firmware() for user contexts where
+ *     it is not possible to sleep for long time. It can't be called
+ *     in atomic contexts.
  **/
 int
 request_firmware_nowait(
        struct module *module, int uevent,
-       const char *name, struct device *device, void *context,
+       const char *name, struct device *device, gfp_t gfp, void *context,
        void (*cont)(const struct firmware *fw, void *context))
 {
        struct task_struct *task;
        struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
-                                               GFP_ATOMIC);
+                                               gfp);
 
        if (!fw_work)
                return -ENOMEM;