[PATCH v2 2/6] USB: gadget: f_hid: switch to IDR for tracking minors

From: John Keeping
Date: Mon Oct 28 2019 - 07:43:07 EST


In the following commits, we are going to move the cdev allocation up to
the module initialisation, and will look up the associated f_hidg
structure from the open operation.

Start preparing for this by switching from IDA to IDR.

Signed-off-by: John Keeping <john@xxxxxxxxxxxx>
---
v2:
- No changes

drivers/usb/gadget/function/f_hid.c | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/drivers/usb/gadget/function/f_hid.c b/drivers/usb/gadget/function/f_hid.c
index e5e18d02b77a..1eb8b545e5b4 100644
--- a/drivers/usb/gadget/function/f_hid.c
+++ b/drivers/usb/gadget/function/f_hid.c
@@ -24,8 +24,8 @@

static int major, minors;
static struct class *hidg_class;
-static DEFINE_IDA(hidg_ida);
-static DEFINE_MUTEX(hidg_ida_lock); /* protects access to hidg_ida */
+static DEFINE_IDR(hidg_idr);
+static DEFINE_MUTEX(hidg_idr_lock); /* protects access to hidg_idr */

/*-------------------------------------------------------------------------*/
/* HID gadget struct */
@@ -851,9 +851,9 @@ static inline int hidg_get_minor(void)
{
int ret;

- ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL);
+ ret = idr_alloc(&hidg_idr, NULL, 0, 0, GFP_KERNEL);
if (ret >= HIDG_MINORS) {
- ida_simple_remove(&hidg_ida, ret);
+ idr_remove(&hidg_idr, ret);
ret = -ENODEV;
}

@@ -996,7 +996,7 @@ static const struct config_item_type hid_func_type = {

static inline void hidg_put_minor(int minor)
{
- ida_simple_remove(&hidg_ida, minor);
+ idr_remove(&hidg_idr, minor);
}

static void hidg_free_inst(struct usb_function_instance *f)
@@ -1005,9 +1005,9 @@ static void hidg_free_inst(struct usb_function_instance *f)

opts = container_of(f, struct f_hid_opts, func_inst);

- mutex_lock(&hidg_ida_lock);
+ mutex_lock(&hidg_idr_lock);
hidg_put_minor(opts->minor);
- mutex_unlock(&hidg_ida_lock);
+ mutex_unlock(&hidg_idr_lock);

if (opts->report_desc_alloc)
kfree(opts->report_desc);
@@ -1027,7 +1027,7 @@ static struct usb_function_instance *hidg_alloc_inst(void)
opts->func_inst.free_func_inst = hidg_free_inst;
ret = &opts->func_inst;

- mutex_lock(&hidg_ida_lock);
+ mutex_lock(&hidg_idr_lock);

opts->minor = hidg_get_minor();
if (opts->minor < 0) {
@@ -1038,7 +1038,7 @@ static struct usb_function_instance *hidg_alloc_inst(void)
config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type);

unlock:
- mutex_unlock(&hidg_ida_lock);
+ mutex_unlock(&hidg_idr_lock);
return ret;
}

--
2.23.0