[PATCH v3 17/19] usb/gadget: storage_common: make attribute operations more generic

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

 



Show/store methods for sysfs attributes contain code which can be used
also by configfs. Make them abstract the source the lun and rw_semaphore
are taken from.

Signed-off-by: Andrzej Pietrasiewicz <andrzej.p@xxxxxxxxxxx>
Signed-off-by: Kyungmin Park <kyungmin.park@xxxxxxxxxxx>
---
 drivers/usb/gadget/f_mass_storage.c |   67 ++++++++++++++++++++++++++++++++---
 drivers/usb/gadget/storage_common.c |   36 ++++--------------
 drivers/usb/gadget/storage_common.h |   21 ++++++-----
 3 files changed, 81 insertions(+), 43 deletions(-)

diff --git a/drivers/usb/gadget/f_mass_storage.c b/drivers/usb/gadget/f_mass_storage.c
index 0a2b3ae..aaa8349 100644
--- a/drivers/usb/gadget/f_mass_storage.c
+++ b/drivers/usb/gadget/f_mass_storage.c
@@ -2570,14 +2570,71 @@ static int fsg_main_thread(void *common_)
 
 /*************************** DEVICE ATTRIBUTES ***************************/
 
-static DEVICE_ATTR(ro, 0644, fsg_show_ro, fsg_store_ro);
-static DEVICE_ATTR(nofua, 0644, fsg_show_nofua, fsg_store_nofua);
-static DEVICE_ATTR(file, 0644, fsg_show_file, fsg_store_file);
+static ssize_t sysfs_fsg_show_ro(struct device *dev,
+				 struct device_attribute *attr,
+				 char *buf)
+{
+	struct fsg_lun		*curlun = fsg_lun_from_dev(dev);
+
+	return fsg_show_ro(curlun, buf);
+}
+
+static ssize_t sysfs_fsg_show_nofua(struct device *dev,
+				    struct device_attribute *attr,
+				    char *buf)
+{
+	struct fsg_lun		*curlun = fsg_lun_from_dev(dev);
+
+	return fsg_show_nofua(curlun, buf);
+}
+
+static ssize_t sysfs_fsg_show_file(struct device *dev,
+				   struct device_attribute *attr,
+				   char *buf)
+{
+	struct fsg_lun		*curlun = fsg_lun_from_dev(dev);
+	struct rw_semaphore	*filesem = dev_get_drvdata(dev);
+
+	return fsg_show_file(curlun, filesem, buf);
+}
+
+static ssize_t sysfs_fsg_store_ro(struct device *dev,
+				  struct device_attribute *attr,
+				  const char *buf, size_t count)
+{
+	struct fsg_lun		*curlun = fsg_lun_from_dev(dev);
+	struct rw_semaphore	*filesem = dev_get_drvdata(dev);
+
+	return fsg_store_ro(curlun, filesem, buf, count);
+}
+
+static ssize_t sysfs_fsg_store_nofua(struct device *dev,
+				     struct device_attribute *attr,
+				     const char *buf, size_t count)
+{
+	struct fsg_lun		*curlun = fsg_lun_from_dev(dev);
+
+	return fsg_store_nofua(curlun, buf, count);
+}
+
+static ssize_t sysfs_fsg_store_file(struct device *dev,
+				    struct device_attribute *attr,
+				    const char *buf, size_t count)
+{
+	struct fsg_lun		*curlun = fsg_lun_from_dev(dev);
+	struct rw_semaphore	*filesem = dev_get_drvdata(dev);
+
+	return fsg_store_file(curlun, filesem, buf, count);
+}
+
+static DEVICE_ATTR(ro, 0644, sysfs_fsg_show_ro, sysfs_fsg_store_ro);
+static DEVICE_ATTR(nofua, 0644, sysfs_fsg_show_nofua, sysfs_fsg_store_nofua);
+static DEVICE_ATTR(file, 0644, sysfs_fsg_show_file, sysfs_fsg_store_file);
 
 static struct device_attribute dev_attr_ro_cdrom =
-	__ATTR(ro, 0444, fsg_show_ro, NULL);
+	__ATTR(ro, 0444, sysfs_fsg_show_ro, NULL);
 static struct device_attribute dev_attr_file_nonremovable =
-	__ATTR(file, 0444, fsg_show_file, NULL);
+	__ATTR(file, 0444, sysfs_fsg_show_file, NULL);
 
 
 /****************************** FSG COMMON ******************************/
diff --git a/drivers/usb/gadget/storage_common.c b/drivers/usb/gadget/storage_common.c
index 942324c..ab83d11 100644
--- a/drivers/usb/gadget/storage_common.c
+++ b/drivers/usb/gadget/storage_common.c
@@ -31,11 +31,6 @@
 
 #include "storage_common.h"
 
