1 // SPDX-License-Identifier: GPL-2.0+
3 * f_midi2.c -- USB MIDI 2.0 class function driver
6 #include <linux/device.h>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/slab.h>
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>
17 #include <linux/usb/ch9.h>
18 #include <linux/usb/gadget.h>
19 #include <linux/usb/audio.h>
20 #include <linux/usb/midi-v2.h>
27 struct f_midi2_usb_ep;
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 */
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 */
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 */
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 */
63 /* MIDI 1.0 message states */
65 STATE_INITIAL = 0, /* pseudo state */
73 STATE_FINISHED, /* pseudo state */
76 /* Resources for UMP Endpoint */
78 struct snd_ump_endpoint *ump; /* assigned UMP EP */
79 struct f_midi2 *card; /* belonging MIDI 2.0 device */
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 */
85 struct f_midi2_usb_ep ep_in; /* USB MIDI EP-in */
86 struct f_midi2_usb_ep ep_out; /* USB MIDI EP-out */
88 u8 in_group_to_cable[SNDRV_UMP_MAX_GROUPS]; /* map to cable; 1-based! */
91 /* indices for USB strings */
97 /* 1-based GTB id to string id */
98 #define gtb_to_str_id(id) (STR_GTB1 + (id) - 1)
100 /* mapping from MIDI 1.0 cable to UMP group */
101 struct midi1_cable_mapping {
102 struct f_midi2_ep *ep;
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 */
114 /* Resources for MIDI 2.0 Device */
116 struct usb_function func;
117 struct usb_gadget *gadget;
118 struct snd_card *card;
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;
124 /* number of MIDI 1.0 I/O cables */
125 unsigned int num_midi1_in;
126 unsigned int num_midi1_out;
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];
136 int midi_if; /* USB MIDI interface number */
137 int operation_mode; /* current operation mode */
139 spinlock_t queue_lock;
141 struct f_midi2_card_info info; /* card info, copied from configfs */
143 unsigned int num_eps;
144 struct f_midi2_ep midi2_eps[MAX_UMP_EPS];
146 unsigned int total_blocks; /* total number of blocks of all EPs */
147 struct usb_string *string_defs;
148 struct usb_string *strings;
151 #define func_to_midi2(f) container_of(f, struct f_midi2, func)
153 /* get EP name string */
154 static const char *ump_ep_name(const struct f_midi2_ep *ep)
156 return ep->info.ep_name ? ep->info.ep_name : "MIDI 2.0 Gadget";
159 /* get EP product ID string */
160 static const char *ump_product_id(const struct f_midi2_ep *ep)
162 return ep->info.product_id ? ep->info.product_id : "Unique Product ID";
165 /* get FB name string */
166 static const char *ump_fb_name(const struct f_midi2_block_info *info)
168 return info->name ? info->name : "MIDI 2.0 Gadget I/O";
172 * USB Descriptor Definitions
174 /* GTB header descriptor */
175 static struct usb_ms20_gr_trm_block_header_descriptor gtb_header_desc = {
176 .bLength = sizeof(gtb_header_desc),
177 .bDescriptorType = USB_DT_CS_GR_TRM_BLOCK,
178 .bDescriptorSubtype = USB_MS_GR_TRM_BLOCK_HEADER,
179 .wTotalLength = __cpu_to_le16(0x12), // to be filled
182 /* GTB descriptor template: most items are replaced dynamically */
183 static struct usb_ms20_gr_trm_block_descriptor gtb_desc = {
184 .bLength = sizeof(gtb_desc),
185 .bDescriptorType = USB_DT_CS_GR_TRM_BLOCK,
186 .bDescriptorSubtype = USB_MS_GR_TRM_BLOCK,
187 .bGrpTrmBlkID = 0x01,
188 .bGrpTrmBlkType = USB_MS_GR_TRM_BLOCK_TYPE_BIDIRECTIONAL,
192 .bMIDIProtocol = USB_MS_MIDI_PROTO_1_0_64,
193 .wMaxInputBandwidth = 0,
194 .wMaxOutputBandwidth = 0,
197 DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1);
198 DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(16);
199 DECLARE_UAC_AC_HEADER_DESCRIPTOR(1);
200 DECLARE_USB_MS20_ENDPOINT_DESCRIPTOR(32);
202 #define EP_MAX_PACKET_INT 8
204 /* Audio Control Interface */
205 static struct usb_interface_descriptor midi2_audio_if_desc = {
206 .bLength = USB_DT_INTERFACE_SIZE,
207 .bDescriptorType = USB_DT_INTERFACE,
208 .bInterfaceNumber = 0, // to be filled
210 .bInterfaceClass = USB_CLASS_AUDIO,
211 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
212 .bInterfaceProtocol = 0,
216 static struct uac1_ac_header_descriptor_1 midi2_audio_class_desc = {
218 .bDescriptorType = USB_DT_CS_INTERFACE,
219 .bDescriptorSubtype = 0x01,
220 .bcdADC = __cpu_to_le16(0x0100),
221 .wTotalLength = __cpu_to_le16(0x0009),
222 .bInCollection = 0x01,
223 .baInterfaceNr = { 0x01 }, // to be filled
226 /* MIDI 1.0 Streaming Interface (altset 0) */
227 static struct usb_interface_descriptor midi2_midi1_if_desc = {
228 .bLength = USB_DT_INTERFACE_SIZE,
229 .bDescriptorType = USB_DT_INTERFACE,
230 .bInterfaceNumber = 0, // to be filled
231 .bAlternateSetting = 0,
232 .bNumEndpoints = 2, // to be filled
233 .bInterfaceClass = USB_CLASS_AUDIO,
234 .bInterfaceSubClass = USB_SUBCLASS_MIDISTREAMING,
235 .bInterfaceProtocol = 0,
236 .iInterface = 0, // to be filled
239 static struct usb_ms_header_descriptor midi2_midi1_class_desc = {
241 .bDescriptorType = USB_DT_CS_INTERFACE,
242 .bDescriptorSubtype = USB_MS_HEADER,
243 .bcdMSC = __cpu_to_le16(0x0100),
244 .wTotalLength = __cpu_to_le16(0x41), // to be calculated
247 /* MIDI 1.0 EP OUT */
248 static struct usb_endpoint_descriptor midi2_midi1_ep_out_desc = {
249 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
250 .bDescriptorType = USB_DT_ENDPOINT,
251 .bEndpointAddress = USB_DIR_OUT | 0, // set up dynamically
252 .bmAttributes = USB_ENDPOINT_XFER_BULK,
255 static struct usb_ss_ep_comp_descriptor midi2_midi1_ep_out_ss_comp_desc = {
256 .bLength = sizeof(midi2_midi1_ep_out_ss_comp_desc),
257 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
260 static struct usb_ms_endpoint_descriptor_16 midi2_midi1_ep_out_class_desc = {
261 .bLength = 0x05, // to be filled
262 .bDescriptorType = USB_DT_CS_ENDPOINT,
263 .bDescriptorSubtype = USB_MS_GENERAL,
264 .bNumEmbMIDIJack = 1,
265 .baAssocJackID = { 0x01 },
269 static struct usb_endpoint_descriptor midi2_midi1_ep_in_desc = {
270 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
271 .bDescriptorType = USB_DT_ENDPOINT,
272 .bEndpointAddress = USB_DIR_IN | 0, // set up dynamically
273 .bmAttributes = USB_ENDPOINT_XFER_BULK,
276 static struct usb_ss_ep_comp_descriptor midi2_midi1_ep_in_ss_comp_desc = {
277 .bLength = sizeof(midi2_midi1_ep_in_ss_comp_desc),
278 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
281 static struct usb_ms_endpoint_descriptor_16 midi2_midi1_ep_in_class_desc = {
282 .bLength = 0x05, // to be filled
283 .bDescriptorType = USB_DT_CS_ENDPOINT,
284 .bDescriptorSubtype = USB_MS_GENERAL,
285 .bNumEmbMIDIJack = 1,
286 .baAssocJackID = { 0x03 },
289 /* MIDI 2.0 Streaming Interface (altset 1) */
290 static struct usb_interface_descriptor midi2_midi2_if_desc = {
291 .bLength = USB_DT_INTERFACE_SIZE,
292 .bDescriptorType = USB_DT_INTERFACE,
293 .bInterfaceNumber = 0, // to be filled
294 .bAlternateSetting = 1,
295 .bNumEndpoints = 2, // to be filled
296 .bInterfaceClass = USB_CLASS_AUDIO,
297 .bInterfaceSubClass = USB_SUBCLASS_MIDISTREAMING,
298 .bInterfaceProtocol = 0,
299 .iInterface = 0, // to be filled
302 static struct usb_ms_header_descriptor midi2_midi2_class_desc = {
304 .bDescriptorType = USB_DT_CS_INTERFACE,
305 .bDescriptorSubtype = USB_MS_HEADER,
306 .bcdMSC = __cpu_to_le16(0x0200),
307 .wTotalLength = __cpu_to_le16(0x07),
310 /* MIDI 2.0 EP OUT */
311 static struct usb_endpoint_descriptor midi2_midi2_ep_out_desc[MAX_UMP_EPS];
313 static struct usb_ss_ep_comp_descriptor midi2_midi2_ep_out_ss_comp_desc = {
314 .bLength = sizeof(midi2_midi1_ep_out_ss_comp_desc),
315 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
318 static struct usb_ms20_endpoint_descriptor_32 midi2_midi2_ep_out_class_desc[MAX_UMP_EPS];
321 static struct usb_endpoint_descriptor midi2_midi2_ep_in_desc[MAX_UMP_EPS];
323 static struct usb_ss_ep_comp_descriptor midi2_midi2_ep_in_ss_comp_desc = {
324 .bLength = sizeof(midi2_midi2_ep_in_ss_comp_desc),
325 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
328 static struct usb_ms20_endpoint_descriptor_32 midi2_midi2_ep_in_class_desc[MAX_UMP_EPS];
330 /* Arrays of descriptors to be created */
331 static void *midi2_audio_descs[] = {
332 &midi2_audio_if_desc,
333 &midi2_audio_class_desc,
337 static void *midi2_midi1_descs[] = {
338 &midi2_midi1_if_desc,
339 &midi2_midi1_class_desc,
343 static void *midi2_midi1_ep_out_descs[] = {
344 &midi2_midi1_ep_out_desc,
345 &midi2_midi1_ep_out_class_desc,
349 static void *midi2_midi1_ep_in_descs[] = {
350 &midi2_midi1_ep_in_desc,
351 &midi2_midi1_ep_in_class_desc,
355 static void *midi2_midi1_ep_out_ss_descs[] = {
356 &midi2_midi1_ep_out_desc,
357 &midi2_midi1_ep_out_ss_comp_desc,
358 &midi2_midi1_ep_out_class_desc,
362 static void *midi2_midi1_ep_in_ss_descs[] = {
363 &midi2_midi1_ep_in_desc,
364 &midi2_midi1_ep_in_ss_comp_desc,
365 &midi2_midi1_ep_in_class_desc,
369 static void *midi2_midi2_descs[] = {
370 &midi2_midi2_if_desc,
371 &midi2_midi2_class_desc,
376 * USB request handling
379 /* get an empty request for the given EP */
380 static struct usb_request *get_empty_request(struct f_midi2_usb_ep *usb_ep)
382 struct usb_request *req = NULL;
386 spin_lock_irqsave(&usb_ep->card->queue_lock, flags);
387 if (!usb_ep->free_reqs)
389 index = find_first_bit(&usb_ep->free_reqs, usb_ep->num_reqs);
390 if (index >= usb_ep->num_reqs)
392 req = usb_ep->reqs[index].req;
395 clear_bit(index, &usb_ep->free_reqs);
398 spin_unlock_irqrestore(&usb_ep->card->queue_lock, flags);
402 /* put the empty request back */
403 static void put_empty_request(struct usb_request *req)
405 struct f_midi2_req_ctx *ctx = req->context;
408 spin_lock_irqsave(&ctx->usb_ep->card->queue_lock, flags);
409 set_bit(ctx->index, &ctx->usb_ep->free_reqs);
410 spin_unlock_irqrestore(&ctx->usb_ep->card->queue_lock, flags);
414 * UMP v1.1 Stream message handling
417 /* queue a request to UMP EP; request is either queued or freed after this */
418 static int queue_request_ep_raw(struct usb_request *req)
420 struct f_midi2_req_ctx *ctx = req->context;
423 req->complete = ctx->usb_ep->complete;
424 err = usb_ep_queue(ctx->usb_ep->usb_ep, req, GFP_ATOMIC);
426 put_empty_request(req);
432 /* queue a request with endianness conversion */
433 static int queue_request_ep_in(struct usb_request *req)
435 /* UMP packets have to be converted to little-endian */
436 cpu_to_le32_array((u32 *)req->buf, req->length >> 2);
437 return queue_request_ep_raw(req);
440 /* reply a UMP packet via EP-in */
441 static int reply_ep_in(struct f_midi2_ep *ep, const void *buf, int len)
443 struct f_midi2_usb_ep *usb_ep = &ep->ep_in;
444 struct usb_request *req;
446 req = get_empty_request(usb_ep);
451 memcpy(req->buf, buf, len);
452 return queue_request_ep_in(req);
455 /* reply a UMP stream EP info */
456 static void reply_ump_stream_ep_info(struct f_midi2_ep *ep)
458 struct snd_ump_stream_msg_ep_info rep = {
459 .type = UMP_MSG_TYPE_STREAM,
460 .status = UMP_STREAM_MSG_STATUS_EP_INFO,
461 .ump_version_major = 0x01,
462 .ump_version_minor = 0x01,
463 .num_function_blocks = ep->num_blks,
464 .static_function_block = !!ep->card->info.static_block,
465 .protocol = (UMP_STREAM_MSG_EP_INFO_CAP_MIDI1 |
466 UMP_STREAM_MSG_EP_INFO_CAP_MIDI2) >> 8,
469 reply_ep_in(ep, &rep, sizeof(rep));
472 /* reply a UMP EP device info */
473 static void reply_ump_stream_ep_device(struct f_midi2_ep *ep)
475 struct snd_ump_stream_msg_devince_info rep = {
476 .type = UMP_MSG_TYPE_STREAM,
477 .status = UMP_STREAM_MSG_STATUS_DEVICE_INFO,
478 .manufacture_id = ep->info.manufacturer,
479 .family_lsb = ep->info.family & 0xff,
480 .family_msb = (ep->info.family >> 8) & 0xff,
481 .model_lsb = ep->info.model & 0xff,
482 .model_msb = (ep->info.model >> 8) & 0xff,
483 .sw_revision = ep->info.sw_revision,
486 reply_ep_in(ep, &rep, sizeof(rep));
489 #define UMP_STREAM_PKT_BYTES 16 /* UMP stream packet size = 16 bytes*/
490 #define UMP_STREAM_EP_STR_OFF 2 /* offset of name string for EP info */
491 #define UMP_STREAM_FB_STR_OFF 3 /* offset of name string for FB info */
493 /* Helper to replay a string */
494 static void reply_ump_stream_string(struct f_midi2_ep *ep, const u8 *name,
495 unsigned int type, unsigned int extra,
496 unsigned int start_ofs)
498 struct f_midi2_usb_ep *usb_ep = &ep->ep_in;
499 struct f_midi2 *midi2 = ep->card;
500 struct usb_request *req;
506 req = get_empty_request(usb_ep);
510 buf = (u32 *)req->buf;
513 if (pos == start_ofs) {
514 memset(buf, 0, UMP_STREAM_PKT_BYTES);
515 buf[0] = ump_stream_compose(type, 0) | extra;
517 buf[pos / 4] |= *name++ << ((3 - (pos % 4)) * 8);
520 buf[0] |= UMP_STREAM_MSG_FORMAT_END << 26;
521 req->length += UMP_STREAM_PKT_BYTES;
524 if (++pos == UMP_STREAM_PKT_BYTES) {
526 buf[0] |= UMP_STREAM_MSG_FORMAT_START << 26;
528 buf[0] |= UMP_STREAM_MSG_FORMAT_CONTINUE << 26;
529 req->length += UMP_STREAM_PKT_BYTES;
530 if (midi2->info.req_buf_size - req->length < UMP_STREAM_PKT_BYTES)
538 queue_request_ep_in(req);
540 put_empty_request(req);
543 /* Reply a UMP EP name string */
544 static void reply_ump_stream_ep_name(struct f_midi2_ep *ep)
546 reply_ump_stream_string(ep, ump_ep_name(ep),
547 UMP_STREAM_MSG_STATUS_EP_NAME, 0,
548 UMP_STREAM_EP_STR_OFF);
551 /* Reply a UMP EP product ID string */
552 static void reply_ump_stream_ep_pid(struct f_midi2_ep *ep)
554 reply_ump_stream_string(ep, ump_product_id(ep),
555 UMP_STREAM_MSG_STATUS_PRODUCT_ID, 0,
556 UMP_STREAM_EP_STR_OFF);
559 /* Reply a UMP EP stream config */
560 static void reply_ump_stream_ep_config(struct f_midi2_ep *ep)
562 struct snd_ump_stream_msg_stream_cfg rep = {
563 .type = UMP_MSG_TYPE_STREAM,
564 .status = UMP_STREAM_MSG_STATUS_STREAM_CFG,
567 if ((ep->info.protocol & SNDRV_UMP_EP_INFO_PROTO_MIDI_MASK) ==
568 SNDRV_UMP_EP_INFO_PROTO_MIDI2)
569 rep.protocol = UMP_STREAM_MSG_EP_INFO_CAP_MIDI2 >> 8;
571 rep.protocol = UMP_STREAM_MSG_EP_INFO_CAP_MIDI1 >> 8;
573 reply_ep_in(ep, &rep, sizeof(rep));
576 /* Reply a UMP FB info */
577 static void reply_ump_stream_fb_info(struct f_midi2_ep *ep, int blk)
579 struct f_midi2_block_info *b = &ep->blks[blk].info;
580 struct snd_ump_stream_msg_fb_info rep = {
581 .type = UMP_MSG_TYPE_STREAM,
582 .status = UMP_STREAM_MSG_STATUS_FB_INFO,
583 .active = !!b->active,
584 .function_block_id = blk,
585 .ui_hint = b->ui_hint,
586 .midi_10 = b->is_midi1,
587 .direction = b->direction,
588 .first_group = b->first_group,
589 .num_groups = b->num_groups,
590 .midi_ci_version = b->midi_ci_version,
591 .sysex8_streams = b->sysex8_streams,
594 reply_ep_in(ep, &rep, sizeof(rep));
597 /* Reply a FB name string */
598 static void reply_ump_stream_fb_name(struct f_midi2_ep *ep, unsigned int blk)
600 reply_ump_stream_string(ep, ump_fb_name(&ep->blks[blk].info),
601 UMP_STREAM_MSG_STATUS_FB_NAME, blk << 8,
602 UMP_STREAM_FB_STR_OFF);
605 /* Process a UMP Stream message */
606 static void process_ump_stream_msg(struct f_midi2_ep *ep, const u32 *data)
608 struct f_midi2 *midi2 = ep->card;
609 unsigned int format, status, blk;
611 format = ump_stream_message_format(*data);
612 status = ump_stream_message_status(*data);
614 case UMP_STREAM_MSG_STATUS_EP_DISCOVERY:
617 if (data[1] & UMP_STREAM_MSG_REQUEST_EP_INFO)
618 reply_ump_stream_ep_info(ep);
619 if (data[1] & UMP_STREAM_MSG_REQUEST_DEVICE_INFO)
620 reply_ump_stream_ep_device(ep);
621 if (data[1] & UMP_STREAM_MSG_REQUEST_EP_NAME)
622 reply_ump_stream_ep_name(ep);
623 if (data[1] & UMP_STREAM_MSG_REQUEST_PRODUCT_ID)
624 reply_ump_stream_ep_pid(ep);
625 if (data[1] & UMP_STREAM_MSG_REQUEST_STREAM_CFG)
626 reply_ump_stream_ep_config(ep);
628 case UMP_STREAM_MSG_STATUS_STREAM_CFG_REQUEST:
629 if (*data & UMP_STREAM_MSG_EP_INFO_CAP_MIDI2) {
630 ep->info.protocol = SNDRV_UMP_EP_INFO_PROTO_MIDI2;
631 DBG(midi2, "Switching Protocol to MIDI2\n");
633 ep->info.protocol = SNDRV_UMP_EP_INFO_PROTO_MIDI1;
634 DBG(midi2, "Switching Protocol to MIDI1\n");
636 snd_ump_switch_protocol(ep->ump, ep->info.protocol);
637 reply_ump_stream_ep_config(ep);
639 case UMP_STREAM_MSG_STATUS_FB_DISCOVERY:
642 blk = (*data >> 8) & 0xff;
643 if (blk >= ep->num_blks)
645 if (*data & UMP_STREAM_MSG_REQUEST_FB_INFO)
646 reply_ump_stream_fb_info(ep, blk);
647 if (*data & UMP_STREAM_MSG_REQUEST_FB_NAME)
648 reply_ump_stream_fb_name(ep, blk);
653 /* Process UMP messages included in a USB request */
654 static void process_ump(struct f_midi2_ep *ep, const struct usb_request *req)
656 const u32 *data = (u32 *)req->buf;
657 int len = req->actual >> 2;
658 const u32 *in_buf = ep->ump->input_buf;
660 for (; len > 0; len--, data++) {
661 if (snd_ump_receive_ump_val(ep->ump, *data) <= 0)
663 if (ump_message_type(*in_buf) == UMP_MSG_TYPE_STREAM)
664 process_ump_stream_msg(ep, in_buf);
669 * MIDI 2.0 UMP USB request handling
672 /* complete handler for UMP EP-out requests */
673 static void f_midi2_ep_out_complete(struct usb_ep *usb_ep,
674 struct usb_request *req)
676 struct f_midi2_req_ctx *ctx = req->context;
677 struct f_midi2_ep *ep = ctx->usb_ep->ep;
678 struct f_midi2 *midi2 = ep->card;
679 int status = req->status;
682 DBG(midi2, "%s complete error %d: %d/%d\n",
683 usb_ep->name, status, req->actual, req->length);
687 /* convert to UMP packet in native endianness */
688 le32_to_cpu_array((u32 *)req->buf, req->actual >> 2);
690 if (midi2->info.process_ump)
691 process_ump(ep, req);
693 snd_ump_receive(ep->ump, req->buf, req->actual & ~3);
695 if (midi2->operation_mode != MIDI_OP_MODE_MIDI2)
698 if (queue_request_ep_raw(req))
703 put_empty_request(req);
706 /* Transmit UMP packets received from user-space to the gadget */
707 static void process_ump_transmit(struct f_midi2_ep *ep)
709 struct f_midi2_usb_ep *usb_ep = &ep->ep_in;
710 struct f_midi2 *midi2 = ep->card;
711 struct usb_request *req;
714 if (!usb_ep->usb_ep->enabled)
718 req = get_empty_request(usb_ep);
721 len = snd_ump_transmit(ep->ump, (u32 *)req->buf,
722 midi2->info.req_buf_size);
724 put_empty_request(req);
729 if (queue_request_ep_in(req) < 0)
734 /* Complete handler for UMP EP-in requests */
735 static void f_midi2_ep_in_complete(struct usb_ep *usb_ep,
736 struct usb_request *req)
738 struct f_midi2_req_ctx *ctx = req->context;
739 struct f_midi2_ep *ep = ctx->usb_ep->ep;
740 struct f_midi2 *midi2 = ep->card;
741 int status = req->status;
743 put_empty_request(req);
746 DBG(midi2, "%s complete error %d: %d/%d\n",
747 usb_ep->name, status, req->actual, req->length);
751 process_ump_transmit(ep);
755 * MIDI1 (altset 0) USB request handling
758 /* process one MIDI byte -- copied from f_midi.c
760 * fill the packet or request if needed
761 * returns true if the request became empty (queued)
763 static bool process_midi1_byte(struct f_midi2 *midi2, u8 cable, u8 b,
764 struct usb_request **req_p)
766 struct f_midi2_midi1_port *port = &midi2->midi1_port[cable];
767 u8 p[4] = { cable << 4, 0, 0, 0 };
768 int next_state = STATE_INITIAL;
769 struct usb_request *req = *req_p;
773 /* System Real-Time Messages */
776 next_state = port->state;
777 port->state = STATE_REAL_TIME;
782 switch (port->state) {
786 next_state = STATE_FINISHED;
790 p[1] = port->data[0];
792 next_state = STATE_FINISHED;
796 p[1] = port->data[0];
797 p[2] = port->data[1];
799 next_state = STATE_FINISHED;
803 next_state = port->state;
804 port->state = STATE_INITIAL;
809 /* System Common Messages */
810 port->data[0] = port->data[1] = 0;
811 port->state = STATE_INITIAL;
816 next_state = STATE_SYSEX_1;
821 next_state = STATE_1PARAM;
825 next_state = STATE_2PARAM_1;
829 next_state = STATE_INITIAL;
834 next_state = STATE_FINISHED;
841 * Channel Voice Messages, Channel Mode Messages
842 * and Control Change Messages.
846 port->state = STATE_INITIAL;
847 if (b >= 0xc0 && b <= 0xdf)
848 next_state = STATE_1PARAM;
850 next_state = STATE_2PARAM_1;
854 /* Message parameters */
855 switch (port->state) {
857 if (port->data[0] < 0xf0)
858 p[0] |= port->data[0] >> 4;
862 p[1] = port->data[0];
864 /* This is to allow Running State Messages */
865 next_state = STATE_1PARAM;
869 next_state = STATE_2PARAM_2;
872 if (port->data[0] < 0xf0)
873 p[0] |= port->data[0] >> 4;
877 p[1] = port->data[0];
878 p[2] = port->data[1];
880 /* This is to allow Running State Messages */
881 next_state = STATE_2PARAM_1;
885 next_state = STATE_SYSEX_1;
889 next_state = STATE_SYSEX_2;
893 p[1] = port->data[0];
894 p[2] = port->data[1];
896 next_state = STATE_SYSEX_0;
902 /* States where we have to write into the USB request */
903 if (next_state == STATE_FINISHED ||
904 port->state == STATE_SYSEX_2 ||
905 port->state == STATE_1PARAM ||
906 port->state == STATE_2PARAM_2 ||
907 port->state == STATE_REAL_TIME) {
908 memcpy(req->buf + req->length, p, sizeof(p));
909 req->length += sizeof(p);
911 if (next_state == STATE_FINISHED) {
912 next_state = STATE_INITIAL;
913 port->data[0] = port->data[1] = 0;
916 if (midi2->info.req_buf_size - req->length <= 4) {
917 queue_request_ep_raw(req);
923 port->state = next_state;
927 /* process all pending MIDI bytes in the internal buffer;
928 * returns true if the request gets empty
929 * returns false if all have been processed
931 static bool process_midi1_pending_buf(struct f_midi2 *midi2,
932 struct usb_request **req_p)
934 unsigned int cable, c;
936 for (cable = 0; cable < midi2->num_midi1_in; cable++) {
937 struct f_midi2_midi1_port *port = &midi2->midi1_port[cable];
941 for (c = 0; c < port->pending; c++) {
942 if (process_midi1_byte(midi2, cable, port->buf[c],
946 memmove(port->buf, port->buf + c,
957 /* fill the MIDI bytes onto the temporary buffer
959 static void fill_midi1_pending_buf(struct f_midi2 *midi2, u8 cable, u8 *buf,
962 struct f_midi2_midi1_port *port = &midi2->midi1_port[cable];
964 if (port->pending + size > sizeof(port->buf))
966 memcpy(port->buf + port->pending, buf, size);
967 port->pending += size;
970 /* try to process data given from the associated UMP stream */
971 static void process_midi1_transmit(struct f_midi2 *midi2)
973 struct f_midi2_usb_ep *usb_ep = &midi2->midi1_ep_in;
974 struct f_midi2_ep *ep = &midi2->midi2_eps[0];
975 struct usb_request *req = NULL;
976 /* 12 is the largest outcome (4 MIDI1 cmds) for a single UMP packet */
977 unsigned char outbuf[12];
978 unsigned char group, cable;
982 if (!usb_ep->usb_ep || !usb_ep->usb_ep->enabled)
987 req = get_empty_request(usb_ep);
992 if (process_midi1_pending_buf(midi2, &req))
995 len = snd_ump_transmit(ep->ump, &ump, 4);
998 if (snd_ump_receive_ump_val(ep->ump, ump) <= 0)
1000 size = snd_ump_convert_from_ump(ep->ump->input_buf, outbuf,
1004 cable = ep->in_group_to_cable[group];
1007 cable--; /* to 0-base */
1008 fill_midi1_pending_buf(midi2, cable, outbuf, size);
1013 queue_request_ep_raw(req);
1015 put_empty_request(req);
1019 /* complete handler for MIDI1 EP-in requests */
1020 static void f_midi2_midi1_ep_in_complete(struct usb_ep *usb_ep,
1021 struct usb_request *req)
1023 struct f_midi2_req_ctx *ctx = req->context;
1024 struct f_midi2 *midi2 = ctx->usb_ep->card;
1025 int status = req->status;
1027 put_empty_request(req);
1030 DBG(midi2, "%s complete error %d: %d/%d\n",
1031 usb_ep->name, status, req->actual, req->length);
1035 process_midi1_transmit(midi2);
1038 /* complete handler for MIDI1 EP-out requests */
1039 static void f_midi2_midi1_ep_out_complete(struct usb_ep *usb_ep,
1040 struct usb_request *req)
1042 struct f_midi2_req_ctx *ctx = req->context;
1043 struct f_midi2 *midi2 = ctx->usb_ep->card;
1044 struct f_midi2_ep *ep;
1045 struct ump_cvt_to_ump *cvt = &midi2->midi1_ump_cvt;
1046 static const u8 midi1_packet_bytes[16] = {
1047 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
1049 unsigned int group, cable, bytes, c, len;
1050 int status = req->status;
1051 const u8 *buf = req->buf;
1054 DBG(midi2, "%s complete error %d: %d/%d\n",
1055 usb_ep->name, status, req->actual, req->length);
1059 len = req->actual >> 2;
1060 for (; len; len--, buf += 4) {
1062 ep = midi2->out_cable_mapping[cable].ep;
1065 group = midi2->out_cable_mapping[cable].group;
1066 bytes = midi1_packet_bytes[*buf & 0x0f];
1067 for (c = 0; c < bytes; c++) {
1068 snd_ump_convert_to_ump(cvt, group, ep->info.protocol,
1070 if (cvt->ump_bytes) {
1071 snd_ump_receive(ep->ump, cvt->ump,
1078 if (midi2->operation_mode != MIDI_OP_MODE_MIDI1)
1081 if (queue_request_ep_raw(req))
1086 put_empty_request(req);
1090 * Common EP handling helpers
1094 static int f_midi2_start_ep(struct f_midi2_usb_ep *usb_ep,
1095 struct usb_function *fn)
1099 if (!usb_ep->usb_ep)
1102 usb_ep_disable(usb_ep->usb_ep);
1103 err = config_ep_by_speed(usb_ep->card->gadget, fn, usb_ep->usb_ep);
1106 return usb_ep_enable(usb_ep->usb_ep);
1109 /* Drop pending requests */
1110 static void f_midi2_drop_reqs(struct f_midi2_usb_ep *usb_ep)
1114 if (!usb_ep->usb_ep || !usb_ep->num_reqs)
1117 for (i = 0; i < usb_ep->num_reqs; i++) {
1118 if (!test_bit(i, &usb_ep->free_reqs) && usb_ep->reqs[i].req) {
1119 usb_ep_dequeue(usb_ep->usb_ep, usb_ep->reqs[i].req);
1120 set_bit(i, &usb_ep->free_reqs);
1125 /* Allocate requests for the given EP */
1126 static int f_midi2_alloc_ep_reqs(struct f_midi2_usb_ep *usb_ep)
1128 struct f_midi2 *midi2 = usb_ep->card;
1131 if (!usb_ep->usb_ep)
1136 for (i = 0; i < midi2->info.num_reqs; i++) {
1137 if (usb_ep->reqs[i].req)
1139 usb_ep->reqs[i].req = alloc_ep_req(usb_ep->usb_ep,
1140 midi2->info.req_buf_size);
1141 if (!usb_ep->reqs[i].req)
1143 usb_ep->reqs[i].req->context = &usb_ep->reqs[i];
1148 /* Free allocated requests */
1149 static void f_midi2_free_ep_reqs(struct f_midi2_usb_ep *usb_ep)
1151 struct f_midi2 *midi2 = usb_ep->card;
1154 for (i = 0; i < midi2->info.num_reqs; i++) {
1155 if (!usb_ep->reqs[i].req)
1157 free_ep_req(usb_ep->usb_ep, usb_ep->reqs[i].req);
1158 usb_ep->reqs[i].req = NULL;
1163 static int f_midi2_init_ep(struct f_midi2 *midi2, struct f_midi2_ep *ep,
1164 struct f_midi2_usb_ep *usb_ep,
1166 void (*complete)(struct usb_ep *usb_ep,
1167 struct usb_request *req))
1171 usb_ep->card = midi2;
1173 usb_ep->usb_ep = usb_ep_autoconfig(midi2->gadget, desc);
1174 if (!usb_ep->usb_ep)
1176 usb_ep->complete = complete;
1178 usb_ep->reqs = kcalloc(midi2->info.num_reqs, sizeof(*usb_ep->reqs),
1182 for (i = 0; i < midi2->info.num_reqs; i++) {
1183 usb_ep->reqs[i].index = i;
1184 usb_ep->reqs[i].usb_ep = usb_ep;
1185 set_bit(i, &usb_ep->free_reqs);
1193 static void f_midi2_free_ep(struct f_midi2_usb_ep *usb_ep)
1195 f_midi2_drop_reqs(usb_ep);
1197 f_midi2_free_ep_reqs(usb_ep);
1199 kfree(usb_ep->reqs);
1200 usb_ep->num_reqs = 0;
1201 usb_ep->free_reqs = 0;
1202 usb_ep->reqs = NULL;
1205 /* Queue requests for EP-out at start */
1206 static void f_midi2_queue_out_reqs(struct f_midi2_usb_ep *usb_ep)
1210 if (!usb_ep->usb_ep)
1213 for (i = 0; i < usb_ep->num_reqs; i++) {
1214 if (!test_bit(i, &usb_ep->free_reqs) || !usb_ep->reqs[i].req)
1216 usb_ep->reqs[i].req->complete = usb_ep->complete;
1217 err = usb_ep_queue(usb_ep->usb_ep, usb_ep->reqs[i].req,
1220 clear_bit(i, &usb_ep->free_reqs);
1225 * Gadget Function callbacks
1228 /* stop both IN and OUT EPs */
1229 static void f_midi2_stop_eps(struct f_midi2_usb_ep *ep_in,
1230 struct f_midi2_usb_ep *ep_out)
1232 f_midi2_drop_reqs(ep_in);
1233 f_midi2_drop_reqs(ep_out);
1234 f_midi2_free_ep_reqs(ep_in);
1235 f_midi2_free_ep_reqs(ep_out);
1238 /* start/queue both IN and OUT EPs */
1239 static int f_midi2_start_eps(struct f_midi2_usb_ep *ep_in,
1240 struct f_midi2_usb_ep *ep_out,
1241 struct usb_function *fn)
1245 err = f_midi2_start_ep(ep_in, fn);
1248 err = f_midi2_start_ep(ep_out, fn);
1252 err = f_midi2_alloc_ep_reqs(ep_in);
1255 err = f_midi2_alloc_ep_reqs(ep_out);
1259 f_midi2_queue_out_reqs(ep_out);
1263 /* gadget function set_alt callback */
1264 static int f_midi2_set_alt(struct usb_function *fn, unsigned int intf,
1267 struct f_midi2 *midi2 = func_to_midi2(fn);
1268 struct f_midi2_ep *ep;
1269 int i, op_mode, err;
1271 if (intf != midi2->midi_if || alt > 1)
1275 op_mode = MIDI_OP_MODE_MIDI1;
1277 op_mode = MIDI_OP_MODE_MIDI2;
1279 op_mode = MIDI_OP_MODE_UNSET;
1281 if (midi2->operation_mode == op_mode)
1284 midi2->operation_mode = op_mode;
1286 if (op_mode != MIDI_OP_MODE_MIDI1)
1287 f_midi2_stop_eps(&midi2->midi1_ep_in, &midi2->midi1_ep_out);
1289 if (op_mode != MIDI_OP_MODE_MIDI2) {
1290 for (i = 0; i < midi2->num_eps; i++) {
1291 ep = &midi2->midi2_eps[i];
1292 f_midi2_stop_eps(&ep->ep_in, &ep->ep_out);
1296 if (op_mode == MIDI_OP_MODE_MIDI1)
1297 return f_midi2_start_eps(&midi2->midi1_ep_in,
1298 &midi2->midi1_ep_out, fn);
1300 if (op_mode == MIDI_OP_MODE_MIDI2) {
1301 for (i = 0; i < midi2->num_eps; i++) {
1302 ep = &midi2->midi2_eps[i];
1304 err = f_midi2_start_eps(&ep->ep_in, &ep->ep_out, fn);
1313 /* gadget function get_alt callback */
1314 static int f_midi2_get_alt(struct usb_function *fn, unsigned int intf)
1316 struct f_midi2 *midi2 = func_to_midi2(fn);
1318 if (intf == midi2->midi_if &&
1319 midi2->operation_mode == MIDI_OP_MODE_MIDI2)
1324 /* convert UMP direction to USB MIDI 2.0 direction */
1325 static unsigned int ump_to_usb_dir(unsigned int ump_dir)
1328 case SNDRV_UMP_DIR_INPUT:
1329 return USB_MS_GR_TRM_BLOCK_TYPE_INPUT_ONLY;
1330 case SNDRV_UMP_DIR_OUTPUT:
1331 return USB_MS_GR_TRM_BLOCK_TYPE_OUTPUT_ONLY;
1333 return USB_MS_GR_TRM_BLOCK_TYPE_BIDIRECTIONAL;
1337 /* assign GTB descriptors (for the given request) */
1338 static void assign_block_descriptors(struct f_midi2 *midi2,
1339 struct usb_request *req,
1342 struct usb_ms20_gr_trm_block_header_descriptor header;
1343 struct usb_ms20_gr_trm_block_descriptor *desc;
1344 struct f_midi2_block_info *b;
1345 struct f_midi2_ep *ep;
1349 len = sizeof(gtb_header_desc) + sizeof(gtb_desc) * midi2->total_blocks;
1350 if (WARN_ON(len > midi2->info.req_buf_size))
1353 header = gtb_header_desc;
1354 header.wTotalLength = cpu_to_le16(len);
1355 if (max_len < len) {
1356 len = min_t(int, len, sizeof(header));
1357 memcpy(req->buf, &header, len);
1359 req->zero = len < max_len;
1363 memcpy(req->buf, &header, sizeof(header));
1364 data = req->buf + sizeof(header);
1365 for (i = 0; i < midi2->num_eps; i++) {
1366 ep = &midi2->midi2_eps[i];
1367 for (blk = 0; blk < ep->num_blks; blk++) {
1368 b = &ep->blks[blk].info;
1369 desc = (struct usb_ms20_gr_trm_block_descriptor *)data;
1372 desc->bGrpTrmBlkID = ep->blks[blk].gtb_id;
1373 desc->bGrpTrmBlkType = ump_to_usb_dir(b->direction);
1374 desc->nGroupTrm = b->first_group;
1375 desc->nNumGroupTrm = b->num_groups;
1376 desc->iBlockItem = ep->blks[blk].string_id;
1378 if (ep->info.protocol & SNDRV_UMP_EP_INFO_PROTO_MIDI2)
1379 desc->bMIDIProtocol = USB_MS_MIDI_PROTO_2_0;
1381 desc->bMIDIProtocol = USB_MS_MIDI_PROTO_1_0_128;
1383 if (b->is_midi1 == 2) {
1384 desc->wMaxInputBandwidth = cpu_to_le16(1);
1385 desc->wMaxOutputBandwidth = cpu_to_le16(1);
1388 data += sizeof(*desc);
1393 req->zero = len < max_len;
1396 /* gadget function setup callback: handle GTB requests */
1397 static int f_midi2_setup(struct usb_function *fn,
1398 const struct usb_ctrlrequest *ctrl)
1400 struct f_midi2 *midi2 = func_to_midi2(fn);
1401 struct usb_composite_dev *cdev = fn->config->cdev;
1402 struct usb_request *req = cdev->req;
1405 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD ||
1406 ctrl->bRequest != USB_REQ_GET_DESCRIPTOR)
1409 value = le16_to_cpu(ctrl->wValue);
1410 length = le16_to_cpu(ctrl->wLength);
1412 if ((value >> 8) != USB_DT_CS_GR_TRM_BLOCK)
1415 /* handle only altset 1 */
1416 if ((value & 0xff) != 1)
1419 assign_block_descriptors(midi2, req, length);
1420 return usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1423 /* gadget function disable callback */
1424 static void f_midi2_disable(struct usb_function *fn)
1426 struct f_midi2 *midi2 = func_to_midi2(fn);
1428 midi2->operation_mode = MIDI_OP_MODE_UNSET;
1432 * ALSA UMP ops: most of them are NOPs, only trigger for write is needed
1434 static int f_midi2_ump_open(struct snd_ump_endpoint *ump, int dir)
1439 static void f_midi2_ump_close(struct snd_ump_endpoint *ump, int dir)
1443 static void f_midi2_ump_trigger(struct snd_ump_endpoint *ump, int dir, int up)
1445 struct f_midi2_ep *ep = ump->private_data;
1446 struct f_midi2 *midi2 = ep->card;
1448 if (up && dir == SNDRV_RAWMIDI_STREAM_OUTPUT) {
1449 switch (midi2->operation_mode) {
1450 case MIDI_OP_MODE_MIDI1:
1451 process_midi1_transmit(midi2);
1453 case MIDI_OP_MODE_MIDI2:
1454 process_ump_transmit(ep);
1460 static void f_midi2_ump_drain(struct snd_ump_endpoint *ump, int dir)
1464 static const struct snd_ump_ops f_midi2_ump_ops = {
1465 .open = f_midi2_ump_open,
1466 .close = f_midi2_ump_close,
1467 .trigger = f_midi2_ump_trigger,
1468 .drain = f_midi2_ump_drain,
1472 * "Operation Mode" control element
1474 static int f_midi2_operation_mode_info(struct snd_kcontrol *kcontrol,
1475 struct snd_ctl_elem_info *uinfo)
1477 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1479 uinfo->value.integer.min = MIDI_OP_MODE_UNSET;
1480 uinfo->value.integer.max = MIDI_OP_MODE_MIDI2;
1484 static int f_midi2_operation_mode_get(struct snd_kcontrol *kcontrol,
1485 struct snd_ctl_elem_value *ucontrol)
1487 struct f_midi2 *midi2 = snd_kcontrol_chip(kcontrol);
1489 ucontrol->value.integer.value[0] = midi2->operation_mode;
1493 static const struct snd_kcontrol_new operation_mode_ctl = {
1494 .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
1495 .name = "Operation Mode",
1496 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1497 .info = f_midi2_operation_mode_info,
1498 .get = f_midi2_operation_mode_get,
1502 * ALSA UMP instance creation / deletion
1504 static void f_midi2_free_card(struct f_midi2 *midi2)
1507 snd_card_free_when_closed(midi2->card);
1512 /* use a reverse direction for the gadget host */
1513 static int reverse_dir(int dir)
1515 if (!dir || dir == SNDRV_UMP_DIR_BIDIRECTION)
1517 return (dir == SNDRV_UMP_DIR_OUTPUT) ?
1518 SNDRV_UMP_DIR_INPUT : SNDRV_UMP_DIR_OUTPUT;
1521 static int f_midi2_create_card(struct f_midi2 *midi2)
1523 struct snd_card *card;
1524 struct snd_ump_endpoint *ump;
1525 struct f_midi2_ep *ep;
1526 int i, id, blk, err;
1529 err = snd_card_new(&midi2->gadget->dev, -1, NULL, THIS_MODULE, 0,
1535 strcpy(card->driver, "f_midi2");
1536 strcpy(card->shortname, "MIDI 2.0 Gadget");
1537 strcpy(card->longname, "MIDI 2.0 Gadget");
1540 for (i = 0; i < midi2->num_eps; i++) {
1541 ep = &midi2->midi2_eps[i];
1542 err = snd_ump_endpoint_new(card, "MIDI 2.0 Gadget", id,
1549 ump->no_process_stream = true;
1550 ump->private_data = ep;
1551 ump->ops = &f_midi2_ump_ops;
1552 if (midi2->info.static_block)
1553 ump->info.flags |= SNDRV_UMP_EP_INFO_STATIC_BLOCKS;
1554 ump->info.protocol_caps = (ep->info.protocol_caps & 3) << 8;
1555 ump->info.protocol = (ep->info.protocol & 3) << 8;
1556 ump->info.version = 0x0101;
1557 ump->info.family_id = ep->info.family;
1558 ump->info.model_id = ep->info.model;
1559 ump->info.manufacturer_id = ep->info.manufacturer & 0xffffff;
1560 sw = cpu_to_be32(ep->info.sw_revision);
1561 memcpy(ump->info.sw_revision, &sw, 4);
1563 strscpy(ump->info.name, ump_ep_name(ep),
1564 sizeof(ump->info.name));
1565 strscpy(ump->info.product_id, ump_product_id(ep),
1566 sizeof(ump->info.product_id));
1567 strscpy(ump->core.name, ump->info.name, sizeof(ump->core.name));
1569 for (blk = 0; blk < ep->num_blks; blk++) {
1570 const struct f_midi2_block_info *b = &ep->blks[blk].info;
1571 struct snd_ump_block *fb;
1573 err = snd_ump_block_new(ump, blk,
1574 reverse_dir(b->direction),
1575 b->first_group, b->num_groups,
1579 fb = ep->blks[blk].fb;
1580 fb->info.active = !!b->active;
1581 fb->info.midi_ci_version = b->midi_ci_version;
1582 fb->info.ui_hint = reverse_dir(b->ui_hint);
1583 fb->info.sysex8_streams = b->sysex8_streams;
1584 fb->info.flags |= b->is_midi1;
1585 strscpy(fb->info.name, ump_fb_name(b),
1586 sizeof(fb->info.name));
1590 for (i = 0; i < midi2->num_eps; i++) {
1591 err = snd_ump_attach_legacy_rawmidi(midi2->midi2_eps[i].ump,
1598 err = snd_ctl_add(card, snd_ctl_new1(&operation_mode_ctl, midi2));
1602 err = snd_card_register(card);
1609 f_midi2_free_card(midi2);
1614 * Creation of USB descriptors
1616 struct f_midi2_usb_config {
1617 struct usb_descriptor_header **list;
1621 /* MIDI 1.0 jacks */
1622 unsigned char jack_in, jack_out, jack_id;
1623 struct usb_midi_in_jack_descriptor jack_ins[MAX_CABLES];
1624 struct usb_midi_out_jack_descriptor_1 jack_outs[MAX_CABLES];
1627 static int append_config(struct f_midi2_usb_config *config, void *d)
1632 if (config->size + 2 >= config->alloc) {
1633 size = config->size + 16;
1634 buf = krealloc(config->list, size * sizeof(void *), GFP_KERNEL);
1638 config->alloc = size;
1641 config->list[config->size] = d;
1643 config->list[config->size] = NULL;
1647 static int append_configs(struct f_midi2_usb_config *config, void **d)
1652 err = append_config(config, *d);
1659 static int append_midi1_in_jack(struct f_midi2 *midi2,
1660 struct f_midi2_usb_config *config,
1661 struct midi1_cable_mapping *map,
1664 struct usb_midi_in_jack_descriptor *jack =
1665 &config->jack_ins[config->jack_in++];
1666 int id = ++config->jack_id;
1669 jack->bLength = 0x06;
1670 jack->bDescriptorType = USB_DT_CS_INTERFACE;
1671 jack->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
1672 jack->bJackType = type;
1674 /* use the corresponding block name as jack name */
1676 jack->iJack = map->ep->blks[map->block].string_id;
1678 err = append_config(config, jack);
1684 static int append_midi1_out_jack(struct f_midi2 *midi2,
1685 struct f_midi2_usb_config *config,
1686 struct midi1_cable_mapping *map,
1687 unsigned int type, unsigned int source)
1689 struct usb_midi_out_jack_descriptor_1 *jack =
1690 &config->jack_outs[config->jack_out++];
1691 int id = ++config->jack_id;
1694 jack->bLength = 0x09;
1695 jack->bDescriptorType = USB_DT_CS_INTERFACE;
1696 jack->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
1697 jack->bJackType = type;
1699 jack->bNrInputPins = 1;
1700 jack->pins[0].baSourceID = source;
1701 jack->pins[0].baSourcePin = 0x01;
1702 /* use the corresponding block name as jack name */
1704 jack->iJack = map->ep->blks[map->block].string_id;
1706 err = append_config(config, jack);
1712 static int f_midi2_create_usb_configs(struct f_midi2 *midi2,
1713 struct f_midi2_usb_config *config,
1716 void **midi1_in_eps, **midi1_out_eps;
1722 case USB_SPEED_HIGH:
1723 midi2_midi1_ep_out_desc.wMaxPacketSize = cpu_to_le16(512);
1724 midi2_midi1_ep_in_desc.wMaxPacketSize = cpu_to_le16(512);
1725 for (i = 0; i < midi2->num_eps; i++)
1726 midi2_midi2_ep_out_desc[i].wMaxPacketSize =
1729 case USB_SPEED_FULL:
1730 midi1_in_eps = midi2_midi1_ep_in_descs;
1731 midi1_out_eps = midi2_midi1_ep_out_descs;
1733 case USB_SPEED_SUPER:
1734 midi2_midi1_ep_out_desc.wMaxPacketSize = cpu_to_le16(1024);
1735 midi2_midi1_ep_in_desc.wMaxPacketSize = cpu_to_le16(1024);
1736 for (i = 0; i < midi2->num_eps; i++)
1737 midi2_midi2_ep_out_desc[i].wMaxPacketSize =
1739 midi1_in_eps = midi2_midi1_ep_in_ss_descs;
1740 midi1_out_eps = midi2_midi1_ep_out_ss_descs;
1744 err = append_configs(config, midi2_audio_descs);
1748 if (midi2->num_midi1_in && midi2->num_midi1_out)
1749 midi2_midi1_if_desc.bNumEndpoints = 2;
1751 midi2_midi1_if_desc.bNumEndpoints = 1;
1753 err = append_configs(config, midi2_midi1_descs);
1757 total = USB_DT_MS_HEADER_SIZE;
1758 if (midi2->num_midi1_out) {
1759 midi2_midi1_ep_out_class_desc.bLength =
1760 USB_DT_MS_ENDPOINT_SIZE(midi2->num_midi1_out);
1761 total += midi2_midi1_ep_out_class_desc.bLength;
1762 midi2_midi1_ep_out_class_desc.bNumEmbMIDIJack =
1763 midi2->num_midi1_out;
1764 total += midi2->num_midi1_out *
1765 (USB_DT_MIDI_IN_SIZE + USB_DT_MIDI_OUT_SIZE(1));
1766 for (i = 0; i < midi2->num_midi1_out; i++) {
1767 jack = append_midi1_in_jack(midi2, config,
1768 &midi2->in_cable_mapping[i],
1772 midi2_midi1_ep_out_class_desc.baAssocJackID[i] = jack;
1773 jack = append_midi1_out_jack(midi2, config,
1774 &midi2->in_cable_mapping[i],
1775 USB_MS_EXTERNAL, jack);
1781 if (midi2->num_midi1_in) {
1782 midi2_midi1_ep_in_class_desc.bLength =
1783 USB_DT_MS_ENDPOINT_SIZE(midi2->num_midi1_in);
1784 total += midi2_midi1_ep_in_class_desc.bLength;
1785 midi2_midi1_ep_in_class_desc.bNumEmbMIDIJack =
1786 midi2->num_midi1_in;
1787 total += midi2->num_midi1_in *
1788 (USB_DT_MIDI_IN_SIZE + USB_DT_MIDI_OUT_SIZE(1));
1789 for (i = 0; i < midi2->num_midi1_in; i++) {
1790 jack = append_midi1_in_jack(midi2, config,
1791 &midi2->out_cable_mapping[i],
1795 jack = append_midi1_out_jack(midi2, config,
1796 &midi2->out_cable_mapping[i],
1797 USB_MS_EMBEDDED, jack);
1800 midi2_midi1_ep_in_class_desc.baAssocJackID[i] = jack;
1804 midi2_midi1_class_desc.wTotalLength = cpu_to_le16(total);
1806 if (midi2->num_midi1_out) {
1807 err = append_configs(config, midi1_out_eps);
1811 if (midi2->num_midi1_in) {
1812 err = append_configs(config, midi1_in_eps);
1817 err = append_configs(config, midi2_midi2_descs);
1821 for (i = 0; i < midi2->num_eps; i++) {
1822 err = append_config(config, &midi2_midi2_ep_out_desc[i]);
1825 if (speed == USB_SPEED_SUPER || speed == USB_SPEED_SUPER_PLUS) {
1826 err = append_config(config, &midi2_midi2_ep_out_ss_comp_desc);
1830 err = append_config(config, &midi2_midi2_ep_out_class_desc[i]);
1833 err = append_config(config, &midi2_midi2_ep_in_desc[i]);
1836 if (speed == USB_SPEED_SUPER || speed == USB_SPEED_SUPER_PLUS) {
1837 err = append_config(config, &midi2_midi2_ep_in_ss_comp_desc);
1841 err = append_config(config, &midi2_midi2_ep_in_class_desc[i]);
1849 static void f_midi2_free_usb_configs(struct f_midi2_usb_config *config)
1851 kfree(config->list);
1852 memset(config, 0, sizeof(*config));
1855 /* as we use the static descriptors for simplicity, serialize bind call */
1856 static DEFINE_MUTEX(f_midi2_desc_mutex);
1858 /* fill MIDI2 EP class-specific descriptor */
1859 static void fill_midi2_class_desc(struct f_midi2_ep *ep,
1860 struct usb_ms20_endpoint_descriptor_32 *cdesc)
1864 cdesc->bLength = USB_DT_MS20_ENDPOINT_SIZE(ep->num_blks);
1865 cdesc->bDescriptorType = USB_DT_CS_ENDPOINT;
1866 cdesc->bDescriptorSubtype = USB_MS_GENERAL_2_0;
1867 cdesc->bNumGrpTrmBlock = ep->num_blks;
1868 for (blk = 0; blk < ep->num_blks; blk++)
1869 cdesc->baAssoGrpTrmBlkID[blk] = ep->blks[blk].gtb_id;
1872 /* initialize MIDI2 EP-in */
1873 static int f_midi2_init_midi2_ep_in(struct f_midi2 *midi2, int index)
1875 struct f_midi2_ep *ep = &midi2->midi2_eps[index];
1876 struct usb_endpoint_descriptor *desc = &midi2_midi2_ep_in_desc[index];
1878 desc->bLength = USB_DT_ENDPOINT_SIZE;
1879 desc->bDescriptorType = USB_DT_ENDPOINT;
1880 desc->bEndpointAddress = USB_DIR_IN;
1881 desc->bmAttributes = USB_ENDPOINT_XFER_INT;
1882 desc->wMaxPacketSize = cpu_to_le16(EP_MAX_PACKET_INT);
1883 desc->bInterval = 1;
1885 fill_midi2_class_desc(ep, &midi2_midi2_ep_in_class_desc[index]);
1887 return f_midi2_init_ep(midi2, ep, &ep->ep_in, desc,
1888 f_midi2_ep_in_complete);
1891 /* initialize MIDI2 EP-out */
1892 static int f_midi2_init_midi2_ep_out(struct f_midi2 *midi2, int index)
1894 struct f_midi2_ep *ep = &midi2->midi2_eps[index];
1895 struct usb_endpoint_descriptor *desc = &midi2_midi2_ep_out_desc[index];
1897 desc->bLength = USB_DT_ENDPOINT_SIZE;
1898 desc->bDescriptorType = USB_DT_ENDPOINT;
1899 desc->bEndpointAddress = USB_DIR_OUT;
1900 desc->bmAttributes = USB_ENDPOINT_XFER_BULK;
1902 fill_midi2_class_desc(ep, &midi2_midi2_ep_out_class_desc[index]);
1904 return f_midi2_init_ep(midi2, ep, &ep->ep_out, desc,
1905 f_midi2_ep_out_complete);
1908 /* gadget function bind callback */
1909 static int f_midi2_bind(struct usb_configuration *c, struct usb_function *f)
1911 struct usb_composite_dev *cdev = c->cdev;
1912 struct f_midi2 *midi2 = func_to_midi2(f);
1913 struct f_midi2_ep *ep;
1914 struct f_midi2_usb_config config = {};
1915 struct usb_gadget_strings string_fn = {
1916 .language = 0x0409, /* en-us */
1917 .strings = midi2->string_defs,
1919 struct usb_gadget_strings *strings[] = {
1925 midi2->gadget = cdev->gadget;
1926 midi2->operation_mode = MIDI_OP_MODE_UNSET;
1928 status = f_midi2_create_card(midi2);
1932 /* maybe allocate device-global string ID */
1933 midi2->strings = usb_gstrings_attach(c->cdev, strings,
1934 midi2->total_blocks + 1);
1935 if (IS_ERR(midi2->strings)) {
1936 status = PTR_ERR(midi2->strings);
1940 mutex_lock(&f_midi2_desc_mutex);
1941 midi2_midi1_if_desc.iInterface = midi2->strings[STR_IFACE].id;
1942 midi2_midi2_if_desc.iInterface = midi2->strings[STR_IFACE].id;
1943 for (i = 0; i < midi2->num_eps; i++) {
1944 ep = &midi2->midi2_eps[i];
1945 for (blk = 0; blk < ep->num_blks; blk++)
1946 ep->blks[blk].string_id =
1947 midi2->strings[gtb_to_str_id(ep->blks[blk].gtb_id)].id;
1950 midi2_midi2_if_desc.bNumEndpoints = midi2->num_eps * 2;
1952 /* audio interface */
1953 status = usb_interface_id(c, f);
1956 midi2_audio_if_desc.bInterfaceNumber = status;
1958 /* MIDI streaming */
1959 status = usb_interface_id(c, f);
1962 midi2->midi_if = status;
1963 midi2_midi1_if_desc.bInterfaceNumber = status;
1964 midi2_midi2_if_desc.bInterfaceNumber = status;
1965 midi2_audio_class_desc.baInterfaceNr[0] = status;
1967 /* allocate instance-specific endpoints */
1968 if (midi2->midi2_eps[0].blks[0].info.direction != SNDRV_UMP_DIR_OUTPUT) {
1969 status = f_midi2_init_ep(midi2, NULL, &midi2->midi1_ep_in,
1970 &midi2_midi1_ep_in_desc,
1971 f_midi2_midi1_ep_in_complete);
1976 if (midi2->midi2_eps[0].blks[0].info.direction != SNDRV_UMP_DIR_INPUT) {
1977 status = f_midi2_init_ep(midi2, NULL, &midi2->midi1_ep_out,
1978 &midi2_midi1_ep_out_desc,
1979 f_midi2_midi1_ep_out_complete);
1984 for (i = 0; i < midi2->num_eps; i++) {
1985 status = f_midi2_init_midi2_ep_in(midi2, i);
1988 status = f_midi2_init_midi2_ep_out(midi2, i);
1993 status = f_midi2_create_usb_configs(midi2, &config, USB_SPEED_FULL);
1996 f->fs_descriptors = usb_copy_descriptors(config.list);
1997 if (!f->fs_descriptors) {
2001 f_midi2_free_usb_configs(&config);
2003 status = f_midi2_create_usb_configs(midi2, &config, USB_SPEED_HIGH);
2006 f->hs_descriptors = usb_copy_descriptors(config.list);
2007 if (!f->hs_descriptors) {
2011 f_midi2_free_usb_configs(&config);
2013 status = f_midi2_create_usb_configs(midi2, &config, USB_SPEED_SUPER);
2016 f->ss_descriptors = usb_copy_descriptors(config.list);
2017 if (!f->ss_descriptors) {
2021 f_midi2_free_usb_configs(&config);
2023 mutex_unlock(&f_midi2_desc_mutex);
2027 f_midi2_free_usb_configs(&config);
2028 mutex_unlock(&f_midi2_desc_mutex);
2029 usb_free_all_descriptors(f);
2031 f_midi2_free_card(midi2);
2033 ERROR(midi2, "%s: can't bind, err %d\n", f->name, status);
2037 /* gadget function unbind callback */
2038 static void f_midi2_unbind(struct usb_configuration *c, struct usb_function *f)
2040 struct f_midi2 *midi2 = func_to_midi2(f);
2043 f_midi2_free_card(midi2);
2045 f_midi2_free_ep(&midi2->midi1_ep_in);
2046 f_midi2_free_ep(&midi2->midi1_ep_out);
2047 for (i = 0; i < midi2->num_eps; i++) {
2048 f_midi2_free_ep(&midi2->midi2_eps[i].ep_in);
2049 f_midi2_free_ep(&midi2->midi2_eps[i].ep_out);
2052 usb_free_all_descriptors(f);
2056 * ConfigFS interface
2059 /* type conversion helpers */
2060 static inline struct f_midi2_opts *to_f_midi2_opts(struct config_item *item)
2062 return container_of(to_config_group(item), struct f_midi2_opts,
2066 static inline struct f_midi2_ep_opts *
2067 to_f_midi2_ep_opts(struct config_item *item)
2069 return container_of(to_config_group(item), struct f_midi2_ep_opts,
2073 static inline struct f_midi2_block_opts *
2074 to_f_midi2_block_opts(struct config_item *item)
2076 return container_of(to_config_group(item), struct f_midi2_block_opts,
2080 /* trim the string to be usable for EP and FB name strings */
2081 static void make_name_string(char *s)
2085 p = strchr(s, '\n');
2090 for (; p > s && isspace(*p); p--)
2094 /* configfs helpers: generic show/store for unisnged int */
2095 static ssize_t f_midi2_opts_uint_show(struct f_midi2_opts *opts,
2096 u32 val, const char *format, char *page)
2100 mutex_lock(&opts->lock);
2101 result = sprintf(page, format, val);
2102 mutex_unlock(&opts->lock);
2106 static ssize_t f_midi2_opts_uint_store(struct f_midi2_opts *opts,
2107 u32 *valp, u32 minval, u32 maxval,
2108 const char *page, size_t len)
2113 mutex_lock(&opts->lock);
2119 ret = kstrtou32(page, 0, &val);
2122 if (val < minval || val > maxval) {
2131 mutex_unlock(&opts->lock);
2135 /* generic store for bool */
2136 static ssize_t f_midi2_opts_bool_store(struct f_midi2_opts *opts,
2137 bool *valp, const char *page, size_t len)
2142 mutex_lock(&opts->lock);
2148 ret = kstrtobool(page, &val);
2155 mutex_unlock(&opts->lock);
2159 /* generic show/store for string */
2160 static ssize_t f_midi2_opts_str_show(struct f_midi2_opts *opts,
2161 const char *str, char *page)
2165 mutex_lock(&opts->lock);
2167 result = scnprintf(page, PAGE_SIZE, "%s\n", str);
2168 mutex_unlock(&opts->lock);
2172 static ssize_t f_midi2_opts_str_store(struct f_midi2_opts *opts,
2173 const char **strp, size_t maxlen,
2174 const char *page, size_t len)
2179 mutex_lock(&opts->lock);
2185 c = kstrndup(page, min(len, maxlen), GFP_KERNEL);
2192 make_name_string(c);
2197 mutex_unlock(&opts->lock);
2202 * Definitions for UMP Block config
2205 /* define an uint option for block */
2206 #define F_MIDI2_BLOCK_OPT(name, format, minval, maxval) \
2207 static ssize_t f_midi2_block_opts_##name##_show(struct config_item *item,\
2210 struct f_midi2_block_opts *opts = to_f_midi2_block_opts(item); \
2211 return f_midi2_opts_uint_show(opts->ep->opts, opts->info.name, \
2212 format "\n", page); \
2215 static ssize_t f_midi2_block_opts_##name##_store(struct config_item *item,\
2216 const char *page, size_t len) \
2218 struct f_midi2_block_opts *opts = to_f_midi2_block_opts(item); \
2219 return f_midi2_opts_uint_store(opts->ep->opts, &opts->info.name,\
2220 minval, maxval, page, len); \
2223 CONFIGFS_ATTR(f_midi2_block_opts_, name)
2225 /* define a boolean option for block */
2226 #define F_MIDI2_BLOCK_BOOL_OPT(name) \
2227 static ssize_t f_midi2_block_opts_##name##_show(struct config_item *item,\
2230 struct f_midi2_block_opts *opts = to_f_midi2_block_opts(item); \
2231 return f_midi2_opts_uint_show(opts->ep->opts, opts->info.name, \
2235 static ssize_t f_midi2_block_opts_##name##_store(struct config_item *item,\
2236 const char *page, size_t len) \
2238 struct f_midi2_block_opts *opts = to_f_midi2_block_opts(item); \
2239 return f_midi2_opts_bool_store(opts->ep->opts, &opts->info.name,\
2243 CONFIGFS_ATTR(f_midi2_block_opts_, name)
2245 F_MIDI2_BLOCK_OPT(direction, "0x%x", 1, 3);
2246 F_MIDI2_BLOCK_OPT(first_group, "0x%x", 0, 15);
2247 F_MIDI2_BLOCK_OPT(num_groups, "0x%x", 1, 16);
2248 F_MIDI2_BLOCK_OPT(midi1_first_group, "0x%x", 0, 15);
2249 F_MIDI2_BLOCK_OPT(midi1_num_groups, "0x%x", 0, 16);
2250 F_MIDI2_BLOCK_OPT(ui_hint, "0x%x", 0, 3);
2251 F_MIDI2_BLOCK_OPT(midi_ci_version, "%u", 0, 1);
2252 F_MIDI2_BLOCK_OPT(sysex8_streams, "%u", 0, 255);
2253 F_MIDI2_BLOCK_OPT(is_midi1, "%u", 0, 2);
2254 F_MIDI2_BLOCK_BOOL_OPT(active);
2256 static ssize_t f_midi2_block_opts_name_show(struct config_item *item,
2259 struct f_midi2_block_opts *opts = to_f_midi2_block_opts(item);
2261 return f_midi2_opts_str_show(opts->ep->opts, opts->info.name, page);
2264 static ssize_t f_midi2_block_opts_name_store(struct config_item *item,
2265 const char *page, size_t len)
2267 struct f_midi2_block_opts *opts = to_f_midi2_block_opts(item);
2269 return f_midi2_opts_str_store(opts->ep->opts, &opts->info.name, 128,
2273 CONFIGFS_ATTR(f_midi2_block_opts_, name);
2275 static struct configfs_attribute *f_midi2_block_attrs[] = {
2276 &f_midi2_block_opts_attr_direction,
2277 &f_midi2_block_opts_attr_first_group,
2278 &f_midi2_block_opts_attr_num_groups,
2279 &f_midi2_block_opts_attr_midi1_first_group,
2280 &f_midi2_block_opts_attr_midi1_num_groups,
2281 &f_midi2_block_opts_attr_ui_hint,
2282 &f_midi2_block_opts_attr_midi_ci_version,
2283 &f_midi2_block_opts_attr_sysex8_streams,
2284 &f_midi2_block_opts_attr_is_midi1,
2285 &f_midi2_block_opts_attr_active,
2286 &f_midi2_block_opts_attr_name,
2290 static void f_midi2_block_opts_release(struct config_item *item)
2292 struct f_midi2_block_opts *opts = to_f_midi2_block_opts(item);
2294 kfree(opts->info.name);
2298 static struct configfs_item_operations f_midi2_block_item_ops = {
2299 .release = f_midi2_block_opts_release,
2302 static const struct config_item_type f_midi2_block_type = {
2303 .ct_item_ops = &f_midi2_block_item_ops,
2304 .ct_attrs = f_midi2_block_attrs,
2305 .ct_owner = THIS_MODULE,
2308 /* create a f_midi2_block_opts instance for the given block number */
2309 static int f_midi2_block_opts_create(struct f_midi2_ep_opts *ep_opts,
2311 struct f_midi2_block_opts **block_p)
2313 struct f_midi2_block_opts *block_opts;
2316 mutex_lock(&ep_opts->opts->lock);
2317 if (ep_opts->opts->refcnt || ep_opts->blks[blk]) {
2322 block_opts = kzalloc(sizeof(*block_opts), GFP_KERNEL);
2328 block_opts->ep = ep_opts;
2329 block_opts->id = blk;
2331 /* set up the default values */
2332 block_opts->info.direction = SNDRV_UMP_DIR_BIDIRECTION;
2333 block_opts->info.first_group = 0;
2334 block_opts->info.num_groups = 1;
2335 block_opts->info.ui_hint = SNDRV_UMP_BLOCK_UI_HINT_BOTH;
2336 block_opts->info.active = 1;
2338 ep_opts->blks[blk] = block_opts;
2339 *block_p = block_opts;
2342 mutex_unlock(&ep_opts->opts->lock);
2346 /* make_group callback for a block */
2347 static struct config_group *
2348 f_midi2_opts_block_make(struct config_group *group, const char *name)
2350 struct f_midi2_ep_opts *ep_opts;
2351 struct f_midi2_block_opts *block_opts;
2355 if (strncmp(name, "block.", 6))
2356 return ERR_PTR(-EINVAL);
2357 ret = kstrtouint(name + 6, 10, &blk);
2359 return ERR_PTR(ret);
2361 ep_opts = to_f_midi2_ep_opts(&group->cg_item);
2363 if (blk >= SNDRV_UMP_MAX_BLOCKS)
2364 return ERR_PTR(-EINVAL);
2365 if (ep_opts->blks[blk])
2366 return ERR_PTR(-EBUSY);
2367 ret = f_midi2_block_opts_create(ep_opts, blk, &block_opts);
2369 return ERR_PTR(ret);
2371 config_group_init_type_name(&block_opts->group, name,
2372 &f_midi2_block_type);
2373 return &block_opts->group;
2376 /* drop_item callback for a block */
2378 f_midi2_opts_block_drop(struct config_group *group, struct config_item *item)
2380 struct f_midi2_block_opts *block_opts = to_f_midi2_block_opts(item);
2382 mutex_lock(&block_opts->ep->opts->lock);
2383 block_opts->ep->blks[block_opts->id] = NULL;
2384 mutex_unlock(&block_opts->ep->opts->lock);
2385 config_item_put(item);
2389 * Definitions for UMP Endpoint config
2392 /* define an uint option for EP */
2393 #define F_MIDI2_EP_OPT(name, format, minval, maxval) \
2394 static ssize_t f_midi2_ep_opts_##name##_show(struct config_item *item, \
2397 struct f_midi2_ep_opts *opts = to_f_midi2_ep_opts(item); \
2398 return f_midi2_opts_uint_show(opts->opts, opts->info.name, \
2399 format "\n", page); \
2402 static ssize_t f_midi2_ep_opts_##name##_store(struct config_item *item, \
2403 const char *page, size_t len)\
2405 struct f_midi2_ep_opts *opts = to_f_midi2_ep_opts(item); \
2406 return f_midi2_opts_uint_store(opts->opts, &opts->info.name, \
2407 minval, maxval, page, len); \
2410 CONFIGFS_ATTR(f_midi2_ep_opts_, name)
2412 /* define a string option for EP */
2413 #define F_MIDI2_EP_STR_OPT(name, maxlen) \
2414 static ssize_t f_midi2_ep_opts_##name##_show(struct config_item *item, \
2417 struct f_midi2_ep_opts *opts = to_f_midi2_ep_opts(item); \
2418 return f_midi2_opts_str_show(opts->opts, opts->info.name, page);\
2421 static ssize_t f_midi2_ep_opts_##name##_store(struct config_item *item, \
2422 const char *page, size_t len) \
2424 struct f_midi2_ep_opts *opts = to_f_midi2_ep_opts(item); \
2425 return f_midi2_opts_str_store(opts->opts, &opts->info.name, maxlen,\
2429 CONFIGFS_ATTR(f_midi2_ep_opts_, name)
2431 F_MIDI2_EP_OPT(protocol, "0x%x", 1, 2);
2432 F_MIDI2_EP_OPT(protocol_caps, "0x%x", 1, 3);
2433 F_MIDI2_EP_OPT(manufacturer, "0x%x", 0, 0xffffff);
2434 F_MIDI2_EP_OPT(family, "0x%x", 0, 0xffff);
2435 F_MIDI2_EP_OPT(model, "0x%x", 0, 0xffff);
2436 F_MIDI2_EP_OPT(sw_revision, "0x%x", 0, 0xffffffff);
2437 F_MIDI2_EP_STR_OPT(ep_name, 128);
2438 F_MIDI2_EP_STR_OPT(product_id, 128);
2440 static struct configfs_attribute *f_midi2_ep_attrs[] = {
2441 &f_midi2_ep_opts_attr_protocol,
2442 &f_midi2_ep_opts_attr_protocol_caps,
2443 &f_midi2_ep_opts_attr_ep_name,
2444 &f_midi2_ep_opts_attr_product_id,
2445 &f_midi2_ep_opts_attr_manufacturer,
2446 &f_midi2_ep_opts_attr_family,
2447 &f_midi2_ep_opts_attr_model,
2448 &f_midi2_ep_opts_attr_sw_revision,
2452 static void f_midi2_ep_opts_release(struct config_item *item)
2454 struct f_midi2_ep_opts *opts = to_f_midi2_ep_opts(item);
2456 kfree(opts->info.ep_name);
2457 kfree(opts->info.product_id);
2461 static struct configfs_item_operations f_midi2_ep_item_ops = {
2462 .release = f_midi2_ep_opts_release,
2465 static struct configfs_group_operations f_midi2_ep_group_ops = {
2466 .make_group = f_midi2_opts_block_make,
2467 .drop_item = f_midi2_opts_block_drop,
2470 static const struct config_item_type f_midi2_ep_type = {
2471 .ct_item_ops = &f_midi2_ep_item_ops,
2472 .ct_group_ops = &f_midi2_ep_group_ops,
2473 .ct_attrs = f_midi2_ep_attrs,
2474 .ct_owner = THIS_MODULE,
2477 /* create a f_midi2_ep_opts instance */
2478 static int f_midi2_ep_opts_create(struct f_midi2_opts *opts,
2480 struct f_midi2_ep_opts **ep_p)
2482 struct f_midi2_ep_opts *ep_opts;
2484 ep_opts = kzalloc(sizeof(*ep_opts), GFP_KERNEL);
2488 ep_opts->opts = opts;
2489 ep_opts->index = index;
2491 /* set up the default values */
2492 ep_opts->info.protocol = 2;
2493 ep_opts->info.protocol_caps = 3;
2495 opts->eps[index] = ep_opts;
2500 /* make_group callback for an EP */
2501 static struct config_group *
2502 f_midi2_opts_ep_make(struct config_group *group, const char *name)
2504 struct f_midi2_opts *opts;
2505 struct f_midi2_ep_opts *ep_opts;
2509 if (strncmp(name, "ep.", 3))
2510 return ERR_PTR(-EINVAL);
2511 ret = kstrtouint(name + 3, 10, &index);
2513 return ERR_PTR(ret);
2515 opts = to_f_midi2_opts(&group->cg_item);
2516 if (index >= MAX_UMP_EPS)
2517 return ERR_PTR(-EINVAL);
2518 if (opts->eps[index])
2519 return ERR_PTR(-EBUSY);
2520 ret = f_midi2_ep_opts_create(opts, index, &ep_opts);
2522 return ERR_PTR(ret);
2524 config_group_init_type_name(&ep_opts->group, name, &f_midi2_ep_type);
2525 return &ep_opts->group;
2528 /* drop_item callback for an EP */
2530 f_midi2_opts_ep_drop(struct config_group *group, struct config_item *item)
2532 struct f_midi2_ep_opts *ep_opts = to_f_midi2_ep_opts(item);
2534 mutex_lock(&ep_opts->opts->lock);
2535 ep_opts->opts->eps[ep_opts->index] = NULL;
2536 mutex_unlock(&ep_opts->opts->lock);
2537 config_item_put(item);
2541 * Definitions for card config
2544 /* define a bool option for card */
2545 #define F_MIDI2_BOOL_OPT(name) \
2546 static ssize_t f_midi2_opts_##name##_show(struct config_item *item, \
2549 struct f_midi2_opts *opts = to_f_midi2_opts(item); \
2550 return f_midi2_opts_uint_show(opts, opts->info.name, \
2554 static ssize_t f_midi2_opts_##name##_store(struct config_item *item, \
2555 const char *page, size_t len) \
2557 struct f_midi2_opts *opts = to_f_midi2_opts(item); \
2558 return f_midi2_opts_bool_store(opts, &opts->info.name, \
2562 CONFIGFS_ATTR(f_midi2_opts_, name)
2564 F_MIDI2_BOOL_OPT(process_ump);
2565 F_MIDI2_BOOL_OPT(static_block);
2567 static ssize_t f_midi2_opts_iface_name_show(struct config_item *item,
2570 struct f_midi2_opts *opts = to_f_midi2_opts(item);
2572 return f_midi2_opts_str_show(opts, opts->info.iface_name, page);
2575 static ssize_t f_midi2_opts_iface_name_store(struct config_item *item,
2576 const char *page, size_t len)
2578 struct f_midi2_opts *opts = to_f_midi2_opts(item);
2580 return f_midi2_opts_str_store(opts, &opts->info.iface_name, 128,
2584 CONFIGFS_ATTR(f_midi2_opts_, iface_name);
2586 static struct configfs_attribute *f_midi2_attrs[] = {
2587 &f_midi2_opts_attr_process_ump,
2588 &f_midi2_opts_attr_static_block,
2589 &f_midi2_opts_attr_iface_name,
2593 static void f_midi2_opts_release(struct config_item *item)
2595 struct f_midi2_opts *opts = to_f_midi2_opts(item);
2597 usb_put_function_instance(&opts->func_inst);
2600 static struct configfs_item_operations f_midi2_item_ops = {
2601 .release = f_midi2_opts_release,
2604 static struct configfs_group_operations f_midi2_group_ops = {
2605 .make_group = f_midi2_opts_ep_make,
2606 .drop_item = f_midi2_opts_ep_drop,
2609 static const struct config_item_type f_midi2_func_type = {
2610 .ct_item_ops = &f_midi2_item_ops,
2611 .ct_group_ops = &f_midi2_group_ops,
2612 .ct_attrs = f_midi2_attrs,
2613 .ct_owner = THIS_MODULE,
2616 static void f_midi2_free_inst(struct usb_function_instance *f)
2618 struct f_midi2_opts *opts;
2620 opts = container_of(f, struct f_midi2_opts, func_inst);
2622 kfree(opts->info.iface_name);
2626 /* gadget alloc_inst */
2627 static struct usb_function_instance *f_midi2_alloc_inst(void)
2629 struct f_midi2_opts *opts;
2630 struct f_midi2_ep_opts *ep_opts;
2631 struct f_midi2_block_opts *block_opts;
2634 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
2636 return ERR_PTR(-ENOMEM);
2638 mutex_init(&opts->lock);
2639 opts->func_inst.free_func_inst = f_midi2_free_inst;
2640 opts->info.process_ump = true;
2641 opts->info.static_block = true;
2642 opts->info.num_reqs = 32;
2643 opts->info.req_buf_size = 512;
2645 /* create the default ep */
2646 ret = f_midi2_ep_opts_create(opts, 0, &ep_opts);
2649 return ERR_PTR(ret);
2652 /* create the default block */
2653 ret = f_midi2_block_opts_create(ep_opts, 0, &block_opts);
2657 return ERR_PTR(ret);
2660 /* set up the default MIDI1 (that is mandatory) */
2661 block_opts->info.midi1_num_groups = 1;
2663 config_group_init_type_name(&opts->func_inst.group, "",
2664 &f_midi2_func_type);
2666 config_group_init_type_name(&ep_opts->group, "ep.0",
2668 configfs_add_default_group(&ep_opts->group, &opts->func_inst.group);
2670 config_group_init_type_name(&block_opts->group, "block.0",
2671 &f_midi2_block_type);
2672 configfs_add_default_group(&block_opts->group, &ep_opts->group);
2674 return &opts->func_inst;
2677 static void do_f_midi2_free(struct f_midi2 *midi2, struct f_midi2_opts *opts)
2679 mutex_lock(&opts->lock);
2681 mutex_unlock(&opts->lock);
2682 kfree(midi2->string_defs);
2686 static void f_midi2_free(struct usb_function *f)
2688 do_f_midi2_free(func_to_midi2(f),
2689 container_of(f->fi, struct f_midi2_opts, func_inst));
2692 /* verify the parameters set up via configfs;
2693 * return the number of EPs or a negative error
2695 static int verify_parameters(struct f_midi2_opts *opts)
2697 int i, j, num_eps, num_blks;
2698 struct f_midi2_ep_info *ep;
2699 struct f_midi2_block_info *bp;
2701 for (num_eps = 0; num_eps < MAX_UMP_EPS && opts->eps[num_eps];
2705 pr_err("f_midi2: No EP is defined\n");
2710 for (i = 0; i < num_eps; i++) {
2711 ep = &opts->eps[i]->info;
2712 if (!(ep->protocol_caps & ep->protocol)) {
2713 pr_err("f_midi2: Invalid protocol 0x%x (caps 0x%x) for EP %d\n",
2714 ep->protocol, ep->protocol_caps, i);
2718 for (j = 0; j < SNDRV_UMP_MAX_BLOCKS && opts->eps[i]->blks[j];
2720 bp = &opts->eps[i]->blks[j]->info;
2721 if (bp->first_group + bp->num_groups > SNDRV_UMP_MAX_GROUPS) {
2722 pr_err("f_midi2: Invalid group definitions for block %d:%d\n",
2727 if (bp->midi1_num_groups) {
2728 if (bp->midi1_first_group < bp->first_group ||
2729 bp->midi1_first_group + bp->midi1_num_groups >
2730 bp->first_group + bp->num_groups) {
2731 pr_err("f_midi2: Invalid MIDI1 group definitions for block %d:%d\n",
2739 pr_err("f_midi2: No block is defined\n");
2746 /* fill mapping between MIDI 1.0 cable and UMP EP/group */
2747 static void fill_midi1_cable_mapping(struct f_midi2 *midi2,
2748 struct f_midi2_ep *ep,
2751 const struct f_midi2_block_info *binfo = &ep->blks[blk].info;
2752 struct midi1_cable_mapping *map;
2755 if (!binfo->midi1_num_groups)
2757 if (binfo->direction != SNDRV_UMP_DIR_OUTPUT) {
2758 group = binfo->midi1_first_group;
2759 map = midi2->in_cable_mapping + midi2->num_midi1_in;
2760 for (i = 0; i < binfo->midi1_num_groups; i++, group++, map++) {
2761 if (midi2->num_midi1_in >= MAX_CABLES)
2766 midi2->num_midi1_in++;
2767 /* store 1-based cable number */
2768 ep->in_group_to_cable[group] = midi2->num_midi1_in;
2772 if (binfo->direction != SNDRV_UMP_DIR_INPUT) {
2773 group = binfo->midi1_first_group;
2774 map = midi2->out_cable_mapping + midi2->num_midi1_out;
2775 for (i = 0; i < binfo->midi1_num_groups; i++, group++, map++) {
2776 if (midi2->num_midi1_out >= MAX_CABLES)
2781 midi2->num_midi1_out++;
2786 /* gadget alloc callback */
2787 static struct usb_function *f_midi2_alloc(struct usb_function_instance *fi)
2789 struct f_midi2 *midi2;
2790 struct f_midi2_opts *opts;
2791 struct f_midi2_ep *ep;
2792 struct f_midi2_block *bp;
2793 int i, num_eps, blk;
2795 midi2 = kzalloc(sizeof(*midi2), GFP_KERNEL);
2797 return ERR_PTR(-ENOMEM);
2799 opts = container_of(fi, struct f_midi2_opts, func_inst);
2800 mutex_lock(&opts->lock);
2801 num_eps = verify_parameters(opts);
2803 mutex_unlock(&opts->lock);
2805 return ERR_PTR(num_eps);
2808 mutex_unlock(&opts->lock);
2810 spin_lock_init(&midi2->queue_lock);
2812 midi2->func.name = "midi2_func";
2813 midi2->func.bind = f_midi2_bind;
2814 midi2->func.unbind = f_midi2_unbind;
2815 midi2->func.get_alt = f_midi2_get_alt;
2816 midi2->func.set_alt = f_midi2_set_alt;
2817 midi2->func.setup = f_midi2_setup;
2818 midi2->func.disable = f_midi2_disable;
2819 midi2->func.free_func = f_midi2_free;
2821 midi2->info = opts->info;
2822 midi2->num_eps = num_eps;
2824 for (i = 0; i < num_eps; i++) {
2825 ep = &midi2->midi2_eps[i];
2826 ep->info = opts->eps[i]->info;
2828 for (blk = 0; blk < SNDRV_UMP_MAX_BLOCKS &&
2829 opts->eps[i]->blks[blk]; blk++) {
2830 bp = &ep->blks[blk];
2832 bp->info = opts->eps[i]->blks[blk]->info;
2833 bp->gtb_id = ++midi2->total_blocks;
2837 midi2->string_defs = kcalloc(midi2->total_blocks + 1,
2838 sizeof(*midi2->string_defs), GFP_KERNEL);
2839 if (!midi2->string_defs) {
2840 do_f_midi2_free(midi2, opts);
2841 return ERR_PTR(-ENOMEM);
2844 if (opts->info.iface_name && *opts->info.iface_name)
2845 midi2->string_defs[STR_IFACE].s = opts->info.iface_name;
2847 midi2->string_defs[STR_IFACE].s = ump_ep_name(&midi2->midi2_eps[0]);
2849 for (i = 0; i < midi2->num_eps; i++) {
2850 ep = &midi2->midi2_eps[i];
2851 for (blk = 0; blk < ep->num_blks; blk++) {
2852 bp = &ep->blks[blk];
2853 midi2->string_defs[gtb_to_str_id(bp->gtb_id)].s =
2854 ump_fb_name(&bp->info);
2856 fill_midi1_cable_mapping(midi2, ep, blk);
2860 if (!midi2->num_midi1_in && !midi2->num_midi1_out) {
2861 pr_err("f_midi2: MIDI1 definition is missing\n");
2862 do_f_midi2_free(midi2, opts);
2863 return ERR_PTR(-EINVAL);
2866 return &midi2->func;
2869 DECLARE_USB_FUNCTION_INIT(midi2, f_midi2_alloc_inst, f_midi2_alloc);
2871 MODULE_LICENSE("GPL");