1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * f_midi2.c -- USB MIDI 2.0 class function driver 4 */ 5 6 #include <linux/device.h> 7 #include <linux/kernel.h> 8 #include <linux/module.h> 9 #include <linux/slab.h> 10 11 #include <sound/core.h> 12 #include <sound/control.h> 13 #include <sound/ump.h> 14 #include <sound/ump_msg.h> 15 #include <sound/ump_convert.h> 16 17 #include <linux/usb/ch9.h> 18 #include <linux/usb/func_utils.h> 19 #include <linux/usb/gadget.h> 20 #include <linux/usb/audio.h> 21 #include <linux/usb/midi-v2.h> 22 23 #include "u_midi2.h" 24 25 struct f_midi2; 26 struct f_midi2_ep; 27 struct f_midi2_usb_ep; 28 29 /* Context for each USB request */ 30 struct f_midi2_req_ctx { 31 struct f_midi2_usb_ep *usb_ep; /* belonging USB EP */ 32 unsigned int index; /* array index: 0-31 */ 33 struct usb_request *req; /* assigned request */ 34 }; 35 36 /* Resources for a USB Endpoint */ 37 struct f_midi2_usb_ep { 38 struct f_midi2 *card; /* belonging card */ 39 struct f_midi2_ep *ep; /* belonging UMP EP (optional) */ 40 struct usb_ep *usb_ep; /* assigned USB EP */ 41 void (*complete)(struct usb_ep *usb_ep, struct usb_request *req); 42 unsigned long free_reqs; /* bitmap for unused requests */ 43 unsigned int num_reqs; /* number of allocated requests */ 44 struct f_midi2_req_ctx *reqs; /* request context array */ 45 }; 46 47 /* Resources for UMP Function Block (and USB Group Terminal Block) */ 48 struct f_midi2_block { 49 struct f_midi2_block_info info; /* FB info, copied from configfs */ 50 struct snd_ump_block *fb; /* assigned FB */ 51 unsigned int gtb_id; /* assigned GTB id */ 52 unsigned int string_id; /* assigned string id */ 53 }; 54 55 /* Temporary buffer for altset 0 MIDI 1.0 handling */ 56 struct f_midi2_midi1_port { 57 unsigned int pending; /* pending bytes on the input buffer */ 58 u8 buf[32]; /* raw MIDI 1.0 byte input */ 59 u8 state; /* running status */ 60 u8 data[2]; /* rendered USB MIDI 1.0 packet data */ 61 }; 62 63 /* MIDI 1.0 message states */ 64 enum { 65 STATE_INITIAL = 0, /* pseudo state */ 66 STATE_1PARAM, 67 STATE_2PARAM_1, 68 STATE_2PARAM_2, 69 STATE_SYSEX_0, 70 STATE_SYSEX_1, 71 STATE_SYSEX_2, 72 STATE_REAL_TIME, 73 STATE_FINISHED, /* pseudo state */ 74 }; 75 76 /* Resources for UMP Endpoint */ 77 struct f_midi2_ep { 78 struct snd_ump_endpoint *ump; /* assigned UMP EP */ 79 struct f_midi2 *card; /* belonging MIDI 2.0 device */ 80 81 struct f_midi2_ep_info info; /* UMP EP info, copied from configfs */ 82 unsigned int num_blks; /* number of FBs */ 83 struct f_midi2_block blks[SNDRV_UMP_MAX_BLOCKS]; /* UMP FBs */ 84 85 struct f_midi2_usb_ep ep_in; /* USB MIDI EP-in */ 86 struct f_midi2_usb_ep ep_out; /* USB MIDI EP-out */ 87 88 u8 in_group_to_cable[SNDRV_UMP_MAX_GROUPS]; /* map to cable; 1-based! */ 89 }; 90 91 /* indices for USB strings */ 92 enum { 93 STR_IFACE = 0, 94 STR_GTB1 = 1, 95 }; 96 97 /* 1-based GTB id to string id */ 98 #define gtb_to_str_id(id) (STR_GTB1 + (id) - 1) 99 100 /* mapping from MIDI 1.0 cable to UMP group */ 101 struct midi1_cable_mapping { 102 struct f_midi2_ep *ep; 103 unsigned char block; 104 unsigned char group; 105 }; 106 107 /* operation mode */ 108 enum { 109 MIDI_OP_MODE_UNSET, /* no altset set yet */ 110 MIDI_OP_MODE_MIDI1, /* MIDI 1.0 (altset 0) is used */ 111 MIDI_OP_MODE_MIDI2, /* MIDI 2.0 (altset 1) is used */ 112 }; 113 114 /* Resources for MIDI 2.0 Device */ 115 struct f_midi2 { 116 struct usb_function func; 117 struct usb_gadget *gadget; 118 struct snd_card *card; 119 120 /* MIDI 1.0 in/out USB EPs */ 121 struct f_midi2_usb_ep midi1_ep_in; 122 struct f_midi2_usb_ep midi1_ep_out; 123 124 /* number of MIDI 1.0 I/O cables */ 125 unsigned int num_midi1_in; 126 unsigned int num_midi1_out; 127 128 /* conversion for MIDI 1.0 EP-in */ 129 struct f_midi2_midi1_port midi1_port[MAX_CABLES]; 130 /* conversion for MIDI 1.0 EP-out */ 131 struct ump_cvt_to_ump midi1_ump_cvt; 132 /* mapping between cables and UMP groups */ 133 struct midi1_cable_mapping in_cable_mapping[MAX_CABLES]; 134 struct midi1_cable_mapping out_cable_mapping[MAX_CABLES]; 135 136 int midi_if; /* USB MIDI interface number */ 137 int operation_mode; /* current operation mode */ 138 139 spinlock_t queue_lock; 140 141 struct f_midi2_card_info info; /* card info, copied from configfs */ 142 143 unsigned int num_eps; 144 struct f_midi2_ep midi2_eps[MAX_UMP_EPS]; 145 146 unsigned int total_blocks; /* total number of blocks of all EPs */ 147 struct usb_string *string_defs; 148 struct usb_string *strings; 149 }; 150 151 #define func_to_midi2(f) container_of(f, struct f_midi2, func) 152 153 /* convert from MIDI protocol number (1 or 2) to SNDRV_UMP_EP_INFO_PROTO_* */ 154 #define to_ump_protocol(v) (((v) & 3) << 8) 155 156 /* get EP name string */ 157 static const char *ump_ep_name(const struct f_midi2_ep *ep) 158 { 159 return ep->info.ep_name ? ep->info.ep_name : "MIDI 2.0 Gadget"; 160 } 161 162 /* get EP product ID string */ 163 static const char *ump_product_id(const struct f_midi2_ep *ep) 164 { 165 return ep->info.product_id ? ep->info.product_id : "Unique Product ID"; 166 } 167 168 /* get FB name string */ 169 static const char *ump_fb_name(const struct f_midi2_block_info *info) 170 { 171 return info->name ? info->name : "MIDI 2.0 Gadget I/O"; 172 } 173 174 static int f_midi2_copy_string(const char **dst, const char *src) 175 { 176 *dst = NULL; 177 if (!src) 178 return 0; 179 180 *dst = kstrdup(src, GFP_KERNEL); 181 return *dst ? 0 : -ENOMEM; 182 } 183 184 static void f_midi2_free_strings(struct f_midi2 *midi2) 185 { 186 int i, blk; 187 188 kfree(midi2->info.iface_name); 189 for (i = 0; i < midi2->num_eps; i++) { 190 struct f_midi2_ep *ep = &midi2->midi2_eps[i]; 191 192 kfree(ep->info.ep_name); 193 kfree(ep->info.product_id); 194 for (blk = 0; blk < ep->num_blks; blk++) 195 kfree(ep->blks[blk].info.name); 196 } 197 } 198 199 /* 200 * USB Descriptor Definitions 201 */ 202 /* GTB header descriptor */ 203 static struct usb_ms20_gr_trm_block_header_descriptor gtb_header_desc = { 204 .bLength = sizeof(gtb_header_desc), 205 .bDescriptorType = USB_DT_CS_GR_TRM_BLOCK, 206 .bDescriptorSubtype = USB_MS_GR_TRM_BLOCK_HEADER, 207 .wTotalLength = __cpu_to_le16(0x12), // to be filled 208 }; 209 210 /* GTB descriptor template: most items are replaced dynamically */ 211 static struct usb_ms20_gr_trm_block_descriptor gtb_desc = { 212 .bLength = sizeof(gtb_desc), 213 .bDescriptorType = USB_DT_CS_GR_TRM_BLOCK, 214 .bDescriptorSubtype = USB_MS_GR_TRM_BLOCK, 215 .bGrpTrmBlkID = 0x01, 216 .bGrpTrmBlkType = USB_MS_GR_TRM_BLOCK_TYPE_BIDIRECTIONAL, 217 .nGroupTrm = 0x00, 218 .nNumGroupTrm = 1, 219 .iBlockItem = 0, 220 .bMIDIProtocol = USB_MS_MIDI_PROTO_1_0_64, 221 .wMaxInputBandwidth = 0, 222 .wMaxOutputBandwidth = 0, 223 }; 224 225 DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1); 226 DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(16); 227 DECLARE_UAC_AC_HEADER_DESCRIPTOR(1); 228 DECLARE_USB_MS20_ENDPOINT_DESCRIPTOR(32); 229 230 #define EP_MAX_PACKET_INT 8 231 232 /* Audio Control Interface */ 233 static struct usb_interface_descriptor midi2_audio_if_desc = { 234 .bLength = USB_DT_INTERFACE_SIZE, 235 .bDescriptorType = USB_DT_INTERFACE, 236 .bInterfaceNumber = 0, // to be filled 237 .bNumEndpoints = 0, 238 .bInterfaceClass = USB_CLASS_AUDIO, 239 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL, 240 .bInterfaceProtocol = 0, 241 .iInterface = 0, 242 }; 243 244 static struct uac1_ac_header_descriptor_1 midi2_audio_class_desc = { 245 .bLength = 0x09, 246 .bDescriptorType = USB_DT_CS_INTERFACE, 247 .bDescriptorSubtype = 0x01, 248 .bcdADC = __cpu_to_le16(0x0100), 249 .wTotalLength = __cpu_to_le16(0x0009), 250 .bInCollection = 0x01, 251 .baInterfaceNr = { 0x01 }, // to be filled 252 }; 253 254 /* MIDI 1.0 Streaming Interface (altset 0) */ 255 static struct usb_interface_descriptor midi2_midi1_if_desc = { 256 .bLength = USB_DT_INTERFACE_SIZE, 257 .bDescriptorType = USB_DT_INTERFACE, 258 .bInterfaceNumber = 0, // to be filled 259 .bAlternateSetting = 0, 260 .bNumEndpoints = 2, // to be filled 261 .bInterfaceClass = USB_CLASS_AUDIO, 262 .bInterfaceSubClass = USB_SUBCLASS_MIDISTREAMING, 263 .bInterfaceProtocol = 0, 264 .iInterface = 0, // to be filled 265 }; 266 267 static struct usb_ms_header_descriptor midi2_midi1_class_desc = { 268 .bLength = 0x07, 269 .bDescriptorType = USB_DT_CS_INTERFACE, 270 .bDescriptorSubtype = USB_MS_HEADER, 271 .bcdMSC = __cpu_to_le16(0x0100), 272 .wTotalLength = __cpu_to_le16(0x41), // to be calculated 273 }; 274 275 /* MIDI 1.0 EP OUT */ 276 static struct usb_endpoint_descriptor midi2_midi1_ep_out_desc = { 277 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE, 278 .bDescriptorType = USB_DT_ENDPOINT, 279 .bEndpointAddress = USB_DIR_OUT | 0, // set up dynamically 280 .bmAttributes = USB_ENDPOINT_XFER_BULK, 281 }; 282 283 static struct usb_ss_ep_comp_descriptor midi2_midi1_ep_out_ss_comp_desc = { 284 .bLength = sizeof(midi2_midi1_ep_out_ss_comp_desc), 285 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 286 }; 287 288 static struct usb_ms_endpoint_descriptor_16 midi2_midi1_ep_out_class_desc = { 289 .bLength = 0x05, // to be filled 290 .bDescriptorType = USB_DT_CS_ENDPOINT, 291 .bDescriptorSubtype = USB_MS_GENERAL, 292 .bNumEmbMIDIJack = 1, 293 .baAssocJackID = { 0x01 }, 294 }; 295 296 /* MIDI 1.0 EP IN */ 297 static struct usb_endpoint_descriptor midi2_midi1_ep_in_desc = { 298 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE, 299 .bDescriptorType = USB_DT_ENDPOINT, 300 .bEndpointAddress = USB_DIR_IN | 0, // set up dynamically 301 .bmAttributes = USB_ENDPOINT_XFER_BULK, 302 }; 303 304 static struct usb_ss_ep_comp_descriptor midi2_midi1_ep_in_ss_comp_desc = { 305 .bLength = sizeof(midi2_midi1_ep_in_ss_comp_desc), 306 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 307 }; 308 309 static struct usb_ms_endpoint_descriptor_16 midi2_midi1_ep_in_class_desc = { 310 .bLength = 0x05, // to be filled 311 .bDescriptorType = USB_DT_CS_ENDPOINT, 312 .bDescriptorSubtype = USB_MS_GENERAL, 313 .bNumEmbMIDIJack = 1, 314 .baAssocJackID = { 0x03 }, 315 }; 316 317 /* MIDI 2.0 Streaming Interface (altset 1) */ 318 static struct usb_interface_descriptor midi2_midi2_if_desc = { 319 .bLength = USB_DT_INTERFACE_SIZE, 320 .bDescriptorType = USB_DT_INTERFACE, 321 .bInterfaceNumber = 0, // to be filled 322 .bAlternateSetting = 1, 323 .bNumEndpoints = 2, // to be filled 324 .bInterfaceClass = USB_CLASS_AUDIO, 325 .bInterfaceSubClass = USB_SUBCLASS_MIDISTREAMING, 326 .bInterfaceProtocol = 0, 327 .iInterface = 0, // to be filled 328 }; 329 330 static struct usb_ms_header_descriptor midi2_midi2_class_desc = { 331 .bLength = 0x07, 332 .bDescriptorType = USB_DT_CS_INTERFACE, 333 .bDescriptorSubtype = USB_MS_HEADER, 334 .bcdMSC = __cpu_to_le16(0x0200), 335 .wTotalLength = __cpu_to_le16(0x07), 336 }; 337 338 /* MIDI 2.0 EP OUT */ 339 static struct usb_endpoint_descriptor midi2_midi2_ep_out_desc[MAX_UMP_EPS]; 340 341 static struct usb_ss_ep_comp_descriptor midi2_midi2_ep_out_ss_comp_desc = { 342 .bLength = sizeof(midi2_midi1_ep_out_ss_comp_desc), 343 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 344 }; 345 346 static struct usb_ms20_endpoint_descriptor_32 midi2_midi2_ep_out_class_desc[MAX_UMP_EPS]; 347 348 /* MIDI 2.0 EP IN */ 349 static struct usb_endpoint_descriptor midi2_midi2_ep_in_desc[MAX_UMP_EPS]; 350 351 static struct usb_ss_ep_comp_descriptor midi2_midi2_ep_in_ss_comp_desc = { 352 .bLength = sizeof(midi2_midi2_ep_in_ss_comp_desc), 353 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 354 }; 355 356 static struct usb_ms20_endpoint_descriptor_32 midi2_midi2_ep_in_class_desc[MAX_UMP_EPS]; 357 358 /* Arrays of descriptors to be created */ 359 static void *midi2_audio_descs[] = { 360 &midi2_audio_if_desc, 361 &midi2_audio_class_desc, 362 NULL 363 }; 364 365 static void *midi2_midi1_descs[] = { 366 &midi2_midi1_if_desc, 367 &midi2_midi1_class_desc, 368 NULL 369 }; 370 371 static void *midi2_midi1_ep_out_descs[] = { 372 &midi2_midi1_ep_out_desc, 373 &midi2_midi1_ep_out_class_desc, 374 NULL 375 }; 376 377 static void *midi2_midi1_ep_in_descs[] = { 378 &midi2_midi1_ep_in_desc, 379 &midi2_midi1_ep_in_class_desc, 380 NULL 381 }; 382 383 static void *midi2_midi1_ep_out_ss_descs[] = { 384 &midi2_midi1_ep_out_desc, 385 &midi2_midi1_ep_out_ss_comp_desc, 386 &midi2_midi1_ep_out_class_desc, 387 NULL 388 }; 389 390 static void *midi2_midi1_ep_in_ss_descs[] = { 391 &midi2_midi1_ep_in_desc, 392 &midi2_midi1_ep_in_ss_comp_desc, 393 &midi2_midi1_ep_in_class_desc, 394 NULL 395 }; 396 397 static void *midi2_midi2_descs[] = { 398 &midi2_midi2_if_desc, 399 &midi2_midi2_class_desc, 400 NULL 401 }; 402 403 /* 404 * USB request handling 405 */ 406 407 /* get an empty request for the given EP */ 408 static struct usb_request *get_empty_request(struct f_midi2_usb_ep *usb_ep) 409 { 410 struct usb_request *req = NULL; 411 unsigned long flags; 412 int index; 413 414 spin_lock_irqsave(&usb_ep->card->queue_lock, flags); 415 if (!usb_ep->free_reqs) 416 goto unlock; 417 index = find_first_bit(&usb_ep->free_reqs, usb_ep->num_reqs); 418 if (index >= usb_ep->num_reqs) 419 goto unlock; 420 req = usb_ep->reqs[index].req; 421 if (!req) 422 goto unlock; 423 clear_bit(index, &usb_ep->free_reqs); 424 req->length = 0; 425 unlock: 426 spin_unlock_irqrestore(&usb_ep->card->queue_lock, flags); 427 return req; 428 } 429 430 /* put the empty request back */ 431 static void put_empty_request(struct usb_request *req) 432 { 433 struct f_midi2_req_ctx *ctx = req->context; 434 unsigned long flags; 435 436 spin_lock_irqsave(&ctx->usb_ep->card->queue_lock, flags); 437 set_bit(ctx->index, &ctx->usb_ep->free_reqs); 438 spin_unlock_irqrestore(&ctx->usb_ep->card->queue_lock, flags); 439 } 440 441 /* 442 * UMP v1.1 Stream message handling 443 */ 444 445 /* queue a request to UMP EP; request is either queued or freed after this */ 446 static int queue_request_ep_raw(struct usb_request *req) 447 { 448 struct f_midi2_req_ctx *ctx = req->context; 449 int err; 450 451 req->complete = ctx->usb_ep->complete; 452 err = usb_ep_queue(ctx->usb_ep->usb_ep, req, GFP_ATOMIC); 453 if (err) { 454 put_empty_request(req); 455 return err; 456 } 457 return 0; 458 } 459 460 /* queue a request with endianness conversion */ 461 static int queue_request_ep_in(struct usb_request *req) 462 { 463 /* UMP packets have to be converted to little-endian */ 464 cpu_to_le32_array((u32 *)req->buf, req->length >> 2); 465 return queue_request_ep_raw(req); 466 } 467 468 /* reply a UMP packet via EP-in */ 469 static int reply_ep_in(struct f_midi2_ep *ep, const void *buf, int len) 470 { 471 struct f_midi2_usb_ep *usb_ep = &ep->ep_in; 472 struct usb_request *req; 473 474 req = get_empty_request(usb_ep); 475 if (!req) 476 return -ENOSPC; 477 478 req->length = len; 479 memcpy(req->buf, buf, len); 480 return queue_request_ep_in(req); 481 } 482 483 /* reply a UMP stream EP info */ 484 static void reply_ump_stream_ep_info(struct f_midi2_ep *ep) 485 { 486 struct snd_ump_stream_msg_ep_info rep = { 487 .type = UMP_MSG_TYPE_STREAM, 488 .status = UMP_STREAM_MSG_STATUS_EP_INFO, 489 .ump_version_major = 0x01, 490 .ump_version_minor = 0x01, 491 .num_function_blocks = ep->num_blks, 492 .static_function_block = !!ep->card->info.static_block, 493 .protocol = (UMP_STREAM_MSG_EP_INFO_CAP_MIDI1 | 494 UMP_STREAM_MSG_EP_INFO_CAP_MIDI2) >> 8, 495 }; 496 497 reply_ep_in(ep, &rep, sizeof(rep)); 498 } 499 500 /* reply a UMP EP device info */ 501 static void reply_ump_stream_ep_device(struct f_midi2_ep *ep) 502 { 503 struct snd_ump_stream_msg_device_info rep = { 504 .type = UMP_MSG_TYPE_STREAM, 505 .status = UMP_STREAM_MSG_STATUS_DEVICE_INFO, 506 .manufacture_id = ep->info.manufacturer, 507 .family_lsb = ep->info.family & 0xff, 508 .family_msb = (ep->info.family >> 8) & 0xff, 509 .model_lsb = ep->info.model & 0xff, 510 .model_msb = (ep->info.model >> 8) & 0xff, 511 .sw_revision = ep->info.sw_revision, 512 }; 513 514 reply_ep_in(ep, &rep, sizeof(rep)); 515 } 516 517 #define UMP_STREAM_PKT_BYTES 16 /* UMP stream packet size = 16 bytes*/ 518 #define UMP_STREAM_EP_STR_OFF 2 /* offset of name string for EP info */ 519 #define UMP_STREAM_FB_STR_OFF 3 /* offset of name string for FB info */ 520 521 /* Helper to replay a string */ 522 static void reply_ump_stream_string(struct f_midi2_ep *ep, const u8 *name, 523 unsigned int type, unsigned int extra, 524 unsigned int start_ofs) 525 { 526 struct f_midi2_usb_ep *usb_ep = &ep->ep_in; 527 struct f_midi2 *midi2 = ep->card; 528 struct usb_request *req; 529 unsigned int pos; 530 u32 *buf; 531 532 if (!*name) 533 return; 534 req = get_empty_request(usb_ep); 535 if (!req) 536 return; 537 538 buf = (u32 *)req->buf; 539 pos = start_ofs; 540 for (;;) { 541 if (pos == start_ofs) { 542 memset(buf, 0, UMP_STREAM_PKT_BYTES); 543 buf[0] = ump_stream_compose(type, 0) | extra; 544 } 545 buf[pos / 4] |= *name++ << ((3 - (pos % 4)) * 8); 546 if (!*name) { 547 if (req->length) 548 buf[0] |= UMP_STREAM_MSG_FORMAT_END << 26; 549 req->length += UMP_STREAM_PKT_BYTES; 550 break; 551 } 552 if (++pos == UMP_STREAM_PKT_BYTES) { 553 if (!req->length) 554 buf[0] |= UMP_STREAM_MSG_FORMAT_START << 26; 555 else 556 buf[0] |= UMP_STREAM_MSG_FORMAT_CONTINUE << 26; 557 req->length += UMP_STREAM_PKT_BYTES; 558 if (midi2->info.req_buf_size - req->length < UMP_STREAM_PKT_BYTES) 559 break; 560 buf += 4; 561 pos = start_ofs; 562 } 563 } 564 565 if (req->length) 566 queue_request_ep_in(req); 567 else 568 put_empty_request(req); 569 } 570 571 /* Reply a UMP EP name string */ 572 static void reply_ump_stream_ep_name(struct f_midi2_ep *ep) 573 { 574 reply_ump_stream_string(ep, ump_ep_name(ep), 575 UMP_STREAM_MSG_STATUS_EP_NAME, 0, 576 UMP_STREAM_EP_STR_OFF); 577 } 578 579 /* Reply a UMP EP product ID string */ 580 static void reply_ump_stream_ep_pid(struct f_midi2_ep *ep) 581 { 582 reply_ump_stream_string(ep, ump_product_id(ep), 583 UMP_STREAM_MSG_STATUS_PRODUCT_ID, 0, 584 UMP_STREAM_EP_STR_OFF); 585 } 586 587 /* Reply a UMP EP stream config */ 588 static void reply_ump_stream_ep_config(struct f_midi2_ep *ep) 589 { 590 struct snd_ump_stream_msg_stream_cfg rep = { 591 .type = UMP_MSG_TYPE_STREAM, 592 .status = UMP_STREAM_MSG_STATUS_STREAM_CFG, 593 }; 594 595 if (ep->info.protocol == 2) 596 rep.protocol = UMP_STREAM_MSG_EP_INFO_CAP_MIDI2 >> 8; 597 else 598 rep.protocol = UMP_STREAM_MSG_EP_INFO_CAP_MIDI1 >> 8; 599 600 reply_ep_in(ep, &rep, sizeof(rep)); 601 } 602 603 /* Reply a UMP FB info */ 604 static void reply_ump_stream_fb_info(struct f_midi2_ep *ep, int blk) 605 { 606 struct f_midi2_block_info *b = &ep->blks[blk].info; 607 struct snd_ump_stream_msg_fb_info rep = { 608 .type = UMP_MSG_TYPE_STREAM, 609 .status = UMP_STREAM_MSG_STATUS_FB_INFO, 610 .active = !!b->active, 611 .function_block_id = blk, 612 .ui_hint = b->ui_hint, 613 .midi_10 = b->is_midi1, 614 .direction = b->direction, 615 .first_group = b->first_group, 616 .num_groups = b->num_groups, 617 .midi_ci_version = b->midi_ci_version, 618 .sysex8_streams = b->sysex8_streams, 619 }; 620 621 reply_ep_in(ep, &rep, sizeof(rep)); 622 } 623 624 /* Reply a FB name string */ 625 static void reply_ump_stream_fb_name(struct f_midi2_ep *ep, unsigned int blk) 626 { 627 reply_ump_stream_string(ep, ump_fb_name(&ep->blks[blk].info), 628 UMP_STREAM_MSG_STATUS_FB_NAME, blk << 8, 629 UMP_STREAM_FB_STR_OFF); 630 } 631 632 /* Process a UMP Stream message */ 633 static void process_ump_stream_msg(struct f_midi2_ep *ep, const u32 *data) 634 { 635 struct f_midi2 *midi2 = ep->card; 636 unsigned int format, status, blk; 637 638 format = ump_stream_message_format(*data); 639 status = ump_stream_message_status(*data); 640 switch (status) { 641 case UMP_STREAM_MSG_STATUS_EP_DISCOVERY: 642 if (format) 643 return; // invalid 644 if (data[1] & UMP_STREAM_MSG_REQUEST_EP_INFO) 645 reply_ump_stream_ep_info(ep); 646 if (data[1] & UMP_STREAM_MSG_REQUEST_DEVICE_INFO) 647 reply_ump_stream_ep_device(ep); 648 if (data[1] & UMP_STREAM_MSG_REQUEST_EP_NAME) 649 reply_ump_stream_ep_name(ep); 650 if (data[1] & UMP_STREAM_MSG_REQUEST_PRODUCT_ID) 651 reply_ump_stream_ep_pid(ep); 652 if (data[1] & UMP_STREAM_MSG_REQUEST_STREAM_CFG) 653 reply_ump_stream_ep_config(ep); 654 return; 655 case UMP_STREAM_MSG_STATUS_STREAM_CFG_REQUEST: 656 if (*data & UMP_STREAM_MSG_EP_INFO_CAP_MIDI2) { 657 ep->info.protocol = 2; 658 DBG(midi2, "Switching Protocol to MIDI2\n"); 659 } else { 660 ep->info.protocol = 1; 661 DBG(midi2, "Switching Protocol to MIDI1\n"); 662 } 663 snd_ump_switch_protocol(ep->ump, to_ump_protocol(ep->info.protocol)); 664 reply_ump_stream_ep_config(ep); 665 return; 666 case UMP_STREAM_MSG_STATUS_FB_DISCOVERY: 667 if (format) 668 return; // invalid 669 blk = (*data >> 8) & 0xff; 670 if (blk == 0xff) { 671 /* inquiry for all blocks */ 672 for (blk = 0; blk < ep->num_blks; blk++) { 673 if (*data & UMP_STREAM_MSG_REQUEST_FB_INFO) 674 reply_ump_stream_fb_info(ep, blk); 675 if (*data & UMP_STREAM_MSG_REQUEST_FB_NAME) 676 reply_ump_stream_fb_name(ep, blk); 677 } 678 } else if (blk < ep->num_blks) { 679 /* only the specified block */ 680 if (*data & UMP_STREAM_MSG_REQUEST_FB_INFO) 681 reply_ump_stream_fb_info(ep, blk); 682 if (*data & UMP_STREAM_MSG_REQUEST_FB_NAME) 683 reply_ump_stream_fb_name(ep, blk); 684 } 685 return; 686 } 687 } 688 689 /* Process UMP messages included in a USB request */ 690 static void process_ump(struct f_midi2_ep *ep, const struct usb_request *req) 691 { 692 const u32 *data = (u32 *)req->buf; 693 int len = req->actual >> 2; 694 const u32 *in_buf = ep->ump->input_buf; 695 696 for (; len > 0; len--, data++) { 697 if (snd_ump_receive_ump_val(ep->ump, *data) <= 0) 698 continue; 699 if (ump_message_type(*in_buf) == UMP_MSG_TYPE_STREAM) 700 process_ump_stream_msg(ep, in_buf); 701 } 702 } 703 704 /* 705 * MIDI 2.0 UMP USB request handling 706 */ 707 708 /* complete handler for UMP EP-out requests */ 709 static void f_midi2_ep_out_complete(struct usb_ep *usb_ep, 710 struct usb_request *req) 711 { 712 struct f_midi2_req_ctx *ctx = req->context; 713 struct f_midi2_ep *ep = ctx->usb_ep->ep; 714 struct f_midi2 *midi2 = ep->card; 715 int status = req->status; 716 717 if (status) { 718 DBG(midi2, "%s complete error %d: %d/%d\n", 719 usb_ep->name, status, req->actual, req->length); 720 goto error; 721 } 722 723 /* convert to UMP packet in native endianness */ 724 le32_to_cpu_array((u32 *)req->buf, req->actual >> 2); 725 726 if (midi2->info.process_ump) 727 process_ump(ep, req); 728 729 snd_ump_receive(ep->ump, req->buf, req->actual & ~3); 730 731 if (midi2->operation_mode != MIDI_OP_MODE_MIDI2) 732 goto error; 733 734 if (queue_request_ep_raw(req)) 735 goto error; 736 return; 737 738 error: 739 put_empty_request(req); 740 } 741 742 /* Transmit UMP packets received from user-space to the gadget */ 743 static void process_ump_transmit(struct f_midi2_ep *ep) 744 { 745 struct f_midi2_usb_ep *usb_ep = &ep->ep_in; 746 struct f_midi2 *midi2 = ep->card; 747 struct usb_request *req; 748 int len; 749 750 if (!usb_ep->usb_ep->enabled) 751 return; 752 753 for (;;) { 754 req = get_empty_request(usb_ep); 755 if (!req) 756 break; 757 len = snd_ump_transmit(ep->ump, (u32 *)req->buf, 758 midi2->info.req_buf_size); 759 if (len <= 0) { 760 put_empty_request(req); 761 break; 762 } 763 764 req->length = len; 765 if (queue_request_ep_in(req) < 0) 766 break; 767 } 768 } 769 770 /* Complete handler for UMP EP-in requests */ 771 static void f_midi2_ep_in_complete(struct usb_ep *usb_ep, 772 struct usb_request *req) 773 { 774 struct f_midi2_req_ctx *ctx = req->context; 775 struct f_midi2_ep *ep = ctx->usb_ep->ep; 776 struct f_midi2 *midi2 = ep->card; 777 int status = req->status; 778 779 put_empty_request(req); 780 781 if (status) { 782 DBG(midi2, "%s complete error %d: %d/%d\n", 783 usb_ep->name, status, req->actual, req->length); 784 return; 785 } 786 787 process_ump_transmit(ep); 788 } 789 790 /* 791 * MIDI1 (altset 0) USB request handling 792 */ 793 794 /* process one MIDI byte -- copied from f_midi.c 795 * 796 * fill the packet or request if needed 797 * returns true if the request became empty (queued) 798 */ 799 static bool process_midi1_byte(struct f_midi2 *midi2, u8 cable, u8 b, 800 struct usb_request **req_p) 801 { 802 struct f_midi2_midi1_port *port = &midi2->midi1_port[cable]; 803 u8 p[4] = { cable << 4, 0, 0, 0 }; 804 int next_state = STATE_INITIAL; 805 struct usb_request *req = *req_p; 806 807 switch (b) { 808 case 0xf8 ... 0xff: 809 /* System Real-Time Messages */ 810 p[0] |= 0x0f; 811 p[1] = b; 812 next_state = port->state; 813 port->state = STATE_REAL_TIME; 814 break; 815 816 case 0xf7: 817 /* End of SysEx */ 818 switch (port->state) { 819 case STATE_SYSEX_0: 820 p[0] |= 0x05; 821 p[1] = 0xf7; 822 next_state = STATE_FINISHED; 823 break; 824 case STATE_SYSEX_1: 825 p[0] |= 0x06; 826 p[1] = port->data[0]; 827 p[2] = 0xf7; 828 next_state = STATE_FINISHED; 829 break; 830 case STATE_SYSEX_2: 831 p[0] |= 0x07; 832 p[1] = port->data[0]; 833 p[2] = port->data[1]; 834 p[3] = 0xf7; 835 next_state = STATE_FINISHED; 836 break; 837 default: 838 /* Ignore byte */ 839 next_state = port->state; 840 port->state = STATE_INITIAL; 841 } 842 break; 843 844 case 0xf0 ... 0xf6: 845 /* System Common Messages */ 846 port->data[0] = port->data[1] = 0; 847 port->state = STATE_INITIAL; 848 switch (b) { 849 case 0xf0: 850 port->data[0] = b; 851 port->data[1] = 0; 852 next_state = STATE_SYSEX_1; 853 break; 854 case 0xf1: 855 case 0xf3: 856 port->data[0] = b; 857 next_state = STATE_1PARAM; 858 break; 859 case 0xf2: 860 port->data[0] = b; 861 next_state = STATE_2PARAM_1; 862 break; 863 case 0xf4: 864 case 0xf5: 865 next_state = STATE_INITIAL; 866 break; 867 case 0xf6: 868 p[0] |= 0x05; 869 p[1] = 0xf6; 870 next_state = STATE_FINISHED; 871 break; 872 } 873 break; 874 875 case 0x80 ... 0xef: 876 /* 877 * Channel Voice Messages, Channel Mode Messages 878 * and Control Change Messages. 879 */ 880 port->data[0] = b; 881 port->data[1] = 0; 882 port->state = STATE_INITIAL; 883 if (b >= 0xc0 && b <= 0xdf) 884 next_state = STATE_1PARAM; 885 else 886 next_state = STATE_2PARAM_1; 887 break; 888 889 case 0x00 ... 0x7f: 890 /* Message parameters */ 891 switch (port->state) { 892 case STATE_1PARAM: 893 if (port->data[0] < 0xf0) 894 p[0] |= port->data[0] >> 4; 895 else 896 p[0] |= 0x02; 897 898 p[1] = port->data[0]; 899 p[2] = b; 900 /* This is to allow Running State Messages */ 901 next_state = STATE_1PARAM; 902 break; 903 case STATE_2PARAM_1: 904 port->data[1] = b; 905 next_state = STATE_2PARAM_2; 906 break; 907 case STATE_2PARAM_2: 908 if (port->data[0] < 0xf0) 909 p[0] |= port->data[0] >> 4; 910 else 911 p[0] |= 0x03; 912 913 p[1] = port->data[0]; 914 p[2] = port->data[1]; 915 p[3] = b; 916 /* This is to allow Running State Messages */ 917 next_state = STATE_2PARAM_1; 918 break; 919 case STATE_SYSEX_0: 920 port->data[0] = b; 921 next_state = STATE_SYSEX_1; 922 break; 923 case STATE_SYSEX_1: 924 port->data[1] = b; 925 next_state = STATE_SYSEX_2; 926 break; 927 case STATE_SYSEX_2: 928 p[0] |= 0x04; 929 p[1] = port->data[0]; 930 p[2] = port->data[1]; 931 p[3] = b; 932 next_state = STATE_SYSEX_0; 933 break; 934 } 935 break; 936 } 937 938 /* States where we have to write into the USB request */ 939 if (next_state == STATE_FINISHED || 940 port->state == STATE_SYSEX_2 || 941 port->state == STATE_1PARAM || 942 port->state == STATE_2PARAM_2 || 943 port->state == STATE_REAL_TIME) { 944 memcpy(req->buf + req->length, p, sizeof(p)); 945 req->length += sizeof(p); 946 947 if (next_state == STATE_FINISHED) { 948 next_state = STATE_INITIAL; 949 port->data[0] = port->data[1] = 0; 950 } 951 952 if (midi2->info.req_buf_size - req->length <= 4) { 953 queue_request_ep_raw(req); 954 *req_p = NULL; 955 return true; 956 } 957 } 958 959 port->state = next_state; 960 return false; 961 } 962 963 /* process all pending MIDI bytes in the internal buffer; 964 * returns true if the request gets empty 965 * returns false if all have been processed 966 */ 967 static bool process_midi1_pending_buf(struct f_midi2 *midi2, 968 struct usb_request **req_p) 969 { 970 unsigned int cable, c; 971 972 for (cable = 0; cable < midi2->num_midi1_in; cable++) { 973 struct f_midi2_midi1_port *port = &midi2->midi1_port[cable]; 974 975 if (!port->pending) 976 continue; 977 for (c = 0; c < port->pending; c++) { 978 if (process_midi1_byte(midi2, cable, port->buf[c], 979 req_p)) { 980 port->pending -= c; 981 if (port->pending) 982 memmove(port->buf, port->buf + c, 983 port->pending); 984 return true; 985 } 986 } 987 port->pending = 0; 988 } 989 990 return false; 991 } 992 993 /* fill the MIDI bytes onto the temporary buffer 994 */ 995 static void fill_midi1_pending_buf(struct f_midi2 *midi2, u8 cable, u8 *buf, 996 unsigned int size) 997 { 998 struct f_midi2_midi1_port *port = &midi2->midi1_port[cable]; 999 1000 if (port->pending + size > sizeof(port->buf)) 1001 return; 1002 memcpy(port->buf + port->pending, buf, size); 1003 port->pending += size; 1004 } 1005 1006 /* try to process data given from the associated UMP stream */ 1007 static void process_midi1_transmit(struct f_midi2 *midi2) 1008 { 1009 struct f_midi2_usb_ep *usb_ep = &midi2->midi1_ep_in; 1010 struct f_midi2_ep *ep = &midi2->midi2_eps[0]; 1011 struct usb_request *req = NULL; 1012 /* 12 is the largest outcome (4 MIDI1 cmds) for a single UMP packet */ 1013 unsigned char outbuf[12]; 1014 unsigned char group, cable; 1015 int len, size; 1016 u32 ump; 1017 1018 if (!usb_ep->usb_ep || !usb_ep->usb_ep->enabled) 1019 return; 1020 1021 for (;;) { 1022 if (!req) { 1023 req = get_empty_request(usb_ep); 1024 if (!req) 1025 break; 1026 } 1027 1028 if (process_midi1_pending_buf(midi2, &req)) 1029 continue; 1030 1031 len = snd_ump_transmit(ep->ump, &ump, 4); 1032 if (len <= 0) 1033 break; 1034 if (snd_ump_receive_ump_val(ep->ump, ump) <= 0) 1035 continue; 1036 size = snd_ump_convert_from_ump(ep->ump->input_buf, outbuf, 1037 &group); 1038 if (size <= 0) 1039 continue; 1040 cable = ep->in_group_to_cable[group]; 1041 if (!cable) 1042 continue; 1043 cable--; /* to 0-base */ 1044 fill_midi1_pending_buf(midi2, cable, outbuf, size); 1045 } 1046 1047 if (req) { 1048 if (req->length) 1049 queue_request_ep_raw(req); 1050 else 1051 put_empty_request(req); 1052 } 1053 } 1054 1055 /* complete handler for MIDI1 EP-in requests */ 1056 static void f_midi2_midi1_ep_in_complete(struct usb_ep *usb_ep, 1057 struct usb_request *req) 1058 { 1059 struct f_midi2_req_ctx *ctx = req->context; 1060 struct f_midi2 *midi2 = ctx->usb_ep->card; 1061 int status = req->status; 1062 1063 put_empty_request(req); 1064 1065 if (status) { 1066 DBG(midi2, "%s complete error %d: %d/%d\n", 1067 usb_ep->name, status, req->actual, req->length); 1068 return; 1069 } 1070 1071 process_midi1_transmit(midi2); 1072 } 1073 1074 /* complete handler for MIDI1 EP-out requests */ 1075 static void f_midi2_midi1_ep_out_complete(struct usb_ep *usb_ep, 1076 struct usb_request *req) 1077 { 1078 struct f_midi2_req_ctx *ctx = req->context; 1079 struct f_midi2 *midi2 = ctx->usb_ep->card; 1080 struct f_midi2_ep *ep; 1081 struct ump_cvt_to_ump *cvt = &midi2->midi1_ump_cvt; 1082 static const u8 midi1_packet_bytes[16] = { 1083 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1 1084 }; 1085 unsigned int group, cable, bytes, c, len; 1086 int status = req->status; 1087 const u8 *buf = req->buf; 1088 1089 if (status) { 1090 DBG(midi2, "%s complete error %d: %d/%d\n", 1091 usb_ep->name, status, req->actual, req->length); 1092 goto error; 1093 } 1094 1095 len = req->actual >> 2; 1096 for (; len; len--, buf += 4) { 1097 cable = *buf >> 4; 1098 ep = midi2->out_cable_mapping[cable].ep; 1099 if (!ep) 1100 continue; 1101 group = midi2->out_cable_mapping[cable].group; 1102 bytes = midi1_packet_bytes[*buf & 0x0f]; 1103 for (c = 0; c < bytes; c++) { 1104 snd_ump_convert_to_ump(cvt, group, 1105 to_ump_protocol(ep->info.protocol), 1106 buf[c + 1]); 1107 if (cvt->ump_bytes) { 1108 snd_ump_receive(ep->ump, cvt->ump, 1109 cvt->ump_bytes); 1110 cvt->ump_bytes = 0; 1111 } 1112 } 1113 } 1114 1115 if (midi2->operation_mode != MIDI_OP_MODE_MIDI1) 1116 goto error; 1117 1118 if (queue_request_ep_raw(req)) 1119 goto error; 1120 return; 1121 1122 error: 1123 put_empty_request(req); 1124 } 1125 1126 /* 1127 * Common EP handling helpers 1128 */ 1129 1130 /* Start MIDI EP */ 1131 static int f_midi2_start_ep(struct f_midi2_usb_ep *usb_ep, 1132 struct usb_function *fn) 1133 { 1134 int err; 1135 1136 if (!usb_ep->usb_ep) 1137 return 0; 1138 1139 usb_ep_disable(usb_ep->usb_ep); 1140 err = config_ep_by_speed(usb_ep->card->gadget, fn, usb_ep->usb_ep); 1141 if (err) 1142 return err; 1143 return usb_ep_enable(usb_ep->usb_ep); 1144 } 1145 1146 /* Drop pending requests */ 1147 static void f_midi2_drop_reqs(struct f_midi2_usb_ep *usb_ep) 1148 { 1149 int i; 1150 1151 if (!usb_ep->usb_ep || !usb_ep->num_reqs) 1152 return; 1153 1154 for (i = 0; i < usb_ep->num_reqs; i++) { 1155 if (!test_bit(i, &usb_ep->free_reqs) && usb_ep->reqs[i].req) { 1156 usb_ep_dequeue(usb_ep->usb_ep, usb_ep->reqs[i].req); 1157 set_bit(i, &usb_ep->free_reqs); 1158 } 1159 } 1160 } 1161 1162 /* Allocate requests for the given EP */ 1163 static int f_midi2_alloc_ep_reqs(struct f_midi2_usb_ep *usb_ep) 1164 { 1165 struct f_midi2 *midi2 = usb_ep->card; 1166 int i; 1167 1168 if (!usb_ep->usb_ep) 1169 return 0; 1170 if (!usb_ep->reqs) 1171 return -EINVAL; 1172 1173 for (i = 0; i < usb_ep->num_reqs; i++) { 1174 if (usb_ep->reqs[i].req) 1175 continue; 1176 usb_ep->reqs[i].req = alloc_ep_req(usb_ep->usb_ep, 1177 midi2->info.req_buf_size); 1178 if (!usb_ep->reqs[i].req) 1179 return -ENOMEM; 1180 usb_ep->reqs[i].req->context = &usb_ep->reqs[i]; 1181 } 1182 return 0; 1183 } 1184 1185 /* Free allocated requests */ 1186 static void f_midi2_free_ep_reqs(struct f_midi2_usb_ep *usb_ep) 1187 { 1188 int i; 1189 1190 for (i = 0; i < usb_ep->num_reqs; i++) { 1191 if (!usb_ep->reqs[i].req) 1192 continue; 1193 free_ep_req(usb_ep->usb_ep, usb_ep->reqs[i].req); 1194 usb_ep->reqs[i].req = NULL; 1195 } 1196 } 1197 1198 /* Initialize EP */ 1199 static int f_midi2_init_ep(struct f_midi2 *midi2, struct f_midi2_ep *ep, 1200 struct f_midi2_usb_ep *usb_ep, 1201 void *desc, 1202 void (*complete)(struct usb_ep *usb_ep, 1203 struct usb_request *req)) 1204 { 1205 int i; 1206 1207 usb_ep->card = midi2; 1208 usb_ep->ep = ep; 1209 usb_ep->usb_ep = usb_ep_autoconfig(midi2->gadget, desc); 1210 if (!usb_ep->usb_ep) 1211 return -ENODEV; 1212 usb_ep->complete = complete; 1213 1214 usb_ep->reqs = kzalloc_objs(*usb_ep->reqs, midi2->info.num_reqs); 1215 if (!usb_ep->reqs) 1216 return -ENOMEM; 1217 for (i = 0; i < midi2->info.num_reqs; i++) { 1218 usb_ep->reqs[i].index = i; 1219 usb_ep->reqs[i].usb_ep = usb_ep; 1220 set_bit(i, &usb_ep->free_reqs); 1221 usb_ep->num_reqs++; 1222 } 1223 1224 return 0; 1225 } 1226 1227 /* Free EP */ 1228 static void f_midi2_free_ep(struct f_midi2_usb_ep *usb_ep) 1229 { 1230 f_midi2_drop_reqs(usb_ep); 1231 1232 f_midi2_free_ep_reqs(usb_ep); 1233 1234 kfree(usb_ep->reqs); 1235 usb_ep->num_reqs = 0; 1236 usb_ep->free_reqs = 0; 1237 usb_ep->reqs = NULL; 1238 } 1239 1240 /* Queue requests for EP-out at start */ 1241 static void f_midi2_queue_out_reqs(struct f_midi2_usb_ep *usb_ep) 1242 { 1243 int i, err; 1244 1245 if (!usb_ep->usb_ep) 1246 return; 1247 1248 for (i = 0; i < usb_ep->num_reqs; i++) { 1249 if (!test_bit(i, &usb_ep->free_reqs) || !usb_ep->reqs[i].req) 1250 continue; 1251 usb_ep->reqs[i].req->complete = usb_ep->complete; 1252 err = usb_ep_queue(usb_ep->usb_ep, usb_ep->reqs[i].req, 1253 GFP_ATOMIC); 1254 if (!err) 1255 clear_bit(i, &usb_ep->free_reqs); 1256 } 1257 } 1258 1259 /* 1260 * Gadget Function callbacks 1261 */ 1262 1263 /* stop both IN and OUT EPs */ 1264 static void f_midi2_stop_eps(struct f_midi2_usb_ep *ep_in, 1265 struct f_midi2_usb_ep *ep_out) 1266 { 1267 f_midi2_drop_reqs(ep_in); 1268 f_midi2_drop_reqs(ep_out); 1269 f_midi2_free_ep_reqs(ep_in); 1270 f_midi2_free_ep_reqs(ep_out); 1271 } 1272 1273 /* start/queue both IN and OUT EPs */ 1274 static int f_midi2_start_eps(struct f_midi2_usb_ep *ep_in, 1275 struct f_midi2_usb_ep *ep_out, 1276 struct usb_function *fn) 1277 { 1278 int err; 1279 1280 err = f_midi2_start_ep(ep_in, fn); 1281 if (err) 1282 return err; 1283 err = f_midi2_start_ep(ep_out, fn); 1284 if (err) 1285 return err; 1286 1287 err = f_midi2_alloc_ep_reqs(ep_in); 1288 if (err) 1289 return err; 1290 err = f_midi2_alloc_ep_reqs(ep_out); 1291 if (err) 1292 return err; 1293 1294 f_midi2_queue_out_reqs(ep_out); 1295 return 0; 1296 } 1297 1298 /* gadget function set_alt callback */ 1299 static int f_midi2_set_alt(struct usb_function *fn, unsigned int intf, 1300 unsigned int alt) 1301 { 1302 struct f_midi2 *midi2 = func_to_midi2(fn); 1303 struct f_midi2_ep *ep; 1304 int i, op_mode, err; 1305 1306 if (intf != midi2->midi_if || alt > 1) 1307 return 0; 1308 1309 if (alt == 0) 1310 op_mode = MIDI_OP_MODE_MIDI1; 1311 else 1312 op_mode = MIDI_OP_MODE_MIDI2; 1313 1314 if (midi2->operation_mode == op_mode) 1315 return 0; 1316 1317 midi2->operation_mode = op_mode; 1318 1319 if (op_mode != MIDI_OP_MODE_MIDI1) 1320 f_midi2_stop_eps(&midi2->midi1_ep_in, &midi2->midi1_ep_out); 1321 1322 if (op_mode != MIDI_OP_MODE_MIDI2) { 1323 for (i = 0; i < midi2->num_eps; i++) { 1324 ep = &midi2->midi2_eps[i]; 1325 f_midi2_stop_eps(&ep->ep_in, &ep->ep_out); 1326 } 1327 } 1328 1329 if (op_mode == MIDI_OP_MODE_MIDI1) 1330 return f_midi2_start_eps(&midi2->midi1_ep_in, 1331 &midi2->midi1_ep_out, fn); 1332 1333 if (op_mode == MIDI_OP_MODE_MIDI2) { 1334 for (i = 0; i < midi2->num_eps; i++) { 1335 ep = &midi2->midi2_eps[i]; 1336 1337 err = f_midi2_start_eps(&ep->ep_in, &ep->ep_out, fn); 1338 if (err) 1339 return err; 1340 } 1341 } 1342 1343 return 0; 1344 } 1345 1346 /* gadget function get_alt callback */ 1347 static int f_midi2_get_alt(struct usb_function *fn, unsigned int intf) 1348 { 1349 struct f_midi2 *midi2 = func_to_midi2(fn); 1350 1351 if (intf == midi2->midi_if && 1352 midi2->operation_mode == MIDI_OP_MODE_MIDI2) 1353 return 1; 1354 return 0; 1355 } 1356 1357 /* convert UMP direction to USB MIDI 2.0 direction */ 1358 static unsigned int ump_to_usb_dir(unsigned int ump_dir) 1359 { 1360 switch (ump_dir) { 1361 case SNDRV_UMP_DIR_INPUT: 1362 return USB_MS_GR_TRM_BLOCK_TYPE_INPUT_ONLY; 1363 case SNDRV_UMP_DIR_OUTPUT: 1364 return USB_MS_GR_TRM_BLOCK_TYPE_OUTPUT_ONLY; 1365 default: 1366 return USB_MS_GR_TRM_BLOCK_TYPE_BIDIRECTIONAL; 1367 } 1368 } 1369 1370 /* assign GTB descriptors (for the given request) */ 1371 static void assign_block_descriptors(struct f_midi2 *midi2, 1372 struct usb_request *req, 1373 int max_len) 1374 { 1375 struct usb_ms20_gr_trm_block_header_descriptor header; 1376 struct usb_ms20_gr_trm_block_descriptor *desc; 1377 struct f_midi2_block_info *b; 1378 struct f_midi2_ep *ep; 1379 int i, blk, len; 1380 char *data; 1381 1382 len = sizeof(gtb_header_desc) + sizeof(gtb_desc) * midi2->total_blocks; 1383 if (WARN_ON(len > midi2->info.req_buf_size)) 1384 return; 1385 1386 header = gtb_header_desc; 1387 header.wTotalLength = cpu_to_le16(len); 1388 if (max_len < len) { 1389 len = min_t(int, len, sizeof(header)); 1390 memcpy(req->buf, &header, len); 1391 req->length = len; 1392 req->zero = len < max_len; 1393 return; 1394 } 1395 1396 memcpy(req->buf, &header, sizeof(header)); 1397 data = req->buf + sizeof(header); 1398 for (i = 0; i < midi2->num_eps; i++) { 1399 ep = &midi2->midi2_eps[i]; 1400 for (blk = 0; blk < ep->num_blks; blk++) { 1401 b = &ep->blks[blk].info; 1402 desc = (struct usb_ms20_gr_trm_block_descriptor *)data; 1403 1404 *desc = gtb_desc; 1405 desc->bGrpTrmBlkID = ep->blks[blk].gtb_id; 1406 desc->bGrpTrmBlkType = ump_to_usb_dir(b->direction); 1407 desc->nGroupTrm = b->first_group; 1408 desc->nNumGroupTrm = b->num_groups; 1409 desc->iBlockItem = ep->blks[blk].string_id; 1410 1411 if (ep->info.protocol == 2) 1412 desc->bMIDIProtocol = USB_MS_MIDI_PROTO_2_0; 1413 else 1414 desc->bMIDIProtocol = USB_MS_MIDI_PROTO_1_0_128; 1415 1416 if (b->is_midi1 == 2) { 1417 desc->wMaxInputBandwidth = cpu_to_le16(1); 1418 desc->wMaxOutputBandwidth = cpu_to_le16(1); 1419 } 1420 1421 data += sizeof(*desc); 1422 } 1423 } 1424 1425 req->length = len; 1426 req->zero = len < max_len; 1427 } 1428 1429 /* gadget function setup callback: handle GTB requests */ 1430 static int f_midi2_setup(struct usb_function *fn, 1431 const struct usb_ctrlrequest *ctrl) 1432 { 1433 struct f_midi2 *midi2 = func_to_midi2(fn); 1434 struct usb_composite_dev *cdev = fn->config->cdev; 1435 struct usb_request *req = cdev->req; 1436 u16 value, length; 1437 1438 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD || 1439 ctrl->bRequest != USB_REQ_GET_DESCRIPTOR) 1440 return -EOPNOTSUPP; 1441 1442 value = le16_to_cpu(ctrl->wValue); 1443 length = le16_to_cpu(ctrl->wLength); 1444 1445 if ((value >> 8) != USB_DT_CS_GR_TRM_BLOCK) 1446 return -EOPNOTSUPP; 1447 1448 /* handle only altset 1 */ 1449 if ((value & 0xff) != 1) 1450 return -EOPNOTSUPP; 1451 1452 assign_block_descriptors(midi2, req, length); 1453 return usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); 1454 } 1455 1456 /* gadget function disable callback */ 1457 static void f_midi2_disable(struct usb_function *fn) 1458 { 1459 struct f_midi2 *midi2 = func_to_midi2(fn); 1460 1461 midi2->operation_mode = MIDI_OP_MODE_UNSET; 1462 } 1463 1464 /* 1465 * ALSA UMP ops: most of them are NOPs, only trigger for write is needed 1466 */ 1467 static int f_midi2_ump_open(struct snd_ump_endpoint *ump, int dir) 1468 { 1469 return 0; 1470 } 1471 1472 static void f_midi2_ump_close(struct snd_ump_endpoint *ump, int dir) 1473 { 1474 } 1475 1476 static void f_midi2_ump_trigger(struct snd_ump_endpoint *ump, int dir, int up) 1477 { 1478 struct f_midi2_ep *ep = ump->private_data; 1479 struct f_midi2 *midi2 = ep->card; 1480 1481 if (up && dir == SNDRV_RAWMIDI_STREAM_OUTPUT) { 1482 switch (midi2->operation_mode) { 1483 case MIDI_OP_MODE_MIDI1: 1484 process_midi1_transmit(midi2); 1485 break; 1486 case MIDI_OP_MODE_MIDI2: 1487 process_ump_transmit(ep); 1488 break; 1489 } 1490 } 1491 } 1492 1493 static void f_midi2_ump_drain(struct snd_ump_endpoint *ump, int dir) 1494 { 1495 } 1496 1497 static const struct snd_ump_ops f_midi2_ump_ops = { 1498 .open = f_midi2_ump_open, 1499 .close = f_midi2_ump_close, 1500 .trigger = f_midi2_ump_trigger, 1501 .drain = f_midi2_ump_drain, 1502 }; 1503 1504 /* 1505 * "Operation Mode" control element 1506 */ 1507 static int f_midi2_operation_mode_info(struct snd_kcontrol *kcontrol, 1508 struct snd_ctl_elem_info *uinfo) 1509 { 1510 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1511 uinfo->count = 1; 1512 uinfo->value.integer.min = MIDI_OP_MODE_UNSET; 1513 uinfo->value.integer.max = MIDI_OP_MODE_MIDI2; 1514 return 0; 1515 } 1516 1517 static int f_midi2_operation_mode_get(struct snd_kcontrol *kcontrol, 1518 struct snd_ctl_elem_value *ucontrol) 1519 { 1520 struct f_midi2 *midi2 = snd_kcontrol_chip(kcontrol); 1521 1522 ucontrol->value.integer.value[0] = midi2->operation_mode; 1523 return 0; 1524 } 1525 1526 static const struct snd_kcontrol_new operation_mode_ctl = { 1527 .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI, 1528 .name = "Operation Mode", 1529 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, 1530 .info = f_midi2_operation_mode_info, 1531 .get = f_midi2_operation_mode_get, 1532 }; 1533 1534 /* 1535 * ALSA UMP instance creation / deletion 1536 */ 1537 static void f_midi2_free_card(struct f_midi2 *midi2) 1538 { 1539 if (midi2->card) { 1540 snd_card_free_when_closed(midi2->card); 1541 midi2->card = NULL; 1542 } 1543 } 1544 1545 /* use a reverse direction for the gadget host */ 1546 static int reverse_dir(int dir) 1547 { 1548 if (!dir || dir == SNDRV_UMP_DIR_BIDIRECTION) 1549 return dir; 1550 return (dir == SNDRV_UMP_DIR_OUTPUT) ? 1551 SNDRV_UMP_DIR_INPUT : SNDRV_UMP_DIR_OUTPUT; 1552 } 1553 1554 static int f_midi2_create_card(struct f_midi2 *midi2) 1555 { 1556 struct snd_card *card; 1557 struct snd_ump_endpoint *ump; 1558 struct f_midi2_ep *ep; 1559 int i, id, blk, err; 1560 __be32 sw; 1561 1562 err = snd_card_new(&midi2->gadget->dev, -1, NULL, THIS_MODULE, 0, 1563 &card); 1564 if (err < 0) 1565 return err; 1566 midi2->card = card; 1567 1568 strscpy(card->driver, "f_midi2"); 1569 strscpy(card->shortname, "MIDI 2.0 Gadget"); 1570 strscpy(card->longname, "MIDI 2.0 Gadget"); 1571 1572 id = 0; 1573 for (i = 0; i < midi2->num_eps; i++) { 1574 ep = &midi2->midi2_eps[i]; 1575 err = snd_ump_endpoint_new(card, "MIDI 2.0 Gadget", id, 1576 1, 1, &ump); 1577 if (err < 0) 1578 goto error; 1579 id++; 1580 1581 ep->ump = ump; 1582 ump->no_process_stream = true; 1583 ump->private_data = ep; 1584 ump->ops = &f_midi2_ump_ops; 1585 if (midi2->info.static_block) 1586 ump->info.flags |= SNDRV_UMP_EP_INFO_STATIC_BLOCKS; 1587 ump->info.protocol_caps = (ep->info.protocol_caps & 3) << 8; 1588 ump->info.protocol = to_ump_protocol(ep->info.protocol); 1589 ump->info.version = 0x0101; 1590 ump->info.family_id = ep->info.family; 1591 ump->info.model_id = ep->info.model; 1592 ump->info.manufacturer_id = ep->info.manufacturer & 0xffffff; 1593 sw = cpu_to_be32(ep->info.sw_revision); 1594 memcpy(ump->info.sw_revision, &sw, 4); 1595 1596 strscpy(ump->info.name, ump_ep_name(ep), 1597 sizeof(ump->info.name)); 1598 strscpy(ump->info.product_id, ump_product_id(ep), 1599 sizeof(ump->info.product_id)); 1600 strscpy(ump->core.name, ump->info.name, sizeof(ump->core.name)); 1601 1602 for (blk = 0; blk < ep->num_blks; blk++) { 1603 const struct f_midi2_block_info *b = &ep->blks[blk].info; 1604 struct snd_ump_block *fb; 1605 1606 err = snd_ump_block_new(ump, blk, 1607 reverse_dir(b->direction), 1608 b->first_group, b->num_groups, 1609 &ep->blks[blk].fb); 1610 if (err < 0) 1611 goto error; 1612 fb = ep->blks[blk].fb; 1613 fb->info.active = !!b->active; 1614 fb->info.midi_ci_version = b->midi_ci_version; 1615 fb->info.ui_hint = reverse_dir(b->ui_hint); 1616 fb->info.sysex8_streams = b->sysex8_streams; 1617 if (b->is_midi1 < 2) 1618 fb->info.flags |= b->is_midi1; 1619 else 1620 fb->info.flags |= SNDRV_UMP_BLOCK_IS_MIDI1 | 1621 SNDRV_UMP_BLOCK_IS_LOWSPEED; 1622 strscpy(fb->info.name, ump_fb_name(b), 1623 sizeof(fb->info.name)); 1624 } 1625 snd_ump_update_group_attrs(ump); 1626 } 1627 1628 for (i = 0; i < midi2->num_eps; i++) { 1629 err = snd_ump_attach_legacy_rawmidi(midi2->midi2_eps[i].ump, 1630 "Legacy MIDI", id); 1631 if (err < 0) 1632 goto error; 1633 id++; 1634 } 1635 1636 err = snd_ctl_add(card, snd_ctl_new1(&operation_mode_ctl, midi2)); 1637 if (err < 0) 1638 goto error; 1639 1640 err = snd_card_register(card); 1641 if (err < 0) 1642 goto error; 1643 1644 return 0; 1645 1646 error: 1647 f_midi2_free_card(midi2); 1648 return err; 1649 } 1650 1651 /* 1652 * Creation of USB descriptors 1653 */ 1654 struct f_midi2_usb_config { 1655 struct usb_descriptor_header **list; 1656 unsigned int size; 1657 unsigned int alloc; 1658 1659 /* MIDI 1.0 jacks */ 1660 unsigned char jack_in, jack_out, jack_id; 1661 struct usb_midi_in_jack_descriptor jack_ins[MAX_CABLES * 2]; 1662 struct usb_midi_out_jack_descriptor_1 jack_outs[MAX_CABLES * 2]; 1663 }; 1664 1665 static int append_config(struct f_midi2_usb_config *config, void *d) 1666 { 1667 unsigned int size; 1668 void *buf; 1669 1670 if (config->size + 2 >= config->alloc) { 1671 size = config->size + 16; 1672 buf = krealloc(config->list, size * sizeof(void *), GFP_KERNEL); 1673 if (!buf) 1674 return -ENOMEM; 1675 config->list = buf; 1676 config->alloc = size; 1677 } 1678 1679 config->list[config->size] = d; 1680 config->size++; 1681 config->list[config->size] = NULL; 1682 return 0; 1683 } 1684 1685 static int append_configs(struct f_midi2_usb_config *config, void **d) 1686 { 1687 int err; 1688 1689 for (; *d; d++) { 1690 err = append_config(config, *d); 1691 if (err) 1692 return err; 1693 } 1694 return 0; 1695 } 1696 1697 static int append_midi1_in_jack(struct f_midi2 *midi2, 1698 struct f_midi2_usb_config *config, 1699 struct midi1_cable_mapping *map, 1700 unsigned int type) 1701 { 1702 struct usb_midi_in_jack_descriptor *jack = 1703 &config->jack_ins[config->jack_in++]; 1704 int id = ++config->jack_id; 1705 int err; 1706 1707 jack->bLength = 0x06; 1708 jack->bDescriptorType = USB_DT_CS_INTERFACE; 1709 jack->bDescriptorSubtype = USB_MS_MIDI_IN_JACK; 1710 jack->bJackType = type; 1711 jack->bJackID = id; 1712 /* use the corresponding block name as jack name */ 1713 if (map->ep) 1714 jack->iJack = map->ep->blks[map->block].string_id; 1715 1716 err = append_config(config, jack); 1717 if (err < 0) 1718 return err; 1719 return id; 1720 } 1721 1722 static int append_midi1_out_jack(struct f_midi2 *midi2, 1723 struct f_midi2_usb_config *config, 1724 struct midi1_cable_mapping *map, 1725 unsigned int type, unsigned int source) 1726 { 1727 struct usb_midi_out_jack_descriptor_1 *jack = 1728 &config->jack_outs[config->jack_out++]; 1729 int id = ++config->jack_id; 1730 int err; 1731 1732 jack->bLength = 0x09; 1733 jack->bDescriptorType = USB_DT_CS_INTERFACE; 1734 jack->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK; 1735 jack->bJackType = type; 1736 jack->bJackID = id; 1737 jack->bNrInputPins = 1; 1738 jack->pins[0].baSourceID = source; 1739 jack->pins[0].baSourcePin = 0x01; 1740 /* use the corresponding block name as jack name */ 1741 if (map->ep) 1742 jack->iJack = map->ep->blks[map->block].string_id; 1743 1744 err = append_config(config, jack); 1745 if (err < 0) 1746 return err; 1747 return id; 1748 } 1749 1750 static int f_midi2_create_usb_configs(struct f_midi2 *midi2, 1751 struct f_midi2_usb_config *config, 1752 int speed) 1753 { 1754 void **midi1_in_eps, **midi1_out_eps; 1755 int i, jack, total; 1756 int err; 1757 1758 switch (speed) { 1759 default: 1760 case USB_SPEED_HIGH: 1761 midi2_midi1_ep_out_desc.wMaxPacketSize = cpu_to_le16(512); 1762 midi2_midi1_ep_in_desc.wMaxPacketSize = cpu_to_le16(512); 1763 for (i = 0; i < midi2->num_eps; i++) { 1764 midi2_midi2_ep_out_desc[i].wMaxPacketSize = 1765 cpu_to_le16(512); 1766 midi2_midi2_ep_in_desc[i].wMaxPacketSize = 1767 cpu_to_le16(512); 1768 } 1769 fallthrough; 1770 case USB_SPEED_FULL: 1771 midi1_in_eps = midi2_midi1_ep_in_descs; 1772 midi1_out_eps = midi2_midi1_ep_out_descs; 1773 break; 1774 case USB_SPEED_SUPER: 1775 midi2_midi1_ep_out_desc.wMaxPacketSize = cpu_to_le16(1024); 1776 midi2_midi1_ep_in_desc.wMaxPacketSize = cpu_to_le16(1024); 1777 for (i = 0; i < midi2->num_eps; i++) { 1778 midi2_midi2_ep_out_desc[i].wMaxPacketSize = 1779 cpu_to_le16(1024); 1780 midi2_midi2_ep_in_desc[i].wMaxPacketSize = 1781 cpu_to_le16(1024); 1782 } 1783 midi1_in_eps = midi2_midi1_ep_in_ss_descs; 1784 midi1_out_eps = midi2_midi1_ep_out_ss_descs; 1785 break; 1786 } 1787 1788 err = append_configs(config, midi2_audio_descs); 1789 if (err < 0) 1790 return err; 1791 1792 if (midi2->num_midi1_in && midi2->num_midi1_out) 1793 midi2_midi1_if_desc.bNumEndpoints = 2; 1794 else 1795 midi2_midi1_if_desc.bNumEndpoints = 1; 1796 1797 err = append_configs(config, midi2_midi1_descs); 1798 if (err < 0) 1799 return err; 1800 1801 total = USB_DT_MS_HEADER_SIZE; 1802 if (midi2->num_midi1_out) { 1803 midi2_midi1_ep_out_class_desc.bLength = 1804 USB_DT_MS_ENDPOINT_SIZE(midi2->num_midi1_out); 1805 total += midi2_midi1_ep_out_class_desc.bLength; 1806 midi2_midi1_ep_out_class_desc.bNumEmbMIDIJack = 1807 midi2->num_midi1_out; 1808 total += midi2->num_midi1_out * 1809 (USB_DT_MIDI_IN_SIZE + USB_DT_MIDI_OUT_SIZE(1)); 1810 for (i = 0; i < midi2->num_midi1_out; i++) { 1811 jack = append_midi1_in_jack(midi2, config, 1812 &midi2->in_cable_mapping[i], 1813 USB_MS_EMBEDDED); 1814 if (jack < 0) 1815 return jack; 1816 midi2_midi1_ep_out_class_desc.baAssocJackID[i] = jack; 1817 jack = append_midi1_out_jack(midi2, config, 1818 &midi2->in_cable_mapping[i], 1819 USB_MS_EXTERNAL, jack); 1820 if (jack < 0) 1821 return jack; 1822 } 1823 } 1824 1825 if (midi2->num_midi1_in) { 1826 midi2_midi1_ep_in_class_desc.bLength = 1827 USB_DT_MS_ENDPOINT_SIZE(midi2->num_midi1_in); 1828 total += midi2_midi1_ep_in_class_desc.bLength; 1829 midi2_midi1_ep_in_class_desc.bNumEmbMIDIJack = 1830 midi2->num_midi1_in; 1831 total += midi2->num_midi1_in * 1832 (USB_DT_MIDI_IN_SIZE + USB_DT_MIDI_OUT_SIZE(1)); 1833 for (i = 0; i < midi2->num_midi1_in; i++) { 1834 jack = append_midi1_in_jack(midi2, config, 1835 &midi2->out_cable_mapping[i], 1836 USB_MS_EXTERNAL); 1837 if (jack < 0) 1838 return jack; 1839 jack = append_midi1_out_jack(midi2, config, 1840 &midi2->out_cable_mapping[i], 1841 USB_MS_EMBEDDED, jack); 1842 if (jack < 0) 1843 return jack; 1844 midi2_midi1_ep_in_class_desc.baAssocJackID[i] = jack; 1845 } 1846 } 1847 1848 midi2_midi1_class_desc.wTotalLength = cpu_to_le16(total); 1849 1850 if (midi2->num_midi1_out) { 1851 err = append_configs(config, midi1_out_eps); 1852 if (err < 0) 1853 return err; 1854 } 1855 if (midi2->num_midi1_in) { 1856 err = append_configs(config, midi1_in_eps); 1857 if (err < 0) 1858 return err; 1859 } 1860 1861 err = append_configs(config, midi2_midi2_descs); 1862 if (err < 0) 1863 return err; 1864 1865 for (i = 0; i < midi2->num_eps; i++) { 1866 err = append_config(config, &midi2_midi2_ep_out_desc[i]); 1867 if (err < 0) 1868 return err; 1869 if (speed == USB_SPEED_SUPER || speed == USB_SPEED_SUPER_PLUS) { 1870 err = append_config(config, &midi2_midi2_ep_out_ss_comp_desc); 1871 if (err < 0) 1872 return err; 1873 } 1874 err = append_config(config, &midi2_midi2_ep_out_class_desc[i]); 1875 if (err < 0) 1876 return err; 1877 err = append_config(config, &midi2_midi2_ep_in_desc[i]); 1878 if (err < 0) 1879 return err; 1880 if (speed == USB_SPEED_SUPER || speed == USB_SPEED_SUPER_PLUS) { 1881 err = append_config(config, &midi2_midi2_ep_in_ss_comp_desc); 1882 if (err < 0) 1883 return err; 1884 } 1885 err = append_config(config, &midi2_midi2_ep_in_class_desc[i]); 1886 if (err < 0) 1887 return err; 1888 } 1889 1890 return 0; 1891 } 1892 1893 static void f_midi2_free_usb_configs(struct f_midi2_usb_config *config) 1894 { 1895 kfree(config->list); 1896 memset(config, 0, sizeof(*config)); 1897 } 1898 1899 /* as we use the static descriptors for simplicity, serialize bind call */ 1900 static DEFINE_MUTEX(f_midi2_desc_mutex); 1901 1902 /* fill MIDI2 EP class-specific descriptor */ 1903 static void fill_midi2_class_desc(struct f_midi2_ep *ep, 1904 struct usb_ms20_endpoint_descriptor_32 *cdesc) 1905 { 1906 int blk; 1907 1908 cdesc->bLength = USB_DT_MS20_ENDPOINT_SIZE(ep->num_blks); 1909 cdesc->bDescriptorType = USB_DT_CS_ENDPOINT; 1910 cdesc->bDescriptorSubtype = USB_MS_GENERAL_2_0; 1911 cdesc->bNumGrpTrmBlock = ep->num_blks; 1912 for (blk = 0; blk < ep->num_blks; blk++) 1913 cdesc->baAssoGrpTrmBlkID[blk] = ep->blks[blk].gtb_id; 1914 } 1915 1916 /* initialize MIDI2 EP-in */ 1917 static int f_midi2_init_midi2_ep_in(struct f_midi2 *midi2, int index) 1918 { 1919 struct f_midi2_ep *ep = &midi2->midi2_eps[index]; 1920 struct usb_endpoint_descriptor *desc = &midi2_midi2_ep_in_desc[index]; 1921 1922 desc->bLength = USB_DT_ENDPOINT_SIZE; 1923 desc->bDescriptorType = USB_DT_ENDPOINT; 1924 desc->bEndpointAddress = USB_DIR_IN; 1925 desc->bmAttributes = USB_ENDPOINT_XFER_INT; 1926 desc->wMaxPacketSize = cpu_to_le16(EP_MAX_PACKET_INT); 1927 desc->bInterval = 1; 1928 1929 fill_midi2_class_desc(ep, &midi2_midi2_ep_in_class_desc[index]); 1930 1931 return f_midi2_init_ep(midi2, ep, &ep->ep_in, desc, 1932 f_midi2_ep_in_complete); 1933 } 1934 1935 /* initialize MIDI2 EP-out */ 1936 static int f_midi2_init_midi2_ep_out(struct f_midi2 *midi2, int index) 1937 { 1938 struct f_midi2_ep *ep = &midi2->midi2_eps[index]; 1939 struct usb_endpoint_descriptor *desc = &midi2_midi2_ep_out_desc[index]; 1940 1941 desc->bLength = USB_DT_ENDPOINT_SIZE; 1942 desc->bDescriptorType = USB_DT_ENDPOINT; 1943 desc->bEndpointAddress = USB_DIR_OUT; 1944 desc->bmAttributes = USB_ENDPOINT_XFER_BULK; 1945 1946 fill_midi2_class_desc(ep, &midi2_midi2_ep_out_class_desc[index]); 1947 1948 return f_midi2_init_ep(midi2, ep, &ep->ep_out, desc, 1949 f_midi2_ep_out_complete); 1950 } 1951 1952 /* gadget function bind callback */ 1953 static int f_midi2_bind(struct usb_configuration *c, struct usb_function *f) 1954 { 1955 struct usb_composite_dev *cdev = c->cdev; 1956 struct f_midi2 *midi2 = func_to_midi2(f); 1957 struct f_midi2_ep *ep; 1958 struct f_midi2_usb_config config = {}; 1959 struct usb_gadget_strings string_fn = { 1960 .language = 0x0409, /* en-us */ 1961 .strings = midi2->string_defs, 1962 }; 1963 struct usb_gadget_strings *strings[] = { 1964 &string_fn, 1965 NULL, 1966 }; 1967 int i, blk, status; 1968 1969 midi2->gadget = cdev->gadget; 1970 midi2->operation_mode = MIDI_OP_MODE_UNSET; 1971 1972 status = f_midi2_create_card(midi2); 1973 if (status < 0) 1974 goto fail_register; 1975 1976 /* maybe allocate device-global string ID */ 1977 midi2->strings = usb_gstrings_attach(c->cdev, strings, 1978 midi2->total_blocks + 1); 1979 if (IS_ERR(midi2->strings)) { 1980 status = PTR_ERR(midi2->strings); 1981 goto fail_string; 1982 } 1983 1984 mutex_lock(&f_midi2_desc_mutex); 1985 midi2_midi1_if_desc.iInterface = midi2->strings[STR_IFACE].id; 1986 midi2_midi2_if_desc.iInterface = midi2->strings[STR_IFACE].id; 1987 for (i = 0; i < midi2->num_eps; i++) { 1988 ep = &midi2->midi2_eps[i]; 1989 for (blk = 0; blk < ep->num_blks; blk++) 1990 ep->blks[blk].string_id = 1991 midi2->strings[gtb_to_str_id(ep->blks[blk].gtb_id)].id; 1992 } 1993 1994 midi2_midi2_if_desc.bNumEndpoints = midi2->num_eps * 2; 1995 1996 /* audio interface */ 1997 status = usb_interface_id(c, f); 1998 if (status < 0) 1999 goto fail; 2000 midi2_audio_if_desc.bInterfaceNumber = status; 2001 2002 /* MIDI streaming */ 2003 status = usb_interface_id(c, f); 2004 if (status < 0) 2005 goto fail; 2006 midi2->midi_if = status; 2007 midi2_midi1_if_desc.bInterfaceNumber = status; 2008 midi2_midi2_if_desc.bInterfaceNumber = status; 2009 midi2_audio_class_desc.baInterfaceNr[0] = status; 2010 2011 /* allocate instance-specific endpoints */ 2012 if (midi2->midi2_eps[0].blks[0].info.direction != SNDRV_UMP_DIR_OUTPUT) { 2013 status = f_midi2_init_ep(midi2, NULL, &midi2->midi1_ep_in, 2014 &midi2_midi1_ep_in_desc, 2015 f_midi2_midi1_ep_in_complete); 2016 if (status) 2017 goto fail; 2018 } 2019 2020 if (midi2->midi2_eps[0].blks[0].info.direction != SNDRV_UMP_DIR_INPUT) { 2021 status = f_midi2_init_ep(midi2, NULL, &midi2->midi1_ep_out, 2022 &midi2_midi1_ep_out_desc, 2023 f_midi2_midi1_ep_out_complete); 2024 if (status) 2025 goto fail; 2026 } 2027 2028 for (i = 0; i < midi2->num_eps; i++) { 2029 status = f_midi2_init_midi2_ep_in(midi2, i); 2030 if (status) 2031 goto fail; 2032 status = f_midi2_init_midi2_ep_out(midi2, i); 2033 if (status) 2034 goto fail; 2035 } 2036 2037 status = f_midi2_create_usb_configs(midi2, &config, USB_SPEED_FULL); 2038 if (status < 0) 2039 goto fail; 2040 f->fs_descriptors = usb_copy_descriptors(config.list); 2041 if (!f->fs_descriptors) { 2042 status = -ENOMEM; 2043 goto fail; 2044 } 2045 f_midi2_free_usb_configs(&config); 2046 2047 status = f_midi2_create_usb_configs(midi2, &config, USB_SPEED_HIGH); 2048 if (status < 0) 2049 goto fail; 2050 f->hs_descriptors = usb_copy_descriptors(config.list); 2051 if (!f->hs_descriptors) { 2052 status = -ENOMEM; 2053 goto fail; 2054 } 2055 f_midi2_free_usb_configs(&config); 2056 2057 status = f_midi2_create_usb_configs(midi2, &config, USB_SPEED_SUPER); 2058 if (status < 0) 2059 goto fail; 2060 f->ss_descriptors = usb_copy_descriptors(config.list); 2061 if (!f->ss_descriptors) { 2062 status = -ENOMEM; 2063 goto fail; 2064 } 2065 f_midi2_free_usb_configs(&config); 2066 2067 mutex_unlock(&f_midi2_desc_mutex); 2068 return 0; 2069 2070 fail: 2071 f_midi2_free_usb_configs(&config); 2072 mutex_unlock(&f_midi2_desc_mutex); 2073 usb_free_all_descriptors(f); 2074 fail_string: 2075 f_midi2_free_card(midi2); 2076 fail_register: 2077 ERROR(midi2, "%s: can't bind, err %d\n", f->name, status); 2078 return status; 2079 } 2080 2081 /* gadget function unbind callback */ 2082 static void f_midi2_unbind(struct usb_configuration *c, struct usb_function *f) 2083 { 2084 struct f_midi2 *midi2 = func_to_midi2(f); 2085 int i; 2086 2087 f_midi2_free_card(midi2); 2088 2089 f_midi2_free_ep(&midi2->midi1_ep_in); 2090 f_midi2_free_ep(&midi2->midi1_ep_out); 2091 for (i = 0; i < midi2->num_eps; i++) { 2092 f_midi2_free_ep(&midi2->midi2_eps[i].ep_in); 2093 f_midi2_free_ep(&midi2->midi2_eps[i].ep_out); 2094 } 2095 2096 usb_free_all_descriptors(f); 2097 } 2098 2099 /* 2100 * ConfigFS interface 2101 */ 2102 2103 /* type conversion helpers */ 2104 static inline struct f_midi2_opts *to_f_midi2_opts(struct config_item *item) 2105 { 2106 return container_of(to_config_group(item), struct f_midi2_opts, 2107 func_inst.group); 2108 } 2109 2110 static inline struct f_midi2_ep_opts * 2111 to_f_midi2_ep_opts(struct config_item *item) 2112 { 2113 return container_of(to_config_group(item), struct f_midi2_ep_opts, 2114 group); 2115 } 2116 2117 static inline struct f_midi2_block_opts * 2118 to_f_midi2_block_opts(struct config_item *item) 2119 { 2120 return container_of(to_config_group(item), struct f_midi2_block_opts, 2121 group); 2122 } 2123 2124 /* trim the string to be usable for EP and FB name strings */ 2125 static void make_name_string(char *s) 2126 { 2127 char *p; 2128 2129 p = strchr(s, '\n'); 2130 if (p) 2131 *p = 0; 2132 2133 p = s + strlen(s); 2134 for (; p > s && isspace(*p); p--) 2135 *p = 0; 2136 } 2137 2138 /* configfs helpers: generic show/store for unisnged int */ 2139 static ssize_t f_midi2_opts_uint_show(struct f_midi2_opts *opts, 2140 u32 val, const char *format, char *page) 2141 { 2142 int result; 2143 2144 mutex_lock(&opts->lock); 2145 result = sprintf(page, format, val); 2146 mutex_unlock(&opts->lock); 2147 return result; 2148 } 2149 2150 static ssize_t f_midi2_opts_uint_store(struct f_midi2_opts *opts, 2151 u32 *valp, u32 minval, u32 maxval, 2152 const char *page, size_t len) 2153 { 2154 int ret; 2155 u32 val; 2156 2157 mutex_lock(&opts->lock); 2158 if (opts->refcnt) { 2159 ret = -EBUSY; 2160 goto end; 2161 } 2162 2163 ret = kstrtou32(page, 0, &val); 2164 if (ret) 2165 goto end; 2166 if (val < minval || val > maxval) { 2167 ret = -EINVAL; 2168 goto end; 2169 } 2170 2171 *valp = val; 2172 ret = len; 2173 2174 end: 2175 mutex_unlock(&opts->lock); 2176 return ret; 2177 } 2178 2179 /* generic store for bool */ 2180 static ssize_t f_midi2_opts_bool_store(struct f_midi2_opts *opts, 2181 bool *valp, const char *page, size_t len) 2182 { 2183 int ret; 2184 bool val; 2185 2186 mutex_lock(&opts->lock); 2187 if (opts->refcnt) { 2188 ret = -EBUSY; 2189 goto end; 2190 } 2191 2192 ret = kstrtobool(page, &val); 2193 if (ret) 2194 goto end; 2195 *valp = val; 2196 ret = len; 2197 2198 end: 2199 mutex_unlock(&opts->lock); 2200 return ret; 2201 } 2202 2203 /* generic show/store for string */ 2204 static ssize_t f_midi2_opts_str_show(struct f_midi2_opts *opts, 2205 const char **strp, char *page) 2206 { 2207 int result = 0; 2208 2209 mutex_lock(&opts->lock); 2210 if (*strp) 2211 result = scnprintf(page, PAGE_SIZE, "%s\n", *strp); 2212 mutex_unlock(&opts->lock); 2213 return result; 2214 } 2215 2216 static ssize_t f_midi2_opts_str_store(struct f_midi2_opts *opts, 2217 const char **strp, size_t maxlen, 2218 const char *page, size_t len) 2219 { 2220 char *c; 2221 int ret; 2222 2223 mutex_lock(&opts->lock); 2224 if (opts->refcnt) { 2225 ret = -EBUSY; 2226 goto end; 2227 } 2228 2229 c = kstrndup(page, min(len, maxlen), GFP_KERNEL); 2230 if (!c) { 2231 ret = -ENOMEM; 2232 goto end; 2233 } 2234 2235 kfree(*strp); 2236 make_name_string(c); 2237 *strp = c; 2238 ret = len; 2239 2240 end: 2241 mutex_unlock(&opts->lock); 2242 return ret; 2243 } 2244 2245 /* 2246 * Definitions for UMP Block config 2247 */ 2248 2249 /* define an uint option for block */ 2250 #define F_MIDI2_BLOCK_OPT(name, format, minval, maxval) \ 2251 static ssize_t f_midi2_block_opts_##name##_show(struct config_item *item,\ 2252 char *page) \ 2253 { \ 2254 struct f_midi2_block_opts *opts = to_f_midi2_block_opts(item); \ 2255 return f_midi2_opts_uint_show(opts->ep->opts, opts->info.name, \ 2256 format "\n", page); \ 2257 } \ 2258 \ 2259 static ssize_t f_midi2_block_opts_##name##_store(struct config_item *item,\ 2260 const char *page, size_t len) \ 2261 { \ 2262 struct f_midi2_block_opts *opts = to_f_midi2_block_opts(item); \ 2263 return f_midi2_opts_uint_store(opts->ep->opts, &opts->info.name,\ 2264 minval, maxval, page, len); \ 2265 } \ 2266 \ 2267 CONFIGFS_ATTR(f_midi2_block_opts_, name) 2268 2269 /* define a boolean option for block */ 2270 #define F_MIDI2_BLOCK_BOOL_OPT(name) \ 2271 static ssize_t f_midi2_block_opts_##name##_show(struct config_item *item,\ 2272 char *page) \ 2273 { \ 2274 struct f_midi2_block_opts *opts = to_f_midi2_block_opts(item); \ 2275 return f_midi2_opts_uint_show(opts->ep->opts, opts->info.name, \ 2276 "%u\n", page); \ 2277 } \ 2278 \ 2279 static ssize_t f_midi2_block_opts_##name##_store(struct config_item *item,\ 2280 const char *page, size_t len) \ 2281 { \ 2282 struct f_midi2_block_opts *opts = to_f_midi2_block_opts(item); \ 2283 return f_midi2_opts_bool_store(opts->ep->opts, &opts->info.name,\ 2284 page, len); \ 2285 } \ 2286 \ 2287 CONFIGFS_ATTR(f_midi2_block_opts_, name) 2288 2289 F_MIDI2_BLOCK_OPT(direction, "0x%x", 1, 3); 2290 F_MIDI2_BLOCK_OPT(first_group, "0x%x", 0, 15); 2291 F_MIDI2_BLOCK_OPT(num_groups, "0x%x", 1, 16); 2292 F_MIDI2_BLOCK_OPT(midi1_first_group, "0x%x", 0, 15); 2293 F_MIDI2_BLOCK_OPT(midi1_num_groups, "0x%x", 0, 16); 2294 F_MIDI2_BLOCK_OPT(ui_hint, "0x%x", 0, 3); 2295 F_MIDI2_BLOCK_OPT(midi_ci_version, "%u", 0, 1); 2296 F_MIDI2_BLOCK_OPT(sysex8_streams, "%u", 0, 255); 2297 F_MIDI2_BLOCK_OPT(is_midi1, "%u", 0, 2); 2298 F_MIDI2_BLOCK_BOOL_OPT(active); 2299 2300 static ssize_t f_midi2_block_opts_name_show(struct config_item *item, 2301 char *page) 2302 { 2303 struct f_midi2_block_opts *opts = to_f_midi2_block_opts(item); 2304 2305 return f_midi2_opts_str_show(opts->ep->opts, &opts->info.name, page); 2306 } 2307 2308 static ssize_t f_midi2_block_opts_name_store(struct config_item *item, 2309 const char *page, size_t len) 2310 { 2311 struct f_midi2_block_opts *opts = to_f_midi2_block_opts(item); 2312 2313 return f_midi2_opts_str_store(opts->ep->opts, &opts->info.name, 128, 2314 page, len); 2315 } 2316 2317 CONFIGFS_ATTR(f_midi2_block_opts_, name); 2318 2319 static struct configfs_attribute *f_midi2_block_attrs[] = { 2320 &f_midi2_block_opts_attr_direction, 2321 &f_midi2_block_opts_attr_first_group, 2322 &f_midi2_block_opts_attr_num_groups, 2323 &f_midi2_block_opts_attr_midi1_first_group, 2324 &f_midi2_block_opts_attr_midi1_num_groups, 2325 &f_midi2_block_opts_attr_ui_hint, 2326 &f_midi2_block_opts_attr_midi_ci_version, 2327 &f_midi2_block_opts_attr_sysex8_streams, 2328 &f_midi2_block_opts_attr_is_midi1, 2329 &f_midi2_block_opts_attr_active, 2330 &f_midi2_block_opts_attr_name, 2331 NULL, 2332 }; 2333 2334 static void f_midi2_block_opts_release(struct config_item *item) 2335 { 2336 struct f_midi2_block_opts *opts = to_f_midi2_block_opts(item); 2337 2338 kfree(opts->info.name); 2339 kfree(opts); 2340 } 2341 2342 static const struct configfs_item_operations f_midi2_block_item_ops = { 2343 .release = f_midi2_block_opts_release, 2344 }; 2345 2346 static const struct config_item_type f_midi2_block_type = { 2347 .ct_item_ops = &f_midi2_block_item_ops, 2348 .ct_attrs = f_midi2_block_attrs, 2349 .ct_owner = THIS_MODULE, 2350 }; 2351 2352 /* create a f_midi2_block_opts instance for the given block number */ 2353 static int f_midi2_block_opts_create(struct f_midi2_ep_opts *ep_opts, 2354 unsigned int blk, 2355 struct f_midi2_block_opts **block_p) 2356 { 2357 struct f_midi2_block_opts *block_opts; 2358 int ret = 0; 2359 2360 mutex_lock(&ep_opts->opts->lock); 2361 if (ep_opts->opts->refcnt || ep_opts->blks[blk]) { 2362 ret = -EBUSY; 2363 goto out; 2364 } 2365 2366 block_opts = kzalloc_obj(*block_opts); 2367 if (!block_opts) { 2368 ret = -ENOMEM; 2369 goto out; 2370 } 2371 2372 block_opts->ep = ep_opts; 2373 block_opts->id = blk; 2374 2375 /* set up the default values */ 2376 block_opts->info.direction = SNDRV_UMP_DIR_BIDIRECTION; 2377 block_opts->info.first_group = 0; 2378 block_opts->info.num_groups = 1; 2379 block_opts->info.ui_hint = SNDRV_UMP_BLOCK_UI_HINT_BOTH; 2380 block_opts->info.active = 1; 2381 2382 ep_opts->blks[blk] = block_opts; 2383 *block_p = block_opts; 2384 2385 out: 2386 mutex_unlock(&ep_opts->opts->lock); 2387 return ret; 2388 } 2389 2390 /* make_group callback for a block */ 2391 static struct config_group * 2392 f_midi2_opts_block_make(struct config_group *group, const char *name) 2393 { 2394 struct f_midi2_ep_opts *ep_opts; 2395 struct f_midi2_block_opts *block_opts; 2396 unsigned int blk; 2397 int ret; 2398 2399 if (strncmp(name, "block.", 6)) 2400 return ERR_PTR(-EINVAL); 2401 ret = kstrtouint(name + 6, 10, &blk); 2402 if (ret) 2403 return ERR_PTR(ret); 2404 2405 ep_opts = to_f_midi2_ep_opts(&group->cg_item); 2406 2407 if (blk >= SNDRV_UMP_MAX_BLOCKS) 2408 return ERR_PTR(-EINVAL); 2409 if (ep_opts->blks[blk]) 2410 return ERR_PTR(-EBUSY); 2411 ret = f_midi2_block_opts_create(ep_opts, blk, &block_opts); 2412 if (ret) 2413 return ERR_PTR(ret); 2414 2415 config_group_init_type_name(&block_opts->group, name, 2416 &f_midi2_block_type); 2417 return &block_opts->group; 2418 } 2419 2420 /* drop_item callback for a block */ 2421 static void 2422 f_midi2_opts_block_drop(struct config_group *group, struct config_item *item) 2423 { 2424 struct f_midi2_block_opts *block_opts = to_f_midi2_block_opts(item); 2425 2426 mutex_lock(&block_opts->ep->opts->lock); 2427 block_opts->ep->blks[block_opts->id] = NULL; 2428 mutex_unlock(&block_opts->ep->opts->lock); 2429 config_item_put(item); 2430 } 2431 2432 /* 2433 * Definitions for UMP Endpoint config 2434 */ 2435 2436 /* define an uint option for EP */ 2437 #define F_MIDI2_EP_OPT(name, format, minval, maxval) \ 2438 static ssize_t f_midi2_ep_opts_##name##_show(struct config_item *item, \ 2439 char *page) \ 2440 { \ 2441 struct f_midi2_ep_opts *opts = to_f_midi2_ep_opts(item); \ 2442 return f_midi2_opts_uint_show(opts->opts, opts->info.name, \ 2443 format "\n", page); \ 2444 } \ 2445 \ 2446 static ssize_t f_midi2_ep_opts_##name##_store(struct config_item *item, \ 2447 const char *page, size_t len)\ 2448 { \ 2449 struct f_midi2_ep_opts *opts = to_f_midi2_ep_opts(item); \ 2450 return f_midi2_opts_uint_store(opts->opts, &opts->info.name, \ 2451 minval, maxval, page, len); \ 2452 } \ 2453 \ 2454 CONFIGFS_ATTR(f_midi2_ep_opts_, name) 2455 2456 /* define a string option for EP */ 2457 #define F_MIDI2_EP_STR_OPT(name, maxlen) \ 2458 static ssize_t f_midi2_ep_opts_##name##_show(struct config_item *item, \ 2459 char *page) \ 2460 { \ 2461 struct f_midi2_ep_opts *opts = to_f_midi2_ep_opts(item); \ 2462 return f_midi2_opts_str_show(opts->opts, &opts->info.name, page);\ 2463 } \ 2464 \ 2465 static ssize_t f_midi2_ep_opts_##name##_store(struct config_item *item, \ 2466 const char *page, size_t len) \ 2467 { \ 2468 struct f_midi2_ep_opts *opts = to_f_midi2_ep_opts(item); \ 2469 return f_midi2_opts_str_store(opts->opts, &opts->info.name, maxlen,\ 2470 page, len); \ 2471 } \ 2472 \ 2473 CONFIGFS_ATTR(f_midi2_ep_opts_, name) 2474 2475 F_MIDI2_EP_OPT(protocol, "0x%x", 1, 2); 2476 F_MIDI2_EP_OPT(protocol_caps, "0x%x", 1, 3); 2477 F_MIDI2_EP_OPT(manufacturer, "0x%x", 0, 0xffffff); 2478 F_MIDI2_EP_OPT(family, "0x%x", 0, 0xffff); 2479 F_MIDI2_EP_OPT(model, "0x%x", 0, 0xffff); 2480 F_MIDI2_EP_OPT(sw_revision, "0x%x", 0, 0xffffffff); 2481 F_MIDI2_EP_STR_OPT(ep_name, 128); 2482 F_MIDI2_EP_STR_OPT(product_id, 128); 2483 2484 static struct configfs_attribute *f_midi2_ep_attrs[] = { 2485 &f_midi2_ep_opts_attr_protocol, 2486 &f_midi2_ep_opts_attr_protocol_caps, 2487 &f_midi2_ep_opts_attr_ep_name, 2488 &f_midi2_ep_opts_attr_product_id, 2489 &f_midi2_ep_opts_attr_manufacturer, 2490 &f_midi2_ep_opts_attr_family, 2491 &f_midi2_ep_opts_attr_model, 2492 &f_midi2_ep_opts_attr_sw_revision, 2493 NULL, 2494 }; 2495 2496 static void f_midi2_ep_opts_release(struct config_item *item) 2497 { 2498 struct f_midi2_ep_opts *opts = to_f_midi2_ep_opts(item); 2499 2500 configfs_remove_default_groups(&opts->group); 2501 kfree(opts->info.ep_name); 2502 kfree(opts->info.product_id); 2503 kfree(opts); 2504 } 2505 2506 static const struct configfs_item_operations f_midi2_ep_item_ops = { 2507 .release = f_midi2_ep_opts_release, 2508 }; 2509 2510 static const struct configfs_group_operations f_midi2_ep_group_ops = { 2511 .make_group = f_midi2_opts_block_make, 2512 .drop_item = f_midi2_opts_block_drop, 2513 }; 2514 2515 static const struct config_item_type f_midi2_ep_type = { 2516 .ct_item_ops = &f_midi2_ep_item_ops, 2517 .ct_group_ops = &f_midi2_ep_group_ops, 2518 .ct_attrs = f_midi2_ep_attrs, 2519 .ct_owner = THIS_MODULE, 2520 }; 2521 2522 /* create a f_midi2_ep_opts instance */ 2523 static int f_midi2_ep_opts_create(struct f_midi2_opts *opts, 2524 unsigned int index, 2525 struct f_midi2_ep_opts **ep_p) 2526 { 2527 struct f_midi2_ep_opts *ep_opts; 2528 2529 ep_opts = kzalloc_obj(*ep_opts); 2530 if (!ep_opts) 2531 return -ENOMEM; 2532 2533 ep_opts->opts = opts; 2534 ep_opts->index = index; 2535 2536 /* set up the default values */ 2537 ep_opts->info.protocol = 2; 2538 ep_opts->info.protocol_caps = 3; 2539 2540 opts->eps[index] = ep_opts; 2541 *ep_p = ep_opts; 2542 return 0; 2543 } 2544 2545 /* make_group callback for an EP */ 2546 static struct config_group * 2547 f_midi2_opts_ep_make(struct config_group *group, const char *name) 2548 { 2549 struct f_midi2_opts *opts; 2550 struct f_midi2_ep_opts *ep_opts; 2551 unsigned int index; 2552 int ret; 2553 2554 if (strncmp(name, "ep.", 3)) 2555 return ERR_PTR(-EINVAL); 2556 ret = kstrtouint(name + 3, 10, &index); 2557 if (ret) 2558 return ERR_PTR(ret); 2559 2560 opts = to_f_midi2_opts(&group->cg_item); 2561 if (index >= MAX_UMP_EPS) 2562 return ERR_PTR(-EINVAL); 2563 if (opts->eps[index]) 2564 return ERR_PTR(-EBUSY); 2565 ret = f_midi2_ep_opts_create(opts, index, &ep_opts); 2566 if (ret) 2567 return ERR_PTR(ret); 2568 2569 config_group_init_type_name(&ep_opts->group, name, &f_midi2_ep_type); 2570 return &ep_opts->group; 2571 } 2572 2573 /* drop_item callback for an EP */ 2574 static void 2575 f_midi2_opts_ep_drop(struct config_group *group, struct config_item *item) 2576 { 2577 struct f_midi2_ep_opts *ep_opts = to_f_midi2_ep_opts(item); 2578 2579 mutex_lock(&ep_opts->opts->lock); 2580 ep_opts->opts->eps[ep_opts->index] = NULL; 2581 mutex_unlock(&ep_opts->opts->lock); 2582 config_item_put(item); 2583 } 2584 2585 /* 2586 * Definitions for card config 2587 */ 2588 2589 /* define a bool option for card */ 2590 #define F_MIDI2_BOOL_OPT(name) \ 2591 static ssize_t f_midi2_opts_##name##_show(struct config_item *item, \ 2592 char *page) \ 2593 { \ 2594 struct f_midi2_opts *opts = to_f_midi2_opts(item); \ 2595 return f_midi2_opts_uint_show(opts, opts->info.name, \ 2596 "%u\n", page); \ 2597 } \ 2598 \ 2599 static ssize_t f_midi2_opts_##name##_store(struct config_item *item, \ 2600 const char *page, size_t len) \ 2601 { \ 2602 struct f_midi2_opts *opts = to_f_midi2_opts(item); \ 2603 return f_midi2_opts_bool_store(opts, &opts->info.name, \ 2604 page, len); \ 2605 } \ 2606 \ 2607 CONFIGFS_ATTR(f_midi2_opts_, name) 2608 2609 F_MIDI2_BOOL_OPT(process_ump); 2610 F_MIDI2_BOOL_OPT(static_block); 2611 2612 static ssize_t f_midi2_opts_iface_name_show(struct config_item *item, 2613 char *page) 2614 { 2615 struct f_midi2_opts *opts = to_f_midi2_opts(item); 2616 2617 return f_midi2_opts_str_show(opts, &opts->info.iface_name, page); 2618 } 2619 2620 static ssize_t f_midi2_opts_iface_name_store(struct config_item *item, 2621 const char *page, size_t len) 2622 { 2623 struct f_midi2_opts *opts = to_f_midi2_opts(item); 2624 2625 return f_midi2_opts_str_store(opts, &opts->info.iface_name, 128, 2626 page, len); 2627 } 2628 2629 CONFIGFS_ATTR(f_midi2_opts_, iface_name); 2630 2631 static struct configfs_attribute *f_midi2_attrs[] = { 2632 &f_midi2_opts_attr_process_ump, 2633 &f_midi2_opts_attr_static_block, 2634 &f_midi2_opts_attr_iface_name, 2635 NULL 2636 }; 2637 2638 static void f_midi2_opts_release(struct config_item *item) 2639 { 2640 struct f_midi2_opts *opts = to_f_midi2_opts(item); 2641 2642 usb_put_function_instance(&opts->func_inst); 2643 } 2644 2645 static const struct configfs_item_operations f_midi2_item_ops = { 2646 .release = f_midi2_opts_release, 2647 }; 2648 2649 static const struct configfs_group_operations f_midi2_group_ops = { 2650 .make_group = f_midi2_opts_ep_make, 2651 .drop_item = f_midi2_opts_ep_drop, 2652 }; 2653 2654 static const struct config_item_type f_midi2_func_type = { 2655 .ct_item_ops = &f_midi2_item_ops, 2656 .ct_group_ops = &f_midi2_group_ops, 2657 .ct_attrs = f_midi2_attrs, 2658 .ct_owner = THIS_MODULE, 2659 }; 2660 2661 static void f_midi2_free_inst(struct usb_function_instance *f) 2662 { 2663 struct f_midi2_opts *opts; 2664 2665 opts = container_of(f, struct f_midi2_opts, func_inst); 2666 2667 configfs_remove_default_groups(&opts->func_inst.group); 2668 kfree(opts->info.iface_name); 2669 kfree(opts); 2670 } 2671 2672 /* gadget alloc_inst */ 2673 static struct usb_function_instance *f_midi2_alloc_inst(void) 2674 { 2675 struct f_midi2_opts *opts; 2676 struct f_midi2_ep_opts *ep_opts; 2677 struct f_midi2_block_opts *block_opts; 2678 int ret; 2679 2680 opts = kzalloc_obj(*opts); 2681 if (!opts) 2682 return ERR_PTR(-ENOMEM); 2683 2684 mutex_init(&opts->lock); 2685 opts->func_inst.free_func_inst = f_midi2_free_inst; 2686 opts->info.process_ump = true; 2687 opts->info.static_block = true; 2688 opts->info.num_reqs = 32; 2689 opts->info.req_buf_size = 512; 2690 2691 /* create the default ep */ 2692 ret = f_midi2_ep_opts_create(opts, 0, &ep_opts); 2693 if (ret) { 2694 kfree(opts); 2695 return ERR_PTR(ret); 2696 } 2697 2698 /* create the default block */ 2699 ret = f_midi2_block_opts_create(ep_opts, 0, &block_opts); 2700 if (ret) { 2701 kfree(ep_opts); 2702 kfree(opts); 2703 return ERR_PTR(ret); 2704 } 2705 2706 /* set up the default MIDI1 (that is mandatory) */ 2707 block_opts->info.midi1_num_groups = 1; 2708 2709 config_group_init_type_name(&opts->func_inst.group, "", 2710 &f_midi2_func_type); 2711 2712 config_group_init_type_name(&ep_opts->group, "ep.0", 2713 &f_midi2_ep_type); 2714 configfs_add_default_group(&ep_opts->group, &opts->func_inst.group); 2715 2716 config_group_init_type_name(&block_opts->group, "block.0", 2717 &f_midi2_block_type); 2718 configfs_add_default_group(&block_opts->group, &ep_opts->group); 2719 2720 return &opts->func_inst; 2721 } 2722 2723 static void do_f_midi2_free(struct f_midi2 *midi2, struct f_midi2_opts *opts) 2724 { 2725 mutex_lock(&opts->lock); 2726 --opts->refcnt; 2727 mutex_unlock(&opts->lock); 2728 kfree(midi2->string_defs); 2729 f_midi2_free_strings(midi2); 2730 kfree(midi2); 2731 } 2732 2733 static void f_midi2_free(struct usb_function *f) 2734 { 2735 do_f_midi2_free(func_to_midi2(f), 2736 container_of(f->fi, struct f_midi2_opts, func_inst)); 2737 } 2738 2739 /* verify the parameters set up via configfs; 2740 * return the number of EPs or a negative error 2741 */ 2742 static int verify_parameters(struct f_midi2_opts *opts) 2743 { 2744 int i, j, num_eps, num_blks; 2745 struct f_midi2_ep_info *ep; 2746 struct f_midi2_block_info *bp; 2747 2748 for (num_eps = 0; num_eps < MAX_UMP_EPS && opts->eps[num_eps]; 2749 num_eps++) 2750 ; 2751 if (!num_eps) { 2752 pr_err("f_midi2: No EP is defined\n"); 2753 return -EINVAL; 2754 } 2755 2756 num_blks = 0; 2757 for (i = 0; i < num_eps; i++) { 2758 ep = &opts->eps[i]->info; 2759 if (!(ep->protocol_caps & ep->protocol)) { 2760 pr_err("f_midi2: Invalid protocol 0x%x (caps 0x%x) for EP %d\n", 2761 ep->protocol, ep->protocol_caps, i); 2762 return -EINVAL; 2763 } 2764 2765 for (j = 0; j < SNDRV_UMP_MAX_BLOCKS && opts->eps[i]->blks[j]; 2766 j++, num_blks++) { 2767 bp = &opts->eps[i]->blks[j]->info; 2768 if (bp->first_group + bp->num_groups > SNDRV_UMP_MAX_GROUPS) { 2769 pr_err("f_midi2: Invalid group definitions for block %d:%d\n", 2770 i, j); 2771 return -EINVAL; 2772 } 2773 2774 if (bp->midi1_num_groups) { 2775 if (bp->midi1_first_group < bp->first_group || 2776 bp->midi1_first_group + bp->midi1_num_groups > 2777 bp->first_group + bp->num_groups) { 2778 pr_err("f_midi2: Invalid MIDI1 group definitions for block %d:%d\n", 2779 i, j); 2780 return -EINVAL; 2781 } 2782 } 2783 } 2784 } 2785 if (!num_blks) { 2786 pr_err("f_midi2: No block is defined\n"); 2787 return -EINVAL; 2788 } 2789 2790 return num_eps; 2791 } 2792 2793 /* fill mapping between MIDI 1.0 cable and UMP EP/group */ 2794 static void fill_midi1_cable_mapping(struct f_midi2 *midi2, 2795 struct f_midi2_ep *ep, 2796 int blk) 2797 { 2798 const struct f_midi2_block_info *binfo = &ep->blks[blk].info; 2799 struct midi1_cable_mapping *map; 2800 int i, group; 2801 2802 if (!binfo->midi1_num_groups) 2803 return; 2804 if (binfo->direction != SNDRV_UMP_DIR_OUTPUT) { 2805 group = binfo->midi1_first_group; 2806 map = midi2->in_cable_mapping + midi2->num_midi1_in; 2807 for (i = 0; i < binfo->midi1_num_groups; i++, group++, map++) { 2808 if (midi2->num_midi1_in >= MAX_CABLES) 2809 break; 2810 map->ep = ep; 2811 map->block = blk; 2812 map->group = group; 2813 midi2->num_midi1_in++; 2814 /* store 1-based cable number */ 2815 ep->in_group_to_cable[group] = midi2->num_midi1_in; 2816 } 2817 } 2818 2819 if (binfo->direction != SNDRV_UMP_DIR_INPUT) { 2820 group = binfo->midi1_first_group; 2821 map = midi2->out_cable_mapping + midi2->num_midi1_out; 2822 for (i = 0; i < binfo->midi1_num_groups; i++, group++, map++) { 2823 if (midi2->num_midi1_out >= MAX_CABLES) 2824 break; 2825 map->ep = ep; 2826 map->block = blk; 2827 map->group = group; 2828 midi2->num_midi1_out++; 2829 } 2830 } 2831 } 2832 2833 /* gadget alloc callback */ 2834 static struct usb_function *f_midi2_alloc(struct usb_function_instance *fi) 2835 { 2836 struct f_midi2 *midi2; 2837 struct f_midi2_opts *opts; 2838 struct f_midi2_ep *ep; 2839 struct f_midi2_block *bp; 2840 int i, num_eps, blk, ret; 2841 2842 midi2 = kzalloc_obj(*midi2); 2843 if (!midi2) 2844 return ERR_PTR(-ENOMEM); 2845 2846 opts = container_of(fi, struct f_midi2_opts, func_inst); 2847 mutex_lock(&opts->lock); 2848 num_eps = verify_parameters(opts); 2849 if (num_eps < 0) { 2850 mutex_unlock(&opts->lock); 2851 kfree(midi2); 2852 return ERR_PTR(num_eps); 2853 } 2854 ++opts->refcnt; 2855 2856 spin_lock_init(&midi2->queue_lock); 2857 2858 midi2->func.name = "midi2_func"; 2859 midi2->func.bind = f_midi2_bind; 2860 midi2->func.unbind = f_midi2_unbind; 2861 midi2->func.get_alt = f_midi2_get_alt; 2862 midi2->func.set_alt = f_midi2_set_alt; 2863 midi2->func.setup = f_midi2_setup; 2864 midi2->func.disable = f_midi2_disable; 2865 midi2->func.free_func = f_midi2_free; 2866 2867 midi2->info = opts->info; 2868 midi2->num_eps = num_eps; 2869 ret = f_midi2_copy_string(&midi2->info.iface_name, 2870 opts->info.iface_name); 2871 if (ret) 2872 goto err_unlock; 2873 2874 for (i = 0; i < num_eps; i++) { 2875 ep = &midi2->midi2_eps[i]; 2876 ep->info = opts->eps[i]->info; 2877 ep->info.ep_name = NULL; 2878 ep->info.product_id = NULL; 2879 ret = f_midi2_copy_string(&ep->info.ep_name, 2880 opts->eps[i]->info.ep_name); 2881 if (ret) 2882 goto err_unlock; 2883 ret = f_midi2_copy_string(&ep->info.product_id, 2884 opts->eps[i]->info.product_id); 2885 if (ret) 2886 goto err_unlock; 2887 ep->card = midi2; 2888 for (blk = 0; blk < SNDRV_UMP_MAX_BLOCKS && 2889 opts->eps[i]->blks[blk]; blk++) { 2890 bp = &ep->blks[blk]; 2891 ep->num_blks++; 2892 bp->info = opts->eps[i]->blks[blk]->info; 2893 ret = f_midi2_copy_string(&bp->info.name, 2894 opts->eps[i]->blks[blk]->info.name); 2895 if (ret) 2896 goto err_unlock; 2897 bp->gtb_id = ++midi2->total_blocks; 2898 } 2899 } 2900 mutex_unlock(&opts->lock); 2901 2902 midi2->string_defs = kzalloc_objs(*midi2->string_defs, 2903 midi2->total_blocks + 1); 2904 if (!midi2->string_defs) { 2905 do_f_midi2_free(midi2, opts); 2906 return ERR_PTR(-ENOMEM); 2907 } 2908 2909 if (midi2->info.iface_name && *midi2->info.iface_name) 2910 midi2->string_defs[STR_IFACE].s = midi2->info.iface_name; 2911 else 2912 midi2->string_defs[STR_IFACE].s = ump_ep_name(&midi2->midi2_eps[0]); 2913 2914 for (i = 0; i < midi2->num_eps; i++) { 2915 ep = &midi2->midi2_eps[i]; 2916 for (blk = 0; blk < ep->num_blks; blk++) { 2917 bp = &ep->blks[blk]; 2918 midi2->string_defs[gtb_to_str_id(bp->gtb_id)].s = 2919 ump_fb_name(&bp->info); 2920 2921 fill_midi1_cable_mapping(midi2, ep, blk); 2922 } 2923 } 2924 2925 if (!midi2->num_midi1_in && !midi2->num_midi1_out) { 2926 pr_err("f_midi2: MIDI1 definition is missing\n"); 2927 do_f_midi2_free(midi2, opts); 2928 return ERR_PTR(-EINVAL); 2929 } 2930 2931 return &midi2->func; 2932 2933 err_unlock: 2934 mutex_unlock(&opts->lock); 2935 do_f_midi2_free(midi2, opts); 2936 return ERR_PTR(ret); 2937 } 2938 2939 DECLARE_USB_FUNCTION_INIT(midi2, f_midi2_alloc_inst, f_midi2_alloc); 2940 2941 MODULE_DESCRIPTION("USB MIDI 2.0 class function driver"); 2942 MODULE_LICENSE("GPL"); 2943