Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

usb: gadget: f_midi: allow customizing the USB MIDI interface string through configfs

When using f_midi from configfs the USB MIDI interface string is hardcoded
to 'MIDI function'.

This USB string descriptor is used by some third-party OS or software to
display the name of the MIDI device

Since we add an additional string option a new macro block was created to
factorize declarations

Signed-off-by: Victor Krawiec <victor.krawiec@arturia.com>
Reviewed-by: Takashi Iwai <tiwai@suse.de>
Link: https://patch.msgid.link/20251209164006.143219-1-victor.krawiec@arturia.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Victor Krawiec and committed by
Greg Kroah-Hartman
1c937381 4dee13db

+78 -68
+9 -8
Documentation/ABI/testing/configfs-usb-gadget-midi
··· 4 4 Description: 5 5 The attributes: 6 6 7 - ========== ==================================== 8 - index index value for the USB MIDI adapter 9 - id ID string for the USB MIDI adapter 10 - buflen MIDI buffer length 11 - qlen USB read request queue length 12 - in_ports number of MIDI input ports 13 - out_ports number of MIDI output ports 14 - ========== ==================================== 7 + ================ ==================================== 8 + index index value for the USB MIDI adapter 9 + id ID string for the USB MIDI adapter 10 + buflen MIDI buffer length 11 + qlen USB read request queue length 12 + in_ports number of MIDI input ports 13 + out_ports number of MIDI output ports 14 + interface_string USB AudioControl interface string 15 + ================ ====================================
+9 -8
Documentation/usb/gadget-testing.rst
··· 368 368 The function name to use when creating the function directory is "midi". 369 369 The MIDI function provides these attributes in its function directory: 370 370 371 - =============== ==================================== 372 - buflen MIDI buffer length 373 - id ID string for the USB MIDI adapter 374 - in_ports number of MIDI input ports 375 - index index value for the USB MIDI adapter 376 - out_ports number of MIDI output ports 377 - qlen USB read request queue length 378 - =============== ==================================== 371 + ================ ==================================== 372 + buflen MIDI buffer length 373 + id ID string for the USB MIDI adapter 374 + in_ports number of MIDI input ports 375 + index index value for the USB MIDI adapter 376 + out_ports number of MIDI output ports 377 + qlen USB read request queue length 378 + interface_string USB AudioControl interface string 379 + ================ ==================================== 379 380 380 381 Testing the MIDI function 381 382 -------------------------
+59 -51
drivers/usb/gadget/function/f_midi.c
··· 875 875 struct usb_composite_dev *cdev = c->cdev; 876 876 struct f_midi *midi = func_to_midi(f); 877 877 struct usb_string *us; 878 + struct f_midi_opts *opts; 878 879 int status, n, jack = 1, i = 0, endpoint_descriptor_index = 0; 879 880 880 881 midi->gadget = cdev->gadget; ··· 883 882 status = f_midi_register_card(midi); 884 883 if (status < 0) 885 884 goto fail_register; 885 + 886 + opts = container_of(f->fi, struct f_midi_opts, func_inst); 887 + if (opts->interface_string) 888 + midi_string_defs[STRING_FUNC_IDX].s = opts->interface_string; 886 889 887 890 /* maybe allocate device-global string ID */ 888 891 us = usb_gstrings_attach(c->cdev, midi_strings, ··· 1183 1178 \ 1184 1179 CONFIGFS_ATTR(f_midi_opts_, name); 1185 1180 1181 + #define F_MIDI_OPT_STRING(name) \ 1182 + static ssize_t f_midi_opts_##name##_show(struct config_item *item, char *page) \ 1183 + { \ 1184 + struct f_midi_opts *opts = to_f_midi_opts(item); \ 1185 + ssize_t result; \ 1186 + \ 1187 + mutex_lock(&opts->lock); \ 1188 + if (opts->name) { \ 1189 + result = strscpy(page, opts->name, PAGE_SIZE); \ 1190 + } else { \ 1191 + page[0] = 0; \ 1192 + result = 0; \ 1193 + } \ 1194 + \ 1195 + mutex_unlock(&opts->lock); \ 1196 + \ 1197 + return result; \ 1198 + } \ 1199 + \ 1200 + static ssize_t f_midi_opts_##name##_store(struct config_item *item, \ 1201 + const char *page, size_t len) \ 1202 + { \ 1203 + struct f_midi_opts *opts = to_f_midi_opts(item); \ 1204 + int ret; \ 1205 + char *c; \ 1206 + \ 1207 + mutex_lock(&opts->lock); \ 1208 + if (opts->refcnt > 1) { \ 1209 + ret = -EBUSY; \ 1210 + goto end; \ 1211 + } \ 1212 + \ 1213 + c = kstrndup(page, len, GFP_KERNEL); \ 1214 + if (!c) { \ 1215 + ret = -ENOMEM; \ 1216 + goto end; \ 1217 + } \ 1218 + kfree(opts->name); \ 1219 + opts->name = c; \ 1220 + ret = len; \ 1221 + end: \ 1222 + mutex_unlock(&opts->lock); \ 1223 + return ret; \ 1224 + } \ 1225 + \ 1226 + CONFIGFS_ATTR(f_midi_opts_, name) 1227 + 1186 1228 F_MIDI_OPT_SIGNED(index, true, SNDRV_CARDS); 1187 1229 F_MIDI_OPT(buflen, false, 0); 1188 1230 F_MIDI_OPT(qlen, false, 0); 1189 1231 F_MIDI_OPT(in_ports, true, MAX_PORTS); 1190 1232 F_MIDI_OPT(out_ports, true, MAX_PORTS); 1191 - 1192 - static ssize_t f_midi_opts_id_show(struct config_item *item, char *page) 1193 - { 1194 - struct f_midi_opts *opts = to_f_midi_opts(item); 1195 - ssize_t result; 1196 - 1197 - mutex_lock(&opts->lock); 1198 - if (opts->id) { 1199 - result = strscpy(page, opts->id, PAGE_SIZE); 1200 - } else { 1201 - page[0] = 0; 1202 - result = 0; 1203 - } 1204 - 1205 - mutex_unlock(&opts->lock); 1206 - 1207 - return result; 1208 - } 1209 - 1210 - static ssize_t f_midi_opts_id_store(struct config_item *item, 1211 - const char *page, size_t len) 1212 - { 1213 - struct f_midi_opts *opts = to_f_midi_opts(item); 1214 - int ret; 1215 - char *c; 1216 - 1217 - mutex_lock(&opts->lock); 1218 - if (opts->refcnt > 1) { 1219 - ret = -EBUSY; 1220 - goto end; 1221 - } 1222 - 1223 - c = kstrndup(page, len, GFP_KERNEL); 1224 - if (!c) { 1225 - ret = -ENOMEM; 1226 - goto end; 1227 - } 1228 - if (opts->id_allocated) 1229 - kfree(opts->id); 1230 - opts->id = c; 1231 - opts->id_allocated = true; 1232 - ret = len; 1233 - end: 1234 - mutex_unlock(&opts->lock); 1235 - return ret; 1236 - } 1237 - 1238 - CONFIGFS_ATTR(f_midi_opts_, id); 1233 + F_MIDI_OPT_STRING(id); 1234 + F_MIDI_OPT_STRING(interface_string); 1239 1235 1240 1236 static struct configfs_attribute *midi_attrs[] = { 1241 1237 &f_midi_opts_attr_index, ··· 1245 1239 &f_midi_opts_attr_in_ports, 1246 1240 &f_midi_opts_attr_out_ports, 1247 1241 &f_midi_opts_attr_id, 1242 + &f_midi_opts_attr_interface_string, 1248 1243 NULL, 1249 1244 }; 1250 1245 ··· 1269 1262 mutex_unlock(&opts->lock); 1270 1263 1271 1264 if (free) { 1272 - if (opts->id_allocated) 1273 - kfree(opts->id); 1265 + kfree(opts->id); 1266 + kfree(opts->interface_string); 1274 1267 kfree(opts); 1275 1268 } 1276 1269 } ··· 1286 1279 mutex_init(&opts->lock); 1287 1280 opts->func_inst.free_func_inst = f_midi_free_inst; 1288 1281 opts->index = SNDRV_DEFAULT_IDX1; 1289 - opts->id = SNDRV_DEFAULT_STR1; 1282 + opts->id = NULL; 1283 + opts->interface_string = NULL; 1290 1284 opts->buflen = 512; 1291 1285 opts->qlen = 32; 1292 1286 opts->in_ports = 1;
+1 -1
drivers/usb/gadget/function/u_midi.h
··· 19 19 struct usb_function_instance func_inst; 20 20 int index; 21 21 char *id; 22 - bool id_allocated; 22 + char *interface_string; 23 23 unsigned int in_ports; 24 24 unsigned int out_ports; 25 25 unsigned int buflen;