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 | 54 +++++++++++++++++++++++++++++++++++++++---- 1 files changed, 49 insertions(+), 5 deletions(-) diff --git a/drivers/usb/chipidea/core.c b/drivers/usb/chipidea/core.c index 8eccd1b..cdbe197 100644 --- a/drivers/usb/chipidea/core.c +++ b/drivers/usb/chipidea/core.c @@ -332,17 +332,58 @@ 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; - /* FIXME: find a way to choose id */ - pdev = platform_device_alloc("ci_hdrc", -1); - if (!pdev) - return ERR_PTR(-ENOMEM); + id = ci_get_device_id(); + if (id < 0) + return ERR_PTR(id); + + pdev = platform_device_alloc("ci_hdrc", id); + if (!pdev) { + ret = -ENOMEM; + goto put_id; + } pdev->dev.parent = dev; pdev->dev.dma_mask = dev->dma_mask; @@ -365,12 +406,15 @@ struct platform_device *ci13xxx_add_device(struct device *dev, err: platform_device_put(pdev); +put_id: + ci_put_device_id(id); return ERR_PTR(ret); } EXPORT_SYMBOL_GPL(ci13xxx_add_device); void ci13xxx_remove_device(struct platform_device *pdev) { + ci_put_device_id(pdev->id); platform_device_put(pdev); } EXPORT_SYMBOL_GPL(ci13xxx_remove_device); -- 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