Skip to content

Commit 0fb2c2a

Browse files
Michal Nazarewiczgregkh
authored andcommitted
USB: gadget: f_mass_storage: per function
Mass Storage Function (MSF) used the same descriptors for each usb_function instance (meaning usb_function::descriptors of different functions pointed to the same static area (the same was true for usb_function::hs_descriptors)). This would leads to problems if MSF were used in several USB configurations with different interface and/or endpoint numbers. Descriptors for all configurations would have interface/endpoint numbers overwritten by the values valid for the last configuration. This patch adds code that copies the descriptors each time MSF is added to USB configuration (that is for each usb_function). Signed-off-by: Michal Nazarewicz <[email protected]> Cc: Kyungmin Park <[email protected]>
1 parent dd0543e commit 0fb2c2a

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

drivers/usb/gadget/f_mass_storage.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2919,6 +2919,8 @@ static void fsg_unbind(struct usb_configuration *c, struct usb_function *f)
29192919

29202920
DBG(fsg, "unbind\n");
29212921
fsg_common_put(fsg->common);
2922+
usb_free_descriptors(fsg->function.descriptors);
2923+
usb_free_descriptors(fsg->function.hs_descriptors);
29222924
kfree(fsg);
29232925
}
29242926

@@ -2959,7 +2961,9 @@ static int __init fsg_bind(struct usb_configuration *c, struct usb_function *f)
29592961
fsg_fs_bulk_in_desc.bEndpointAddress;
29602962
fsg_hs_bulk_out_desc.bEndpointAddress =
29612963
fsg_fs_bulk_out_desc.bEndpointAddress;
2962-
f->hs_descriptors = fsg_hs_function;
2964+
f->hs_descriptors = usb_copy_descriptors(fsg_hs_function);
2965+
if (unlikely(!f->hs_descriptors))
2966+
return -ENOMEM;
29632967
}
29642968

29652969
return 0;
@@ -2991,7 +2995,11 @@ static int fsg_add(struct usb_composite_dev *cdev,
29912995

29922996
fsg->function.name = FSG_DRIVER_DESC;
29932997
fsg->function.strings = fsg_strings_array;
2994-
fsg->function.descriptors = fsg_fs_function;
2998+
fsg->function.descriptors = usb_copy_descriptors(fsg_fs_function);
2999+
if (unlikely(!fsg->function.descriptors)) {
3000+
rc = -ENOMEM;
3001+
goto error_free_fsg;
3002+
}
29953003
fsg->function.bind = fsg_bind;
29963004
fsg->function.unbind = fsg_unbind;
29973005
fsg->function.setup = fsg_setup;
@@ -3006,11 +3014,19 @@ static int fsg_add(struct usb_composite_dev *cdev,
30063014
* call to usb_add_function() was successful. */
30073015

30083016
rc = usb_add_function(c, &fsg->function);
3017+
if (unlikely(rc))
3018+
goto error_free_all;
30093019

3010-
if (likely(rc == 0))
3011-
fsg_common_get(fsg->common);
3012-
else
3013-
kfree(fsg);
3020+
fsg_common_get(fsg->common);
3021+
return 0;
3022+
3023+
error_free_all:
3024+
usb_free_descriptors(fsg->function.descriptors);
3025+
/* fsg_bind() might have copied those; or maybe not? who cares
3026+
* -- free it just in case. */
3027+
usb_free_descriptors(fsg->function.hs_descriptors);
3028+
error_free_fsg:
3029+
kfree(fsg);
30143030

30153031
return rc;
30163032
}

0 commit comments

Comments
 (0)