[PATCH 9/9] usb/gadget: f_ncm: add configfs support

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

 



Signed-off-by: Andrzej Pietrasiewicz <andrzej.p@xxxxxxxxxxx>
Signed-off-by: Kyungmin Park <kyungmin.park@xxxxxxxxxxx>
---
 drivers/usb/gadget/f_ncm.c |  162 +++++++++++++++++++++++++++++++++++++++++++-
 drivers/usb/gadget/u_ncm.h |    9 +++
 2 files changed, 170 insertions(+), 1 deletions(-)

diff --git a/drivers/usb/gadget/f_ncm.c b/drivers/usb/gadget/f_ncm.c
index 0198f60..d4a6982 100644
--- a/drivers/usb/gadget/f_ncm.c
+++ b/drivers/usb/gadget/f_ncm.c
@@ -1167,9 +1167,13 @@ static int ncm_bind(struct usb_configuration *c, struct usb_function *f)
 		return -EINVAL;
 
 	ncm_opts = container_of(f->fi, struct f_ncm_opts, func_inst);
-	if (!ncm_opts->ethaddr)
+	mutex_lock(&ncm_opts->lock);
+	if (!ncm_opts->ethaddr) {
+		mutex_unlock(&ncm_opts->lock);
 		return -EINVAL;
+	}
 	gether_set_gadget(ncm_opts->net, cdev->gadget);
+	mutex_unlock(&ncm_opts->lock);
 	us = usb_gstrings_attach(cdev, ncm_strings,
 				 ARRAY_SIZE(ncm_string_defs));
 	if (IS_ERR(us))
@@ -1279,6 +1283,151 @@ fail:
 	return status;
 }
 
+static inline struct f_ncm_opts *to_f_ncm_opts(struct config_item *item)
+{
+	return container_of(to_config_group(item), struct f_ncm_opts,
+			    func_inst.group);
+}
+
+CONFIGFS_ATTR_STRUCT(f_ncm_opts);
+CONFIGFS_ATTR_OPS(f_ncm_opts);
+
+static void ncm_attr_release(struct config_item *item)
+{
+	struct f_ncm_opts *opts = to_f_ncm_opts(item);
+
+	usb_put_function_instance(&opts->func_inst);
+}
+
+static struct configfs_item_operations ncm_item_ops = {
+	.release	= ncm_attr_release,
+	.show_attribute = f_ncm_opts_attr_show,
+	.store_attribute = f_ncm_opts_attr_store,
+};
+
+static ssize_t ncm_opts_dev_addr_show(struct f_ncm_opts *opts, char *page)
+{
+	int result;
+
+	mutex_lock(&opts->lock);
+	result = gether_get_dev_addr(opts->net, page, PAGE_SIZE);
+	mutex_unlock(&opts->lock);
+
+	return result;
+}
+
+static ssize_t ncm_opts_dev_addr_store(struct f_ncm_opts *opts,
+		const char *page, size_t len)
+{
+	mutex_lock(&opts->lock);
+	if (opts->refcnt) {
+		mutex_unlock(&opts->lock);
+		return -EBUSY;
+	}
+
+	gether_set_dev_addr(opts->net, page);
+	mutex_unlock(&opts->lock);
+	return len;
+}
+
+static struct f_ncm_opts_attribute f_ncm_opts_dev_addr =
+	__CONFIGFS_ATTR(dev_addr, S_IRUGO | S_IWUSR, ncm_opts_dev_addr_show,
+			ncm_opts_dev_addr_store);
+
+static ssize_t ncm_opts_host_addr_show(struct f_ncm_opts *opts, char *page)
+{
+	int result;
+
+	mutex_lock(&opts->lock);
+	result = gether_get_host_addr(opts->net, page, PAGE_SIZE);
+	mutex_unlock(&opts->lock);
+
+	return result;
+}
+
+static ssize_t ncm_opts_host_addr_store(struct f_ncm_opts *opts,
+		const char *page, size_t len)
+{
+	mutex_lock(&opts->lock);
+	if (opts->refcnt) {
+		mutex_unlock(&opts->lock);
+		return -EBUSY;
+	}
+
+	gether_set_host_addr(opts->net, page, opts->ethaddr);
+	mutex_unlock(&opts->lock);
+	return len;
+}
+
+static struct f_ncm_opts_attribute f_ncm_opts_host_addr =
+	__CONFIGFS_ATTR(host_addr, S_IRUGO | S_IWUSR, ncm_opts_host_addr_show,
+			ncm_opts_host_addr_store);
+
+static ssize_t ncm_opts_qmult_show(struct f_ncm_opts *opts, char *page)
+{
+	unsigned qmult;
+
+	mutex_lock(&opts->lock);
+	qmult = gether_get_qmult(opts->net);
+	mutex_unlock(&opts->lock);
+	return sprintf(page, "%d", qmult);
+}
+
+static ssize_t ncm_opts_qmult_store(struct f_ncm_opts *opts,
+		const char *page, size_t len)
+{
+	u8 val;
+	int ret;
+
+	mutex_lock(&opts->lock);
+	if (opts->refcnt) {
+		ret = -EBUSY;
+		goto out;
+	}
+
+	ret = kstrtou8(page, 0, &val);
+	if (ret)
+		goto out;
+
+	gether_set_qmult(opts->net, val);
+	ret = len;
+out:
+	mutex_unlock(&opts->lock);
+	return ret;
+}
+
+static struct f_ncm_opts_attribute f_ncm_opts_qmult =
+	__CONFIGFS_ATTR(qmult, S_IRUGO | S_IWUSR, ncm_opts_qmult_show,
+			ncm_opts_qmult_store);
+
+static ssize_t ncm_opts_ifname_show(struct f_ncm_opts *opts, char *page)
+{
+	int ret;
+
+	mutex_lock(&opts->lock);
+	ret = gether_get_ifname(opts->net, page, PAGE_SIZE);
+	mutex_unlock(&opts->lock);
+
+	return ret;
+}
+
+static struct f_ncm_opts_attribute f_ncm_opts_ifname =
+	__CONFIGFS_ATTR_RO(ifname, ncm_opts_ifname_show);
+
+static struct configfs_attribute *ncm_attrs[] = {
+	&f_ncm_opts_dev_addr.attr,
+	&f_ncm_opts_host_addr.attr,
+	&f_ncm_opts_qmult.attr,
+	&f_ncm_opts_ifname.attr,
+	NULL,
+};
+
+static struct config_item_type ncm_func_type = {
+	.ct_item_ops	= &ncm_item_ops,
+	.ct_attrs	= ncm_attrs,
+	.ct_owner	= THIS_MODULE,
+};
+
 static void ncm_free_inst(struct usb_function_instance *f)
 {
 	struct f_ncm_opts *opts;
@@ -1295,20 +1444,28 @@ static struct usb_function_instance *ncm_alloc_inst(void)
 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
 	if (!opts)
 		return ERR_PTR(-ENOMEM);
+	mutex_init(&opts->lock);
 	opts->func_inst.free_func_inst = ncm_free_inst;
 	opts->net = gether_setup_default(opts->ethaddr);
 	if (IS_ERR(opts->net))
 		return ERR_PTR(PTR_ERR(opts->net));
 
+	config_group_init_type_name(&opts->func_inst.group, "", &ncm_func_type);
+
 	return &opts->func_inst;
 }
 
 static void ncm_free(struct usb_function *f)
 {
 	struct f_ncm *ncm;
+	struct f_ncm_opts *opts;
 
 	ncm = func_to_ncm(f);
+	opts = container_of(f->fi, struct f_ncm_opts, func_inst);
 	kfree(ncm);
+	mutex_lock(&opts->lock);
+	opts->refcnt--;
+	mutex_unlock(&opts->lock);
 }
 
 static void ncm_unbind(struct usb_configuration *c, struct usb_function *f)
