usb: gadget: f_mass_storage: create fsg_common_set_num_buffers for use in fsg_common_init
[firefly-linux-kernel-4.4.55.git] / drivers / usb / gadget / f_mass_storage.c
index 32e5ab73f74689a9059436963b37a1364b8cf577..79d989979360f5fc51b7a7750a19705eea01b28e 100644 (file)
@@ -299,6 +299,7 @@ struct fsg_common {
        unsigned int            short_packet_received:1;
        unsigned int            bad_lun_okay:1;
        unsigned int            running:1;
+       unsigned int            sysfs:1;
 
        int                     thread_wakeup_needed;
        struct completion       thread_notifier;
@@ -2642,22 +2643,126 @@ static inline int fsg_num_buffers_validate(unsigned int fsg_num_buffers)
        return -EINVAL;
 }
 
+static struct fsg_common *fsg_common_setup(struct fsg_common *common)
+{
+       if (!common) {
+               common = kzalloc(sizeof(*common), GFP_KERNEL);
+               if (!common)
+                       return ERR_PTR(-ENOMEM);
+               common->free_storage_on_release = 1;
+       } else {
+               memset(common, 0, sizeof(*common));
+               common->free_storage_on_release = 0;
+       }
+       init_rwsem(&common->filesem);
+       spin_lock_init(&common->lock);
+       kref_init(&common->ref);
+       init_completion(&common->thread_notifier);
+       init_waitqueue_head(&common->fsg_wait);
+       common->state = FSG_STATE_TERMINATED;
+
+       return common;
+}
+
+void fsg_common_set_sysfs(struct fsg_common *common, bool sysfs)
+{
+       common->sysfs = sysfs;
+}
+
+static void _fsg_common_free_buffers(struct fsg_buffhd *buffhds, unsigned n)
+{
+       if (buffhds) {
+               struct fsg_buffhd *bh = buffhds;
+               while (n--) {
+                       kfree(bh->buf);
+                       ++bh;
+               }
+               kfree(buffhds);
+       }
+}
+
+int fsg_common_set_num_buffers(struct fsg_common *common, unsigned int n)
+{
+       struct fsg_buffhd *bh, *buffhds;
+       int i, rc;
+
+       rc = fsg_num_buffers_validate(n);
+       if (rc != 0)
+               return rc;
+
+       buffhds = kcalloc(n, sizeof(*buffhds), GFP_KERNEL);
+       if (!buffhds)
+               return -ENOMEM;
+
+       /* Data buffers cyclic list */
+       bh = buffhds;
+       i = n;
+       goto buffhds_first_it;
+       do {
+               bh->next = bh + 1;
+               ++bh;
+buffhds_first_it:
+               bh->buf = kmalloc(FSG_BUFLEN, GFP_KERNEL);
+               if (unlikely(!bh->buf))
+                       goto error_release;
+       } while (--i);
+       bh->next = buffhds;
+
+       _fsg_common_free_buffers(common->buffhds, common->fsg_num_buffers);
+       common->fsg_num_buffers = n;
+       common->buffhds = buffhds;
+
+       return 0;
+
+error_release:
+       /*
+        * "buf"s pointed to by heads after n - i are NULL
+        * so releasing them won't hurt
+        */
+       _fsg_common_free_buffers(buffhds, n);
+
+       return -ENOMEM;
+}
+
+static inline void fsg_common_remove_sysfs(struct fsg_lun *lun)
+{
+       device_remove_file(&lun->dev, &dev_attr_nofua);
+       /*
+        * device_remove_file() =>
+        *
+        * here the attr (e.g. dev_attr_ro) is only used to be passed to:
+        *
+        *      sysfs_remove_file() =>
+        *
+        *      here e.g. both dev_attr_ro_cdrom and dev_attr_ro are in
+        *      the same namespace and
+        *      from here only attr->name is passed to:
+        *
+        *              sysfs_hash_and_remove()
+        *
+        *              attr->name is the same for dev_attr_ro_cdrom and
+        *              dev_attr_ro
+        *              attr->name is the same for dev_attr_file and
+        *              dev_attr_file_nonremovable
+        *
+        * so we don't differentiate between removing e.g. dev_attr_ro_cdrom
+        * and dev_attr_ro
+        */
+       device_remove_file(&lun->dev, &dev_attr_ro);
+       device_remove_file(&lun->dev, &dev_attr_file);
+}
+
 struct fsg_common *fsg_common_init(struct fsg_common *common,
                                   struct usb_composite_dev *cdev,
                                   struct fsg_config *cfg)
 {
        struct usb_gadget *gadget = cdev->gadget;
-       struct fsg_buffhd *bh;
        struct fsg_lun **curlun_it;
        struct fsg_lun_config *lcfg;
        struct usb_string *us;
        int nluns, i, rc;
        char *pathbuf;
 
-       rc = fsg_num_buffers_validate(cfg->fsg_num_buffers);
-       if (rc != 0)
-               return ERR_PTR(rc);
-
        /* Find out how many LUNs there should be */
        nluns = cfg->nluns;
        if (nluns < 1 || nluns > FSG_MAX_LUNS) {
@@ -2665,26 +2770,18 @@ struct fsg_common *fsg_common_init(struct fsg_common *common,
                return ERR_PTR(-EINVAL);
        }
 
-       /* Allocate? */
-       if (!common) {
-               common = kzalloc(sizeof *common, GFP_KERNEL);
-               if (!common)
-                       return ERR_PTR(-ENOMEM);
-               common->free_storage_on_release = 1;
-       } else {
-               memset(common, 0, sizeof *common);
-               common->free_storage_on_release = 0;
-       }
+       common = fsg_common_setup(common);
+       if (IS_ERR(common))
+               return common;
+       fsg_common_set_sysfs(common, true);
+       common->state = FSG_STATE_IDLE;
 
-       common->fsg_num_buffers = cfg->fsg_num_buffers;
-       common->buffhds = kcalloc(common->fsg_num_buffers,
-                                 sizeof *(common->buffhds), GFP_KERNEL);
-       if (!common->buffhds) {
+       rc = fsg_common_set_num_buffers(common, cfg->fsg_num_buffers);
+       if (rc) {
                if (common->free_storage_on_release)
                        kfree(common);
-               return ERR_PTR(-ENOMEM);
+               return ERR_PTR(rc);
        }
-
        common->ops = cfg->ops;
        common->private_data = cfg->private_data;
 
@@ -2712,8 +2809,6 @@ struct fsg_common *fsg_common_init(struct fsg_common *common,
        }
        common->luns = curlun_it;
 
-       init_rwsem(&common->filesem);
-
        for (i = 0, lcfg = cfg->luns; i < nluns; ++i, ++curlun_it, ++lcfg) {
                struct fsg_lun *curlun;
 
@@ -2734,6 +2829,7 @@ struct fsg_common *fsg_common_init(struct fsg_common *common,
                /* curlun->dev.driver = &fsg_driver.driver; XXX */
                dev_set_drvdata(&curlun->dev, &common->filesem);
                dev_set_name(&curlun->dev, "lun%d", i);
+               curlun->name = dev_name(&curlun->dev);
 
                rc = device_register(&curlun->dev);
                if (rc) {
@@ -2772,21 +2868,6 @@ struct fsg_common *fsg_common_init(struct fsg_common *common,
        }
        common->nluns = nluns;
 
-       /* Data buffers cyclic list */
-       bh = common->buffhds;
-       i = common->fsg_num_buffers;
-       goto buffhds_first_it;
-       do {
-               bh->next = bh + 1;
-               ++bh;
-buffhds_first_it:
-               bh->buf = kmalloc(FSG_BUFLEN, GFP_KERNEL);
-               if (unlikely(!bh->buf)) {
-                       rc = -ENOMEM;
-                       goto error_release;
-               }
-       } while (--i);
-       bh->next = common->buffhds;
 
        /* Prepare inquiryString */
        i = get_default_bcdDevice();
@@ -2806,8 +2887,6 @@ buffhds_first_it:
        common->can_stall = cfg->can_stall &&
                !(gadget_is_at91(common->gadget));
 
-       spin_lock_init(&common->lock);
-       kref_init(&common->ref);
 
        /* Tell the thread to start working */
        common->thread_task =
@@ -2816,8 +2895,6 @@ buffhds_first_it:
                rc = PTR_ERR(common->thread_task);
                goto error_release;
        }
-       init_completion(&common->thread_notifier);
-       init_waitqueue_head(&common->fsg_wait);
 
        /* Information */
        INFO(common, FSG_DRIVER_DESC ", version: " FSG_DRIVER_VERSION "\n");
@@ -2880,32 +2957,18 @@ static void fsg_common_release(struct kref *ref)
                        struct fsg_lun *lun = *lun_it;
                        if (!lun)
                                continue;
-                       device_remove_file(&lun->dev, &dev_attr_nofua);
-                       device_remove_file(&lun->dev,
-                                          lun->cdrom
-                                        ? &dev_attr_ro_cdrom
-                                        : &dev_attr_ro);
-                       device_remove_file(&lun->dev,
-                                          lun->removable
-                                        ? &dev_attr_file
-                                        : &dev_attr_file_nonremovable);
+                       if (common->sysfs)
+                               fsg_common_remove_sysfs(lun);
                        fsg_lun_close(lun);
-                       device_unregister(&lun->dev);
+                       if (common->sysfs)
+                               device_unregister(&lun->dev);
                        kfree(lun);
                }
 
                kfree(common->luns);
        }
 
-       {
-               struct fsg_buffhd *bh = common->buffhds;
-               unsigned i = common->fsg_num_buffers;
-               do {
-                       kfree(bh->buf);
-               } while (++bh, --i);
-       }
-
-       kfree(common->buffhds);
+       _fsg_common_free_buffers(common->buffhds, common->fsg_num_buffers);
        if (common->free_storage_on_release)
                kfree(common);
 }