[PATCH v5 05/15] usb/gadget: f_mass_storage: create lun handling helpers for use in fsg_common_init

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



fsg_common_init is a lengthy function. Factor portions of it out.

Signed-off-by: Andrzej Pietrasiewicz <andrzej.p@xxxxxxxxxxx>
Signed-off-by: Kyungmin Park <kyungmin.park@xxxxxxxxxxx>
---
 drivers/usb/gadget/f_mass_storage.c |   81 +++++++++++++++++++++++++++-------
 drivers/usb/gadget/f_mass_storage.h |    8 +++
 2 files changed, 72 insertions(+), 17 deletions(-)

diff --git a/drivers/usb/gadget/f_mass_storage.c b/drivers/usb/gadget/f_mass_storage.c
index 4833710..16a44e1 100644
--- a/drivers/usb/gadget/f_mass_storage.c
+++ b/drivers/usb/gadget/f_mass_storage.c
@@ -2730,6 +2730,65 @@ static inline void fsg_common_remove_sysfs(struct fsg_lun *lun)
 			   ? &dev_attr_file : &dev_attr_file_nonremovable);
 }
 
+void fsg_common_remove_lun(struct fsg_lun *lun, bool sysfs)
+{
+	if (sysfs) {
+		fsg_common_remove_sysfs(lun);
+		device_unregister(&lun->dev);
+	}
+	fsg_lun_close(lun);
+	kfree(lun->name);
+	kfree(lun);
+}
+
+void _fsg_common_remove_luns(struct fsg_common *common, int n)
+{
+	int i;
+
+	for (i = 0; i < n; ++i)
+		if (common->luns[i]) {
+			fsg_common_remove_lun(common->luns[i], common->sysfs);
+			common->luns[i] = NULL;
+		}
+}
+
+void fsg_common_remove_luns(struct fsg_common *common)
+{
+	_fsg_common_remove_luns(common, common->nluns);
+}
+
+void fsg_common_free_luns(struct fsg_common *common)
+{
+	fsg_common_remove_luns(common);
+	kfree(common->luns);
+	common->luns = NULL;
+}
+
+int fsg_common_set_nluns(struct fsg_common *common, int nluns)
+{
+	struct fsg_lun **curlun;
+
+	/* Find out how many LUNs there should be */
+	if (nluns < 1 || nluns > FSG_MAX_LUNS) {
+		pr_err("invalid number of LUNs: %u\n", nluns);
+		return -EINVAL;
+	}
+
+	curlun = kcalloc(nluns, sizeof(*curlun), GFP_KERNEL);
+	if (unlikely(!curlun))
+		return -ENOMEM;
+
+	if (common->luns)
+		fsg_common_free_luns(common);
+
+	common->luns = curlun;
+	common->nluns = nluns;
+
+	pr_info("Number of LUNs=%d\n", common->nluns);
+
+	return 0;
+}
+
 #define MAX_LUN_NAME_LEN 80
 
 struct fsg_common *fsg_common_init(struct fsg_common *common,
@@ -2743,12 +2802,6 @@ struct fsg_common *fsg_common_init(struct fsg_common *common,
 	int nluns, i, rc;
 	char *pathbuf;
 
-	/* Find out how many LUNs there should be */
-	nluns = cfg->nluns;
-	if (nluns < 1 || nluns > FSG_MAX_LUNS) {
-		dev_err(&gadget->dev, "invalid number of LUNs: %u\n", nluns);
-		return ERR_PTR(-EINVAL);
-	}
 
 	common = fsg_common_setup(common, !!common);
 	if (IS_ERR(common))
@@ -2778,17 +2831,12 @@ struct fsg_common *fsg_common_init(struct fsg_common *common,
 	}
 	fsg_intf_desc.iInterface = us[FSG_STRING_INTERFACE].id;
 
-	/*
-	 * Create the LUNs, open their backing files, and register the
-	 * LUN devices in sysfs.
-	 */
-	curlun_it = kcalloc(nluns, sizeof(*curlun_it), GFP_KERNEL);
-	if (unlikely(!curlun_it)) {
-		rc = -ENOMEM;
-		goto error_release;
-	}
-	common->luns = curlun_it;
 
+	rc = fsg_common_set_nluns(common, cfg->nluns);
+	if (rc)
+		goto error_release;
+	curlun_it = common->luns;
+	nluns = cfg->nluns;
 	for (i = 0, lcfg = cfg->luns; i < nluns; ++i, ++curlun_it, ++lcfg) {
 		struct fsg_lun *curlun;
 
@@ -2852,7 +2900,6 @@ struct fsg_common *fsg_common_init(struct fsg_common *common,
 			goto error_luns;
 		}
 	}
-	common->nluns = nluns;
 
 
 	/* Prepare inquiryString */
diff --git a/drivers/usb/gadget/f_mass_storage.h b/drivers/usb/gadget/f_mass_storage.h
index a00f51a..f98c792 100644
--- a/drivers/usb/gadget/f_mass_storage.h
+++ b/drivers/usb/gadget/f_mass_storage.h
@@ -106,6 +106,14 @@ void fsg_common_set_sysfs(struct fsg_common *common, bool sysfs);
 
 int fsg_common_set_num_buffers(struct fsg_common *common, unsigned int n);
 
+void fsg_common_remove_lun(struct fsg_lun *lun, bool sysfs);
+
+void fsg_common_remove_luns(struct fsg_common *common);
+
+void fsg_common_free_luns(struct fsg_common *common);
+
+int fsg_common_set_nluns(struct fsg_common *common, int nluns);
+
 void fsg_config_from_params(struct fsg_config *cfg,
 			    const struct fsg_module_parameters *params,
 			    unsigned int fsg_num_buffers);
-- 
1.7.0.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux