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