-static inline struct fsg_lun *fsg_lun_from_dev(struct device *dev)
-{
-	return container_of(dev, struct fsg_lun, dev);
-}
-
 /* There is only one interface. */
 
 struct usb_interface_descriptor fsg_intf_desc = {
@@ -324,31 +319,23 @@ EXPORT_SYMBOL(store_cdrom_address);
 /*-------------------------------------------------------------------------*/
 
 
-ssize_t fsg_show_ro(struct device *dev, struct device_attribute *attr,
-			   char *buf)
+ssize_t fsg_show_ro(struct fsg_lun *curlun, char *buf)
 {
-	struct fsg_lun	*curlun = fsg_lun_from_dev(dev);
-
 	return sprintf(buf, "%d\n", fsg_lun_is_open(curlun)
 				  ? curlun->ro
 				  : curlun->initially_ro);
 }
 EXPORT_SYMBOL(fsg_show_ro);
 
-ssize_t fsg_show_nofua(struct device *dev, struct device_attribute *attr,
-			      char *buf)
+ssize_t fsg_show_nofua(struct fsg_lun *curlun, char *buf)
 {
-	struct fsg_lun	*curlun = fsg_lun_from_dev(dev);
-
 	return sprintf(buf, "%u\n", curlun->nofua);
 }
 EXPORT_SYMBOL(fsg_show_nofua);
 
-ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr,
-			     char *buf)
+ssize_t fsg_show_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
+		      char *buf)
 {
-	struct fsg_lun	*curlun = fsg_lun_from_dev(dev);
-	struct rw_semaphore	*filesem = dev_get_drvdata(dev);
 	char		*p;
 	ssize_t		rc;
 
@@ -373,12 +360,10 @@ ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr,
 EXPORT_SYMBOL(fsg_show_file);
 
 
-ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr,
+ssize_t fsg_store_ro(struct fsg_lun *curlun, struct rw_semaphore *filesem,
 			    const char *buf, size_t count)
 {
 	ssize_t		rc;
-	struct fsg_lun	*curlun = fsg_lun_from_dev(dev);
-	struct rw_semaphore	*filesem = dev_get_drvdata(dev);
 	unsigned	ro;
 
 	rc = kstrtouint(buf, 2, &ro);
@@ -404,11 +389,8 @@ ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr,
 }
 EXPORT_SYMBOL(fsg_store_ro);
 
-ssize_t fsg_store_nofua(struct device *dev,
-			       struct device_attribute *attr,
-			       const char *buf, size_t count)
+ssize_t fsg_store_nofua(struct fsg_lun *curlun, const char *buf, size_t count)
 {
-	struct fsg_lun	*curlun = fsg_lun_from_dev(dev);
 	unsigned	nofua;
 	int		ret;
 
@@ -426,11 +408,9 @@ ssize_t fsg_store_nofua(struct device *dev,
 }
 EXPORT_SYMBOL(fsg_store_nofua);
 
-ssize_t fsg_store_file(struct device *dev, struct device_attribute *attr,
-			      const char *buf, size_t count)
+ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
+		       const char *buf, size_t count)
 {
-	struct fsg_lun	*curlun = fsg_lun_from_dev(dev);
-	struct rw_semaphore	*filesem = dev_get_drvdata(dev);
 	int		rc = 0;
 
 	if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) {
diff --git a/drivers/usb/gadget/storage_common.h b/drivers/usb/gadget/storage_common.h
index 9955477..aa8bf94 100644
--- a/drivers/usb/gadget/storage_common.h
+++ b/drivers/usb/gadget/storage_common.h
@@ -181,6 +181,11 @@ static inline u32 get_unaligned_be24(u8 *buf)
 	return 0xffffff & (u32) get_unaligned_be32(buf - 1);
 }
 
+static inline struct fsg_lun *fsg_lun_from_dev(struct device *dev)
+{
+	return container_of(dev, struct fsg_lun, dev);
+}
+
 enum {
 	FSG_STRING_INTERFACE
 };
@@ -205,18 +210,14 @@ void fsg_lun_close(struct fsg_lun *curlun);
 int fsg_lun_open(struct fsg_lun *curlun, const char *filename);
 int fsg_lun_fsync_sub(struct fsg_lun *curlun);
 void store_cdrom_address(u8 *dest, int msf, u32 addr);
-ssize_t fsg_show_ro(struct device *dev, struct device_attribute *attr,
-		    char *buf);
-ssize_t fsg_show_nofua(struct device *dev, struct device_attribute *attr,
-		       char *buf);
-ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr,
+ssize_t fsg_show_ro(struct fsg_lun *curlun, char *buf);
+ssize_t fsg_show_nofua(struct fsg_lun *curlun, char *buf);
+ssize_t fsg_show_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
 		      char *buf);
-ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr,
+ssize_t fsg_store_ro(struct fsg_lun *curlun, struct rw_semaphore *filesem,
 		     const char *buf, size_t count);
-ssize_t fsg_store_nofua(struct device *dev,
-			struct device_attribute *attr,
-			const char *buf, size_t count);
-ssize_t fsg_store_file(struct device *dev, struct device_attribute *attr,
+ssize_t fsg_store_nofua(struct fsg_lun *curlun, const char *buf, size_t count);
+ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
 		       const char *buf, size_t count);
 
 #endif /* USB_STORAGE_COMMON_H */
-- 
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