[PATCH v2 4/4] USB: Chipidea: add ci13xxx device id management

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


We simply use bitmap to manage the ids.
Max count of devices CI_DEVS_POSSIBLE is 32.

Signed-off-by: Richard Zhao <richard.zhao@xxxxxxxxxxxxx>
---
 drivers/usb/chipidea/core.c |   46 ++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 43 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/chipidea/core.c b/drivers/usb/chipidea/core.c
index 5fccb9c..63f9f07 100644
--- a/drivers/usb/chipidea/core.c
+++ b/drivers/usb/chipidea/core.c
@@ -332,15 +332,54 @@ static irqreturn_t ci_irq(int irq, void *data)
 	return ci->role == CI_ROLE_END ? ret : ci_role(ci)->irq(ci);
 }
 
+#define CI_DEVS_POSSIBLE	32
+
+static DECLARE_BITMAP(ci_devs, CI_DEVS_POSSIBLE);
+
+static int ci_get_device_id(void)
+{
+	int		id;
+
+again:
+	id = find_first_zero_bit(ci_devs, CI_DEVS_POSSIBLE);
+	if (id < CI_DEVS_POSSIBLE) {
+		int old;
+
+		old = test_and_set_bit(id, ci_devs);
+		if (old)
+			goto again;
+	} else {
+		pr_err("Chipidea: no space for new device\n");
+		id = -ENOMEM;
+	}
+
+	return id;
+}
+
+void ci_put_device_id(int id)
+{
+	int ret;
+
+	if (id < 0)
+		return;
+
+	ret = test_bit(id, ci_devs);
+	WARN(!ret, "Chipidea: ID %d not in use\n", id);
+	clear_bit(id, ci_devs);
+}
+
 struct platform_device *ci13xxx_add_device(struct device *dev,
 			struct resource *res, int nres,
 			struct ci13xxx_platform_data *platdata)
 {
 	struct platform_device *pdev;
-	int ret;
+	int id, ret;
+
+	id = ci_get_device_id();
+	if (id < 0)
+		return ERR_PTR(id);
 
-	/* FIXME: find a way to choose id */
-	pdev = platform_device_alloc("ci_hdrc", -1);
+	pdev = platform_device_alloc("ci_hdrc", id);
 	if (!pdev)
 		return ERR_PTR(-ENOMEM);
 
@@ -382,6 +421,7 @@ EXPORT_SYMBOL_GPL(ci13xxx_add_device);
 
 void ci13xxx_remove_device(struct platform_device *pdev)
 {
+	ci_put_device_id(pdev->id);
 	kfree(pdev->dev.dma_mask);
 	pdev->dev.dma_mask = NULL;
 	platform_device_put(pdev);
-- 
1.7.5.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


B and H Foto and Electronics Corp.

[Linux Media]     [Video for Linux]     [Linux Input]     [Linux Audio Users]     [Photo]     [Yosemite News]    [Yosemite Photos]    [Free Online Dating]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]     [More Archives]

Add to Google Powered by Linux