[PATCH RFC v3 6/9] staging: most: change storage class of struct mostcore

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

 



This patch allocated the mostcore structure dynamically and releases
the memory in the dedicated release function.

Signed-off-by: Christian Gromm <christian.gromm@xxxxxxxxxxxxx>
---
v3:
	This patch has been added to the series.

 drivers/staging/most/core.c | 110 ++++++++++++++++++++++++--------------------
 1 file changed, 59 insertions(+), 51 deletions(-)

diff --git a/drivers/staging/most/core.c b/drivers/staging/most/core.c
index 4f60c09..92303d0 100644
--- a/drivers/staging/most/core.c
+++ b/drivers/staging/most/core.c
@@ -33,9 +33,10 @@ static struct mostcore {
 	struct device_driver drv;
 	struct bus_type bus;
 	struct list_head comp_list;
-} mc;
+} *mc;
 
 #define to_driver(d) container_of(d, struct mostcore, drv)
+#define to_mostcore(d) container_of(d, struct mostcore, dev)
 
 struct pipe {
 	struct most_component *comp;
@@ -154,7 +155,7 @@ static void flush_channel_fifos(struct most_channel *c)
 	spin_unlock_irqrestore(&c->fifo_lock, hf_flags);
 
 	if (unlikely((!list_empty(&c->fifo) || !list_empty(&c->halt_fifo))))
-		dev_warn(&mc.dev, "fifo | trash fifo not empty\n");
+		dev_warn(&mc->dev, "fifo | trash fifo not empty\n");
 }
 
 /**
@@ -461,7 +462,7 @@ static struct most_component *match_component(char *name)
 {
 	struct most_component *comp;
 
-	list_for_each_entry(comp, &mc.comp_list, list) {
+	list_for_each_entry(comp, &mc->comp_list, list) {
 		if (!strcmp(comp->name, name))
 			return comp;
 	}
@@ -507,7 +508,7 @@ static ssize_t links_show(struct device_driver *drv, char *buf)
 {
 	struct show_links_data d = { .buf = buf };
 
-	bus_for_each_dev(&mc.bus, NULL, &d, print_links);
+	bus_for_each_dev(&mc->bus, NULL, &d, print_links);
 	return d.offs;
 }
 
@@ -516,7 +517,7 @@ static ssize_t components_show(struct device_driver *drv, char *buf)
 	struct most_component *comp;
 	int offs = 0;
 
-	list_for_each_entry(comp, &mc.comp_list, list) {
+	list_for_each_entry(comp, &mc->comp_list, list) {
 		offs += snprintf(buf + offs, PAGE_SIZE - offs, "%s\n",
 				 comp->name);
 	}
@@ -534,7 +535,7 @@ static struct most_channel *get_channel(char *mdev, char *mdev_ch)
 	struct most_interface *iface;
 	struct most_channel *c, *tmp;
 
-	dev = bus_find_device_by_name(&mc.bus, NULL, mdev);
+	dev = bus_find_device_by_name(&mc->bus, NULL, mdev);
 	if (!dev)
 		return NULL;
 	iface = to_iface_priv(dev)->iface;
@@ -626,7 +627,7 @@ int most_set_cfg_datatype(char *mdev, char *mdev_ch, char *buf)
 	}
 
 	if (i == ARRAY_SIZE(ch_data_type))
-		dev_warn(&mc.dev, "invalid attribute settings\n");
+		dev_warn(&mc->dev, "invalid attribute settings\n");
 	return 0;
 }
 
@@ -645,7 +646,7 @@ int most_set_cfg_direction(char *mdev, char *mdev_ch, char *buf)
 	} else if (!strcmp(buf, "tx")) {
 		c->cfg.direction = MOST_CH_TX;
 	} else {
-		dev_err(&mc.dev, "Invalid direction\n");
+		dev_err(&mc->dev, "Invalid direction\n");
 		return -ENODATA;
 	}
 	return 0;
@@ -798,7 +799,7 @@ static int hdm_enqueue_thread(void *data)
 		mutex_unlock(&c->nq_mutex);
 
 		if (unlikely(ret)) {
-			dev_err(&mc.dev, "hdm enqueue failed\n");
+			dev_err(&mc->dev, "hdm enqueue failed\n");
 			nq_hdm_mbo(mbo);
 			c->hdm_enqueue_task = NULL;
 			return 0;
@@ -945,7 +946,7 @@ static void most_write_completion(struct mbo *mbo)
 
 	c = mbo->context;
 	if (mbo->status == MBO_E_INVAL)
-		dev_warn(&mc.dev, "Tx MBO status: invalid\n");
+		dev_warn(&mc->dev, "Tx MBO status: invalid\n");
 	if (unlikely(c->is_poisoned || (mbo->status == MBO_E_CLOSE)))
 		trash_mbo(mbo);
 	else
@@ -1104,14 +1105,14 @@ int most_start_channel(struct most_interface *iface, int id,
 		goto out; /* already started by another component */
 
 	if (!try_module_get(iface->mod)) {
-		dev_err(&mc.dev, "failed to acquire HDM lock\n");
+		dev_err(&mc->dev, "failed to acquire HDM lock\n");
 		mutex_unlock(&c->start_mutex);
 		return -ENOLCK;
 	}
 
 	c->cfg.extra_len = 0;
 	if (c->iface->configure(c->iface, c->channel_id, &c->cfg)) {
-		dev_err(&mc.dev, "channel configuration failed. Go check settings...\n");
+		dev_err(&mc->dev, "channel configuration failed. Go check settings...\n");
 		ret = -EINVAL;
 		goto err_put_module;
 	}