@@ -1334,6 +1491,8 @@ struct usb_function *ncm_alloc(struct usb_function_instance *fi)
 		return ERR_PTR(-ENOMEM);
 
 	opts = container_of(fi, struct f_ncm_opts, func_inst);
+	mutex_lock(&opts->lock);
+	opts->refcnt++;
 
 	/* export host's Ethernet address in CDC format */
 	snprintf(ncm->ethaddr, sizeof(ncm->ethaddr), "%pm", opts->ethaddr);
@@ -1342,6 +1501,7 @@ struct usb_function *ncm_alloc(struct usb_function_instance *fi)
 	spin_lock_init(&ncm->lock);
 	ncm_reset_values(ncm);
 	ncm->port.ioport = netdev_priv(opts->net);
+	mutex_unlock(&opts->lock);
 	ncm->port.is_fixed = true;
 
 	ncm->port.func.name = "cdc_network";
diff --git a/drivers/usb/gadget/u_ncm.h b/drivers/usb/gadget/u_ncm.h
index bdf87d8..f1d69f7 100644
--- a/drivers/usb/gadget/u_ncm.h
+++ b/drivers/usb/gadget/u_ncm.h
@@ -22,6 +22,15 @@ struct f_ncm_opts {
 	struct usb_function_instance	func_inst;
 	u8				ethaddr[ETH_ALEN];
 	struct net_device		*net;
+
+	/*
+	 * Read/write access to configfs attributes is handled by configfs.
+	 *
+	 * This is to protect the data from concurrent access by read/write
+	 * and create symlink/remove symlink.
+	 */
+	struct mutex			lock;
+	int				refcnt;
 };
 
 #endif /* U_NCM_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