usb: gadget: let f_* use usb_string_ids_tab() where it makes sense
authorSebastian Andrzej Siewior <bigeasy@linutronix.de>
Mon, 22 Oct 2012 20:15:10 +0000 (22:15 +0200)
committerFelipe Balbi <balbi@ti.com>
Wed, 31 Oct 2012 13:11:47 +0000 (15:11 +0200)
Instead of calling usb_string_id() multiple times I replace it with one
usb_string_ids_tab(). The NULL pointer in struct usb_string with "" and
are not overwritten in fail or unbind case.

The conditional assignment remains because some gadgets recycle the string
ID because the same descriptor (and string ID) is used if we have more
than one config descriptor.

Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Signed-off-by: Felipe Balbi <balbi@ti.com>
drivers/usb/gadget/f_acm.c
drivers/usb/gadget/f_ecm.c
drivers/usb/gadget/f_ncm.c
drivers/usb/gadget/f_obex.c
drivers/usb/gadget/f_rndis.c
drivers/usb/gadget/f_subset.c
drivers/usb/gadget/f_uac2.c
drivers/usb/gadget/f_uvc.c

index 308242b5d6e0ab4a9c02ea00798f2edada1421a1..549174466c2160d40003ee5787a0b9e7adaa6fca 100644 (file)
@@ -705,6 +705,7 @@ acm_unbind(struct usb_configuration *c, struct usb_function *f)
 {
        struct f_acm            *acm = func_to_acm(f);
 
+       acm_string_defs[0].id = 0;
        usb_free_all_descriptors(f);
        gs_free_req(acm->notify, acm->notify_req);
        kfree(acm);
@@ -742,27 +743,15 @@ int acm_bind_config(struct usb_configuration *c, u8 port_num)
         */
 
        /* maybe allocate device-global string IDs, and patch descriptors */
-       if (acm_string_defs[ACM_CTRL_IDX].id == 0) {
-               status = usb_string_id(c->cdev);
+       if (acm_string_defs[0].id == 0) {
+               status = usb_string_ids_tab(c->cdev, acm_string_defs);
                if (status < 0)
                        return status;
-               acm_string_defs[ACM_CTRL_IDX].id = status;
-
-               acm_control_interface_desc.iInterface = status;
-
-               status = usb_string_id(c->cdev);
-               if (status < 0)
-                       return status;
-               acm_string_defs[ACM_DATA_IDX].id = status;
-
-               acm_data_interface_desc.iInterface = status;
-
-               status = usb_string_id(c->cdev);
-               if (status < 0)
-                       return status;
-               acm_string_defs[ACM_IAD_IDX].id = status;
-
-               acm_iad_descriptor.iFunction = status;
+               acm_control_interface_desc.iInterface =
+                       acm_string_defs[ACM_CTRL_IDX].id;
+               acm_data_interface_desc.iInterface =
+                       acm_string_defs[ACM_DATA_IDX].id;
+               acm_iad_descriptor.iFunction = acm_string_defs[ACM_IAD_IDX].id;
        }
 
        /* allocate and initialize one new instance */
index 2d3c5a46de8e71d08094f83a8f774397c8c10c7f..83420a310fb71784db5b269bf39e7939ef9af1b3 100644 (file)
@@ -354,7 +354,7 @@ static struct usb_descriptor_header *ecm_ss_function[] = {
 
 static struct usb_string ecm_string_defs[] = {
        [0].s = "CDC Ethernet Control Model (ECM)",
-       [1].s = NULL /* DYNAMIC */,
+       [1].s = "",
        [2].s = "CDC Ethernet Data",
        [3].s = "CDC ECM",
        {  } /* end of list */
@@ -803,12 +803,11 @@ ecm_unbind(struct usb_configuration *c, struct usb_function *f)
 
        DBG(c->cdev, "ecm unbind\n");
 
+       ecm_string_defs[0].id = 0;
        usb_free_all_descriptors(f);
 
        kfree(ecm->notify_req->buf);
        usb_ep_free_request(ecm->notify, ecm->notify_req);
-
-       ecm_string_defs[1].s = NULL;
        kfree(ecm);
 }
 
@@ -833,36 +832,15 @@ ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
        if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
                return -EINVAL;
 
-       /* maybe allocate device-global string IDs */
        if (ecm_string_defs[0].id == 0) {
-
-               /* control interface label */
-               status = usb_string_id(c->cdev);
-               if (status < 0)
+               status = usb_string_ids_tab(c->cdev, ecm_string_defs);
+               if (status)
                        return status;
-               ecm_string_defs[0].id = status;
-               ecm_control_intf.iInterface = status;
 
-               /* data interface label */
-               status = usb_string_id(c->cdev);
-               if (status < 0)
-                       return status;
-               ecm_string_defs[2].id = status;
-               ecm_data_intf.iInterface = status;
-
-               /* MAC address */
-               status = usb_string_id(c->cdev);
-               if (status < 0)
-                       return status;
-               ecm_string_defs[1].id = status;
-               ecm_desc.iMACAddress = status;
-
-               /* IAD label */
-               status = usb_string_id(c->cdev);
-               if (status < 0)
-                       return status;
-               ecm_string_defs[3].id = status;
-               ecm_iad_descriptor.iFunction = status;
+               ecm_control_intf.iInterface = ecm_string_defs[0].id;
+               ecm_data_intf.iInterface = ecm_string_defs[2].id;
+               ecm_desc.iMACAddress = ecm_string_defs[1].id;
+               ecm_iad_descriptor.iFunction = ecm_string_defs[3].id;
        }
 
        /* allocate and initialize one new instance */
@@ -887,9 +865,7 @@ ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
        ecm->port.func.disable = ecm_disable;
 
        status = usb_add_function(c, &ecm->port.func);
-       if (status) {
-               ecm_string_defs[1].s = NULL;
+       if (status)
                kfree(ecm);
-       }
        return status;
 }
index 326d7e6c297c6939f06db85ddd64a7bdd25daf30..f1f66e9c76bafac8d0722499fd89609b9c8ffe00 100644 (file)
@@ -321,7 +321,7 @@ static struct usb_descriptor_header *ncm_hs_function[] __initdata = {
 
 static struct usb_string ncm_string_defs[] = {
        [STRING_CTRL_IDX].s = "CDC Network Control Model (NCM)",
-       [STRING_MAC_IDX].s = NULL /* DYNAMIC */,
+       [STRING_MAC_IDX].s = "",
        [STRING_DATA_IDX].s = "CDC Network Data",
        [STRING_IAD_IDX].s = "CDC NCM",
        {  } /* end of list */
@@ -1262,12 +1262,12 @@ ncm_unbind(struct usb_configuration *c, struct usb_function *f)
 
        DBG(c->cdev, "ncm unbind\n");
 
+       ncm_string_defs[0].id = 0;
        usb_free_all_descriptors(f);
 
        kfree(ncm->notify_req->buf);
        usb_ep_free_request(ncm->notify, ncm->notify_req);
 
-       ncm_string_defs[1].s = NULL;
        kfree(ncm);
 }
 
@@ -1291,37 +1291,19 @@ int __init ncm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
        if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
                return -EINVAL;
 
-       /* maybe allocate device-global string IDs */
        if (ncm_string_defs[0].id == 0) {
-
-               /* control interface label */
-               status = usb_string_id(c->cdev);
+               status = usb_string_ids_tab(c->cdev, ncm_string_defs);
                if (status < 0)
                        return status;
-               ncm_string_defs[STRING_CTRL_IDX].id = status;
-               ncm_control_intf.iInterface = status;
+               ncm_control_intf.iInterface =
+                       ncm_string_defs[STRING_CTRL_IDX].id;
 
-               /* data interface label */
-               status = usb_string_id(c->cdev);
-               if (status < 0)
-                       return status;
-               ncm_string_defs[STRING_DATA_IDX].id = status;
+               status = ncm_string_defs[STRING_DATA_IDX].id;
                ncm_data_nop_intf.iInterface = status;
                ncm_data_intf.iInterface = status;
 
-               /* MAC address */
-               status = usb_string_id(c->cdev);
-               if (status < 0)
-                       return status;
-               ncm_string_defs[STRING_MAC_IDX].id = status;
-               ecm_desc.iMACAddress = status;
-
-               /* IAD */
-               status = usb_string_id(c->cdev);
-               if (status < 0)
-                       return status;
-               ncm_string_defs[STRING_IAD_IDX].id = status;
-               ncm_iad_desc.iFunction = status;
+               ecm_desc.iMACAddress = ncm_string_defs[STRING_MAC_IDX].id;
+               ncm_iad_desc.iFunction = ncm_string_defs[STRING_IAD_IDX].id;
        }
 
        /* allocate and initialize one new instance */
@@ -1331,7 +1313,7 @@ int __init ncm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
 
        /* export host's Ethernet address in CDC format */
        snprintf(ncm->ethaddr, sizeof ncm->ethaddr, "%pm", ethaddr);
-       ncm_string_defs[1].s = ncm->ethaddr;
+       ncm_string_defs[STRING_MAC_IDX].s = ncm->ethaddr;
 
        spin_lock_init(&ncm->lock);
        ncm_reset_values(ncm);
@@ -1351,9 +1333,7 @@ int __init ncm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
        ncm->port.unwrap = ncm_unwrap_ntb;
 
        status = usb_add_function(c, &ncm->port.func);
-       if (status) {
-               ncm_string_defs[1].s = NULL;
+       if (status)
                kfree(ncm);
-       }
        return status;
 }
index d74491ad82cb1e43c613801bfd74901ecdaf6635..d8dd8782768cd6a878750e094bb88f19c222181c 100644 (file)
@@ -379,6 +379,7 @@ fail:
 static void
 obex_unbind(struct usb_configuration *c, struct usb_function *f)
 {
+       obex_string_defs[OBEX_CTRL_IDX].id = 0;
        usb_free_all_descriptors(f);
        kfree(func_to_obex(f));
 }
@@ -418,22 +419,16 @@ int __init obex_bind_config(struct usb_configuration *c, u8 port_num)
        if (!can_support_obex(c))
                return -EINVAL;
 
-       /* maybe allocate device-global string IDs, and patch descriptors */
        if (obex_string_defs[OBEX_CTRL_IDX].id == 0) {
-               status = usb_string_id(c->cdev);
+               status = usb_string_ids_tab(c->cdev, obex_string_defs);
                if (status < 0)
                        return status;
-               obex_string_defs[OBEX_CTRL_IDX].id = status;
+               obex_control_intf.iInterface =
+                       obex_string_defs[OBEX_CTRL_IDX].id;
 
-               obex_control_intf.iInterface = status;
-
-               status = usb_string_id(c->cdev);
-               if (status < 0)
-                       return status;
-               obex_string_defs[OBEX_DATA_IDX].id = status;
-
-               obex_data_nop_intf.iInterface =
-                       obex_data_intf.iInterface = status;
+               status = obex_string_defs[OBEX_DATA_IDX].id;
+               obex_data_nop_intf.iInterface = status;
+               obex_data_intf.iInterface = status;
        }
 
        /* allocate and initialize one new instance */
index e7c25105bd8ed8529a121b8fa0fa0814544a92b7..71beeb83355874b601998a5764abddc076515138 100644 (file)
@@ -795,8 +795,8 @@ rndis_unbind(struct usb_configuration *c, struct usb_function *f)
 
        rndis_deregister(rndis->config);
        rndis_exit();
-       rndis_string_defs[0].id = 0;
 
+       rndis_string_defs[0].id = 0;
        usb_free_all_descriptors(f);
 
        kfree(rndis->notify_req->buf);
@@ -822,34 +822,19 @@ rndis_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
        if (!can_support_rndis(c) || !ethaddr)
                return -EINVAL;
 
-       /* maybe allocate device-global string IDs */
        if (rndis_string_defs[0].id == 0) {
-
                /* ... and setup RNDIS itself */
                status = rndis_init();
                if (status < 0)
                        return status;
 
-               /* control interface label */
-               status = usb_string_id(c->cdev);
-               if (status < 0)
+               status = usb_string_ids_tab(c->cdev, rndis_string_defs);
+               if (status)
                        return status;
-               rndis_string_defs[0].id = status;
-               rndis_control_intf.iInterface = status;
 
-               /* data interface label */
-               status = usb_string_id(c->cdev);
-               if (status < 0)
-                       return status;
-               rndis_string_defs[1].id = status;
-               rndis_data_intf.iInterface = status;
-
-               /* IAD iFunction label */
-               status = usb_string_id(c->cdev);
-               if (status < 0)
-                       return status;
-               rndis_string_defs[2].id = status;
-               rndis_iad_descriptor.iFunction = status;
+               rndis_control_intf.iInterface = rndis_string_defs[0].id;
+               rndis_data_intf.iInterface = rndis_string_defs[1].id;
+               rndis_iad_descriptor.iFunction = rndis_string_defs[2].id;
        }
 
        /* allocate and initialize one new instance */
index 856dbae586f14dbd8be61bb1c1c64eea600a1bb6..f172bd152fbb905d34906a468e79e0422e98483b 100644 (file)
@@ -236,7 +236,7 @@ static struct usb_descriptor_header *ss_eth_function[] = {
 
 static struct usb_string geth_string_defs[] = {
        [0].s = "CDC Ethernet Subset/SAFE",
-       [1].s = NULL /* DYNAMIC */,
+       [1].s = "",
        {  } /* end of list */
 };
 
@@ -363,8 +363,8 @@ fail:
 static void
 geth_unbind(struct usb_configuration *c, struct usb_function *f)
 {
+       geth_string_defs[0].id = 0;
        usb_free_all_descriptors(f);
-       geth_string_defs[1].s = NULL;
        kfree(func_to_geth(f));
 }
 
@@ -390,20 +390,11 @@ int geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
 
        /* maybe allocate device-global string IDs */
        if (geth_string_defs[0].id == 0) {
-
-               /* interface label */
-               status = usb_string_id(c->cdev);
-               if (status < 0)
-                       return status;
-               geth_string_defs[0].id = status;
-               subset_data_intf.iInterface = status;
-
-               /* MAC address */
-               status = usb_string_id(c->cdev);
+               status = usb_string_ids_tab(c->cdev, geth_string_defs);
                if (status < 0)
                        return status;
-               geth_string_defs[1].id = status;
-               ether_desc.iMACAddress = status;
+               subset_data_intf.iInterface = geth_string_defs[0].id;
+               ether_desc.iMACAddress = geth_string_defs[1].id;
        }
 
        /* allocate and initialize one new instance */
@@ -425,9 +416,7 @@ int geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
        geth->port.func.disable = geth_disable;
 
        status = usb_add_function(c, &geth->port.func);
-       if (status) {
-               geth_string_defs[1].s = NULL;
+       if (status)
                kfree(geth);
-       }
        return status;
 }
index 2840f1819065d641254a61deeaf3ef406f662f8d..91396a1683ebd4396a2c2b0a86914e86d6fadf27 100644 (file)
@@ -1312,7 +1312,7 @@ afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
 
 static int audio_bind_config(struct usb_configuration *cfg)
 {
-       int id, res;
+       int res;
 
        agdev_g = kzalloc(sizeof *agdev_g, GFP_KERNEL);
        if (agdev_g == NULL) {
@@ -1320,89 +1320,21 @@ static int audio_bind_config(struct usb_configuration *cfg)
                return -ENOMEM;
        }
 
-       id = usb_string_id(cfg->cdev);
-       if (id < 0)
-               return id;
-
-       strings_fn[STR_ASSOC].id = id;
-       iad_desc.iFunction = id,
-
-       id = usb_string_id(cfg->cdev);
-       if (id < 0)
-               return id;
-
-       strings_fn[STR_IF_CTRL].id = id;
-       std_ac_if_desc.iInterface = id,
-
-       id = usb_string_id(cfg->cdev);
-       if (id < 0)
-               return id;
-
-       strings_fn[STR_CLKSRC_IN].id = id;
-       in_clk_src_desc.iClockSource = id,
-
-       id = usb_string_id(cfg->cdev);
-       if (id < 0)
-               return id;
-
-       strings_fn[STR_CLKSRC_OUT].id = id;
-       out_clk_src_desc.iClockSource = id,
-
-       id = usb_string_id(cfg->cdev);
-       if (id < 0)
-               return id;
-
-       strings_fn[STR_USB_IT].id = id;
-       usb_out_it_desc.iTerminal = id,
-
-       id = usb_string_id(cfg->cdev);
-       if (id < 0)
-               return id;
-
-       strings_fn[STR_IO_IT].id = id;
-       io_in_it_desc.iTerminal = id;
-
-       id = usb_string_id(cfg->cdev);
-       if (id < 0)
-               return id;
-
-       strings_fn[STR_USB_OT].id = id;
-       usb_in_ot_desc.iTerminal = id;
-
-       id = usb_string_id(cfg->cdev);
-       if (id < 0)
-               return id;
-
-       strings_fn[STR_IO_OT].id = id;
-       io_out_ot_desc.iTerminal = id;
-
-       id = usb_string_id(cfg->cdev);
-       if (id < 0)
-               return id;
-
-       strings_fn[STR_AS_OUT_ALT0].id = id;
-       std_as_out_if0_desc.iInterface = id;
-
-       id = usb_string_id(cfg->cdev);
-       if (id < 0)
-               return id;
-
-       strings_fn[STR_AS_OUT_ALT1].id = id;
-       std_as_out_if1_desc.iInterface = id;
-
-       id = usb_string_id(cfg->cdev);
-       if (id < 0)
-               return id;
-
-       strings_fn[STR_AS_IN_ALT0].id = id;
-       std_as_in_if0_desc.iInterface = id;
-
-       id = usb_string_id(cfg->cdev);
-       if (id < 0)
-               return id;
-
-       strings_fn[STR_AS_IN_ALT1].id = id;
-       std_as_in_if1_desc.iInterface = id;
+       res = usb_string_ids_tab(cfg->cdev, strings_fn);
+       if (res)
+               return res;
+       iad_desc.iFunction = strings_fn[STR_ASSOC].id;
+       std_ac_if_desc.iInterface = strings_fn[STR_IF_CTRL].id;
+       in_clk_src_desc.iClockSource = strings_fn[STR_CLKSRC_IN].id;
+       out_clk_src_desc.iClockSource = strings_fn[STR_CLKSRC_OUT].id;
+       usb_out_it_desc.iTerminal = strings_fn[STR_USB_IT].id;
+       io_in_it_desc.iTerminal = strings_fn[STR_IO_IT].id;
+       usb_in_ot_desc.iTerminal = strings_fn[STR_USB_OT].id;
+       io_out_ot_desc.iTerminal = strings_fn[STR_IO_OT].id;
+       std_as_out_if0_desc.iInterface = strings_fn[STR_AS_OUT_ALT0].id;
+       std_as_out_if1_desc.iInterface = strings_fn[STR_AS_OUT_ALT1].id;
+       std_as_in_if0_desc.iInterface = strings_fn[STR_AS_IN_ALT0].id;
+       std_as_in_if1_desc.iInterface = strings_fn[STR_AS_IN_ALT1].id;
 
        agdev_g->func.name = "uac2_func";
        agdev_g->func.strings = fn_strings;
index 28ff2546a5b3dcd5770365de2b191304468203ff..5b629876941bc282d1ceea95e3caac4c2cdd6cbf 100644 (file)
@@ -580,6 +580,7 @@ uvc_function_unbind(struct usb_configuration *c, struct usb_function *f)
        uvc->control_ep->driver_data = NULL;
        uvc->video.ep->driver_data = NULL;
 
+       uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id = 0;
        usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
        kfree(uvc->control_buf);
 
@@ -798,25 +799,16 @@ uvc_bind_config(struct usb_configuration *c,
        uvc->desc.hs_streaming = hs_streaming;
        uvc->desc.ss_streaming = ss_streaming;
 
-       /* maybe allocate device-global string IDs, and patch descriptors */
+       /* Allocate string descriptor numbers. */
        if (uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id == 0) {
-               /* Allocate string descriptor numbers. */
-               ret = usb_string_id(c->cdev);
-               if (ret < 0)
+               ret = usb_string_ids_tab(c->cdev, uvc_en_us_strings);
+               if (ret)
                        goto error;
-               uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id = ret;
-               uvc_iad.iFunction = ret;
-
-               ret = usb_string_id(c->cdev);
-               if (ret < 0)
-                       goto error;
-               uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id = ret;
-               uvc_control_intf.iInterface = ret;
-
-               ret = usb_string_id(c->cdev);
-               if (ret < 0)
-                       goto error;
-               uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id = ret;
+               uvc_iad.iFunction =
+                       uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id;
+               uvc_control_intf.iInterface =
+                       uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id;
+               ret = uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id;
                uvc_streaming_intf_alt0.iInterface = ret;
                uvc_streaming_intf_alt1.iInterface = ret;
        }