@@ -1165,7 +1166,7 @@ int most_stop_channel(struct most_interface *iface, int id,
 	struct most_channel *c;
 
 	if (unlikely((!iface) || (id >= iface->num_channels) || (id < 0))) {
-		dev_err(&mc.dev, "Bad interface or index out of range\n");
+		dev_err(&mc->dev, "Bad interface or index out of range\n");
 		return -EINVAL;
 	}
 	c = iface->p->channel[id];
@@ -1185,7 +1186,7 @@ int most_stop_channel(struct most_interface *iface, int id,
 
 	c->is_poisoned = true;
 	if (c->iface->poison_channel(c->iface, c->channel_id)) {
-		dev_err(&mc.dev, "Cannot stop channel %d of mdev %s\n", c->channel_id,
+		dev_err(&mc->dev, "Cannot stop channel %d of mdev %s\n", c->channel_id,
 			c->iface->description);
 		mutex_unlock(&c->start_mutex);
 		return -EAGAIN;
@@ -1195,7 +1196,7 @@ int most_stop_channel(struct most_interface *iface, int id,
 
 #ifdef CMPL_INTERRUPTIBLE
 	if (wait_for_completion_interruptible(&c->cleanup)) {
-		dev_err(&mc.dev, "Interrupted while clean up ch %d\n", c->channel_id);
+		dev_err(&mc->dev, "Interrupted while clean up ch %d\n", c->channel_id);
 		mutex_unlock(&c->start_mutex);
 		return -EINTR;
 	}
@@ -1221,10 +1222,10 @@ EXPORT_SYMBOL_GPL(most_stop_channel);
 int most_register_component(struct most_component *comp)
 {
 	if (!comp) {
-		dev_err(&mc.dev, "Bad component\n");
+		dev_err(&mc->dev, "Bad component\n");
 		return -EINVAL;
 	}
-	list_add_tail(&comp->list, &mc.comp_list);
+	list_add_tail(&comp->list, &mc->comp_list);
 	return 0;
 }
 EXPORT_SYMBOL_GPL(most_register_component);
@@ -1254,11 +1255,11 @@ static int disconnect_channels(struct device *dev, void *data)
 int most_deregister_component(struct most_component *comp)
 {
 	if (!comp) {
-		dev_err(&mc.dev, "Bad component\n");
+		dev_err(&mc->dev, "Bad component\n");
 		return -EINVAL;
 	}
 
-	bus_for_each_dev(&mc.bus, NULL, comp, disconnect_channels);
+	bus_for_each_dev(&mc->bus, NULL, comp, disconnect_channels);
 	list_del(&comp->list);
 	return 0;
 }
@@ -1268,7 +1269,6 @@ static void release_interface_priv(struct device *dev)
 {
 	struct interface_private *p = to_iface_priv(dev);
 
-	dev_info(&mc.dev, "releasing interface dev %s...\n", dev_name(dev));
 	kfree(p);
 }
 
@@ -1276,7 +1276,6 @@ static void release_channel(struct device *dev)
 {
 	struct most_channel *c = to_channel(dev);
 
-	dev_info(&mc.dev, "releasing channel dev %s...\n", dev_name(dev));
 	kfree(c);
 }
 
@@ -1308,13 +1307,13 @@ int most_register_interface(struct most_interface *iface)
 
 	if (!iface || !iface->enqueue || !iface->configure ||
 	    !iface->poison_channel || (iface->num_channels > MAX_CHANNELS)) {
-		dev_err(&mc.dev, "Bad interface or channel overflow\n");
+		dev_err(&mc->dev, "Bad interface or channel overflow\n");
 		return -EINVAL;
 	}
 
 	id = ida_simple_get(&mdev_id, 0, 0, GFP_KERNEL);
 	if (id < 0) {
-		dev_err(&mc.dev, "Failed to alloc mdev ID\n");
+		dev_err(&mc->dev, "Failed to alloc mdev ID\n");
 		return id;
 	}
 
@@ -1329,12 +1328,12 @@ int most_register_interface(struct most_interface *iface)
 	iface->p->dev_id = id;
 	strscpy(iface->p->name, iface->description, sizeof(iface->p->name));
 	iface->p->dev.init_name = iface->p->name;
-	iface->p->dev.bus = &mc.bus;
-	iface->p->dev.parent = &mc.dev;
+	iface->p->dev.bus = &mc->bus;
+	iface->p->dev.parent = &mc->dev;
 	iface->p->dev.groups = interface_attr_groups;
 	iface->p->dev.release = release_interface_priv;
 	if (device_register(&iface->p->dev)) {
-		dev_err(&mc.dev, "registering iface->p->dev failed\n");
+		dev_err(&mc->dev, "registering iface->p->dev failed\n");
 		put_device(&iface->p->dev);
 		ida_simple_remove(&mdev_id, id);
 		return -ENOMEM;
@@ -1377,7 +1376,7 @@ int most_register_interface(struct most_interface *iface)
 		mutex_init(&c->nq_mutex);
 		list_add_tail(&c->list, &iface->p->channel_list);
 		if (device_register(&c->dev)) {
-			dev_err(&mc.dev, "registering c->dev failed\n");
+			dev_err(&mc->dev, "registering c->dev failed\n");
 			goto err_free_most_channel;
 		}
 	}
@@ -1477,53 +1476,62 @@ EXPORT_SYMBOL_GPL(most_resume_enqueue);
 
 static void release_most_sub(struct device *dev)
 {
-	dev_info(&mc.dev, "releasing most_subsystem\n");
+	struct mostcore *mc = to_mostcore(dev);
+
+	kfree(mc);
 }
 
 static int __init most_init(void)
 {
 	int err;
 
-	INIT_LIST_HEAD(&mc.comp_list);
-	ida_init(&mdev_id);
+	mc = kzalloc(sizeof(*mc), GFP_KERNEL);
+	if (!mc)
+		return -ENOMEM;
 
-	mc.bus.name = "most",
-	mc.bus.match = most_match,
-	mc.drv.name = "most_core",
-	mc.drv.bus = &mc.bus,
-	mc.drv.groups = mc_attr_groups;
+	INIT_LIST_HEAD(&mc->comp_list);
+	ida_init(&mdev_id);
 
-	err = bus_register(&mc.bus);
+	mc->bus.name = "most",
+	mc->bus.match = most_match,
+	mc->drv.name = "most_core",
+	mc->drv.bus = &mc->bus,
+	mc->drv.groups = mc_attr_groups;
+	mc->dev.init_name = "most_bus";
+	mc->dev.release = release_most_sub;
+	err = device_register(&mc->dev);
 	if (err) {
-		dev_err(&mc.dev, "Cannot register most bus\n");
+		ida_destroy(&mdev_id);
+		put_device(&mc->dev);
 		return err;
 	}
-	err = driver_register(&mc.drv);
+
+	err = bus_register(&mc->bus);
 	if (err) {
-		dev_err(&mc.dev, "Cannot register core driver\n");
-		goto err_unregister_bus;
+		dev_err(&mc->dev, "Cannot register most bus\n");
+		goto err_unregister_device;
 	}
-	mc.dev.init_name = "most_bus";
-	mc.dev.release = release_most_sub;
-	if (device_register(&mc.dev)) {
-		err = -ENOMEM;
-		goto err_unregister_driver;
+	err = driver_register(&mc->drv);
+	if (err) {
+		dev_err(&mc->dev, "Cannot register core driver\n");
+		goto err_unregister_bus;
 	}
 	configfs_init();
 	return 0;
 
-err_unregister_driver:
-	driver_unregister(&mc.drv);
 err_unregister_bus:
-	bus_unregister(&mc.bus);
+	bus_unregister(&mc->bus);
+err_unregister_device:
+	device_unregister(&mc->dev);
+	ida_destroy(&mdev_id);
 	return err;
 }
 
 static void __exit most_exit(void)
 {
-	device_unregister(&mc.dev);
-	driver_unregister(&mc.drv);
-	bus_unregister(&mc.bus);
+	driver_unregister(&mc->drv);
+	bus_unregister(&mc->bus);
+	device_unregister(&mc->dev);
 	ida_destroy(&mdev_id);
 }
 
-- 
2.7.4

_______________________________________________
devel mailing list
devel@xxxxxxxxxxxxxxxxxxxxxx
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel



[Index of Archives]     [Linux Driver Backports]     [DMA Engine]     [Linux GPIO]     [Linux SPI]     [Video for Linux]     [Linux USB Devel]     [Linux Coverity]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Yosemite Backpacking]
  Powered by Linux