2 * f_midi.c -- USB MIDI class function driver
4 * Copyright (C) 2006 Thumtronics Pty Ltd.
5 * Developed for Thumtronics by Grey Innovation
6 * Ben Williamson <ben.williamson@greyinnovation.com>
8 * Rewritten for the composite framework
9 * Copyright (C) 2011 Daniel Mack <zonque@gmail.com>
11 * Based on drivers/usb/gadget/f_audio.c,
12 * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
13 * Copyright (C) 2008 Analog Devices, Inc
15 * and drivers/usb/gadget/midi.c,
16 * Copyright (C) 2006 Thumtronics Pty Ltd.
17 * Ben Williamson <ben.williamson@greyinnovation.com>
19 * Licensed under the GPL-2 or later.
22 #include <linux/kernel.h>
23 #include <linux/slab.h>
24 #include <linux/utsname.h>
25 #include <linux/device.h>
27 #include <sound/core.h>
28 #include <sound/initval.h>
29 #include <sound/rawmidi.h>
31 #include <linux/usb/ch9.h>
32 #include <linux/usb/gadget.h>
33 #include <linux/usb/audio.h>
34 #include <linux/usb/midi.h>
36 MODULE_AUTHOR("Ben Williamson");
37 MODULE_LICENSE("GPL v2");
39 static const char f_midi_shortname[] = "f_midi";
40 static const char f_midi_longname[] = "MIDI Gadget";
43 * We can only handle 16 cables on one single endpoint, as cable numbers are
44 * stored in 4-bit fields. And as the interface currently only holds one
45 * single endpoint, this is the maximum number of ports we can allow.
50 * This is a gadget, and the IN/OUT naming is from the host's perspective.
51 * USB -> OUT endpoint -> rawmidi
52 * USB <- IN endpoint <- rawmidi
54 struct gmidi_in_port {
59 #define STATE_UNKNOWN 0
60 #define STATE_1PARAM 1
61 #define STATE_2PARAM_1 2
62 #define STATE_2PARAM_2 3
63 #define STATE_SYSEX_0 4
64 #define STATE_SYSEX_1 5
65 #define STATE_SYSEX_2 6
70 struct usb_function func;
71 struct usb_gadget *gadget;
72 struct usb_ep *in_ep, *out_ep;
73 struct snd_card *card;
74 struct snd_rawmidi *rmidi;
76 struct snd_rawmidi_substream *in_substream[MAX_PORTS];
77 struct snd_rawmidi_substream *out_substream[MAX_PORTS];
78 struct gmidi_in_port *in_port[MAX_PORTS];
80 unsigned long out_triggered;
81 struct tasklet_struct tasklet;
82 unsigned int in_ports;
83 unsigned int out_ports;
86 unsigned int buflen, qlen;
89 static inline struct f_midi *func_to_midi(struct usb_function *f)
91 return container_of(f, struct f_midi, func);
94 static void f_midi_transmit(struct f_midi *midi, struct usb_request *req);
96 DECLARE_UAC_AC_HEADER_DESCRIPTOR(1);
97 DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1);
98 DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(16);
100 /* B.3.1 Standard AC Interface Descriptor */
101 static struct usb_interface_descriptor ac_interface_desc __initdata = {
102 .bLength = USB_DT_INTERFACE_SIZE,
103 .bDescriptorType = USB_DT_INTERFACE,
104 /* .bInterfaceNumber = DYNAMIC */
105 /* .bNumEndpoints = DYNAMIC */
106 .bInterfaceClass = USB_CLASS_AUDIO,
107 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
108 /* .iInterface = DYNAMIC */
111 /* B.3.2 Class-Specific AC Interface Descriptor */
112 static struct uac1_ac_header_descriptor_1 ac_header_desc __initdata = {
113 .bLength = UAC_DT_AC_HEADER_SIZE(1),
114 .bDescriptorType = USB_DT_CS_INTERFACE,
115 .bDescriptorSubtype = USB_MS_HEADER,
116 .bcdADC = cpu_to_le16(0x0100),
117 .wTotalLength = cpu_to_le16(UAC_DT_AC_HEADER_SIZE(1)),
119 /* .baInterfaceNr = DYNAMIC */
122 /* B.4.1 Standard MS Interface Descriptor */
123 static struct usb_interface_descriptor ms_interface_desc __initdata = {
124 .bLength = USB_DT_INTERFACE_SIZE,
125 .bDescriptorType = USB_DT_INTERFACE,
126 /* .bInterfaceNumber = DYNAMIC */
128 .bInterfaceClass = USB_CLASS_AUDIO,
129 .bInterfaceSubClass = USB_SUBCLASS_MIDISTREAMING,
130 /* .iInterface = DYNAMIC */
133 /* B.4.2 Class-Specific MS Interface Descriptor */
134 static struct usb_ms_header_descriptor ms_header_desc __initdata = {
135 .bLength = USB_DT_MS_HEADER_SIZE,
136 .bDescriptorType = USB_DT_CS_INTERFACE,
137 .bDescriptorSubtype = USB_MS_HEADER,
138 .bcdMSC = cpu_to_le16(0x0100),
139 /* .wTotalLength = DYNAMIC */
142 /* B.5.1 Standard Bulk OUT Endpoint Descriptor */
143 static struct usb_endpoint_descriptor bulk_out_desc = {
144 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
145 .bDescriptorType = USB_DT_ENDPOINT,
146 .bEndpointAddress = USB_DIR_OUT,
147 .bmAttributes = USB_ENDPOINT_XFER_BULK,
150 /* B.5.2 Class-specific MS Bulk OUT Endpoint Descriptor */
151 static struct usb_ms_endpoint_descriptor_16 ms_out_desc = {
152 /* .bLength = DYNAMIC */
153 .bDescriptorType = USB_DT_CS_ENDPOINT,
154 .bDescriptorSubtype = USB_MS_GENERAL,
155 /* .bNumEmbMIDIJack = DYNAMIC */
156 /* .baAssocJackID = DYNAMIC */
159 /* B.6.1 Standard Bulk IN Endpoint Descriptor */
160 static struct usb_endpoint_descriptor bulk_in_desc = {
161 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
162 .bDescriptorType = USB_DT_ENDPOINT,
163 .bEndpointAddress = USB_DIR_IN,
164 .bmAttributes = USB_ENDPOINT_XFER_BULK,
167 /* B.6.2 Class-specific MS Bulk IN Endpoint Descriptor */
168 static struct usb_ms_endpoint_descriptor_16 ms_in_desc = {
169 /* .bLength = DYNAMIC */
170 .bDescriptorType = USB_DT_CS_ENDPOINT,
171 .bDescriptorSubtype = USB_MS_GENERAL,
172 /* .bNumEmbMIDIJack = DYNAMIC */
173 /* .baAssocJackID = DYNAMIC */
176 /* string IDs are assigned dynamically */
178 #define STRING_FUNC_IDX 0
180 static struct usb_string midi_string_defs[] = {
181 [STRING_FUNC_IDX].s = "MIDI function",
182 { } /* end of list */
185 static struct usb_gadget_strings midi_stringtab = {
186 .language = 0x0409, /* en-us */
187 .strings = midi_string_defs,
190 static struct usb_gadget_strings *midi_strings[] = {
195 static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
197 struct usb_request *req;
199 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
201 req->length = length;
202 req->buf = kmalloc(length, GFP_ATOMIC);
204 usb_ep_free_request(ep, req);
211 static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
214 usb_ep_free_request(ep, req);
217 static const uint8_t f_midi_cin_length[] = {
218 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
222 * Receives a chunk of MIDI data.
224 static void f_midi_read_data(struct usb_ep *ep, int cable,
225 uint8_t *data, int length)
227 struct f_midi *midi = ep->driver_data;
228 struct snd_rawmidi_substream *substream = midi->out_substream[cable];
231 /* Nobody is listening - throw it on the floor. */
234 if (!test_bit(cable, &midi->out_triggered))
237 snd_rawmidi_receive(substream, data, length);
240 static void f_midi_handle_out_data(struct usb_ep *ep, struct usb_request *req)
245 for (i = 0; i + 3 < req->actual; i += 4)
247 int cable = buf[i] >> 4;
248 int length = f_midi_cin_length[buf[i] & 0x0f];
249 f_midi_read_data(ep, cable, &buf[i + 1], length);
254 f_midi_complete(struct usb_ep *ep, struct usb_request *req)
256 struct f_midi *midi = ep->driver_data;
257 struct usb_composite_dev *cdev = midi->func.config->cdev;
258 int status = req->status;
261 case 0: /* normal completion */
262 if (ep == midi->out_ep) {
263 /* We received stuff. req is queued again, below */
264 f_midi_handle_out_data(ep, req);
265 } else if (ep == midi->in_ep) {
266 /* Our transmit completed. See if there's more to go.
267 * f_midi_transmit eats req, don't queue it again. */
268 f_midi_transmit(midi, req);
273 /* this endpoint is normally active while we're configured */
274 case -ECONNABORTED: /* hardware forced ep reset */
275 case -ECONNRESET: /* request dequeued */
276 case -ESHUTDOWN: /* disconnect from host */
277 VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
278 req->actual, req->length);
279 if (ep == midi->out_ep)
280 f_midi_handle_out_data(ep, req);
282 free_ep_req(ep, req);
285 case -EOVERFLOW: /* buffer overrun on read means that
286 * we didn't provide a big enough buffer.
289 DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
290 status, req->actual, req->length);
292 case -EREMOTEIO: /* short read */
296 status = usb_ep_queue(ep, req, GFP_ATOMIC);
298 ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
299 ep->name, req->length, status);
301 /* FIXME recover later ... somehow */
305 static int f_midi_start_ep(struct f_midi *midi,
306 struct usb_function *f,
310 struct usb_composite_dev *cdev = f->config->cdev;
315 err = config_ep_by_speed(midi->gadget, f, ep);
317 ERROR(cdev, "can't configure %s: %d\n", ep->name, err);
321 err = usb_ep_enable(ep);
323 ERROR(cdev, "can't start %s: %d\n", ep->name, err);
327 ep->driver_data = midi;
332 static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
334 struct f_midi *midi = func_to_midi(f);
335 struct usb_composite_dev *cdev = f->config->cdev;
339 err = f_midi_start_ep(midi, f, midi->in_ep);
343 err = f_midi_start_ep(midi, f, midi->out_ep);
347 if (midi->out_ep->driver_data)
348 usb_ep_disable(midi->out_ep);
350 err = config_ep_by_speed(midi->gadget, f, midi->out_ep);
352 ERROR(cdev, "can't configure %s: %d\n",
353 midi->out_ep->name, err);
357 err = usb_ep_enable(midi->out_ep);
359 ERROR(cdev, "can't start %s: %d\n",
360 midi->out_ep->name, err);
364 midi->out_ep->driver_data = midi;
366 /* allocate a bunch of read buffers and queue them all at once. */
367 for (i = 0; i < midi->qlen && err == 0; i++) {
368 struct usb_request *req =
369 alloc_ep_req(midi->out_ep, midi->buflen);
373 req->complete = f_midi_complete;
374 err = usb_ep_queue(midi->out_ep, req, GFP_ATOMIC);
376 ERROR(midi, "%s queue req: %d\n",
377 midi->out_ep->name, err);
384 static void f_midi_disable(struct usb_function *f)
386 struct f_midi *midi = func_to_midi(f);
387 struct usb_composite_dev *cdev = f->config->cdev;
389 DBG(cdev, "disable\n");
392 * just disable endpoints, forcing completion of pending i/o.
393 * all our completion handlers free their requests in this case.
395 usb_ep_disable(midi->in_ep);
396 usb_ep_disable(midi->out_ep);
399 static void f_midi_unbind(struct usb_configuration *c, struct usb_function *f)
401 struct usb_composite_dev *cdev = f->config->cdev;
402 struct f_midi *midi = func_to_midi(f);
403 struct snd_card *card;
405 DBG(cdev, "unbind\n");
407 /* just to be sure */
418 usb_free_descriptors(f->descriptors);
419 usb_free_descriptors(f->hs_descriptors);
423 static int f_midi_snd_free(struct snd_device *device)
428 static void f_midi_transmit_packet(struct usb_request *req, uint8_t p0,
429 uint8_t p1, uint8_t p2, uint8_t p3)
431 unsigned length = req->length;
432 u8 *buf = (u8 *)req->buf + length;
438 req->length = length + 4;
442 * Converts MIDI commands to USB MIDI packets.
444 static void f_midi_transmit_byte(struct usb_request *req,
445 struct gmidi_in_port *port, uint8_t b)
447 uint8_t p0 = port->cable << 4;
450 f_midi_transmit_packet(req, p0 | 0x0f, b, 0, 0);
451 } else if (b >= 0xf0) {
455 port->state = STATE_SYSEX_1;
460 port->state = STATE_1PARAM;
464 port->state = STATE_2PARAM_1;
468 port->state = STATE_UNKNOWN;
471 f_midi_transmit_packet(req, p0 | 0x05, 0xf6, 0, 0);
472 port->state = STATE_UNKNOWN;
475 switch (port->state) {
477 f_midi_transmit_packet(req,
478 p0 | 0x05, 0xf7, 0, 0);
481 f_midi_transmit_packet(req,
482 p0 | 0x06, port->data[0], 0xf7, 0);
485 f_midi_transmit_packet(req,
486 p0 | 0x07, port->data[0],
487 port->data[1], 0xf7);
490 port->state = STATE_UNKNOWN;
493 } else if (b >= 0x80) {
495 if (b >= 0xc0 && b <= 0xdf)
496 port->state = STATE_1PARAM;
498 port->state = STATE_2PARAM_1;
499 } else { /* b < 0x80 */
500 switch (port->state) {
502 if (port->data[0] < 0xf0) {
503 p0 |= port->data[0] >> 4;
506 port->state = STATE_UNKNOWN;
508 f_midi_transmit_packet(req, p0, port->data[0], b, 0);
512 port->state = STATE_2PARAM_2;
515 if (port->data[0] < 0xf0) {
516 p0 |= port->data[0] >> 4;
517 port->state = STATE_2PARAM_1;
520 port->state = STATE_UNKNOWN;
522 f_midi_transmit_packet(req,
523 p0, port->data[0], port->data[1], b);
527 port->state = STATE_SYSEX_1;
531 port->state = STATE_SYSEX_2;
534 f_midi_transmit_packet(req,
535 p0 | 0x04, port->data[0], port->data[1], b);
536 port->state = STATE_SYSEX_0;
542 static void f_midi_transmit(struct f_midi *midi, struct usb_request *req)
544 struct usb_ep *ep = midi->in_ep;
551 req = alloc_ep_req(ep, midi->buflen);
554 ERROR(midi, "gmidi_transmit: alloc_ep_request failed\n");
558 req->complete = f_midi_complete;
560 for (i = 0; i < MAX_PORTS; i++) {
561 struct gmidi_in_port *port = midi->in_port[i];
562 struct snd_rawmidi_substream *substream = midi->in_substream[i];
564 if (!port || !port->active || !substream)
567 while (req->length + 3 < midi->buflen) {
569 if (snd_rawmidi_transmit(substream, &b, 1) != 1) {
573 f_midi_transmit_byte(req, port, b);
578 usb_ep_queue(ep, req, GFP_ATOMIC);
580 free_ep_req(ep, req);
583 static void f_midi_in_tasklet(unsigned long data)
585 struct f_midi *midi = (struct f_midi *) data;
586 f_midi_transmit(midi, NULL);
589 static int f_midi_in_open(struct snd_rawmidi_substream *substream)
591 struct f_midi *midi = substream->rmidi->private_data;
593 if (!midi->in_port[substream->number])
596 VDBG(midi, "%s()\n", __func__);
597 midi->in_substream[substream->number] = substream;
598 midi->in_port[substream->number]->state = STATE_UNKNOWN;
602 static int f_midi_in_close(struct snd_rawmidi_substream *substream)
604 struct f_midi *midi = substream->rmidi->private_data;
606 VDBG(midi, "%s()\n", __func__);
610 static void f_midi_in_trigger(struct snd_rawmidi_substream *substream, int up)
612 struct f_midi *midi = substream->rmidi->private_data;
614 if (!midi->in_port[substream->number])
617 VDBG(midi, "%s() %d\n", __func__, up);
618 midi->in_port[substream->number]->active = up;
620 tasklet_hi_schedule(&midi->tasklet);
623 static int f_midi_out_open(struct snd_rawmidi_substream *substream)
625 struct f_midi *midi = substream->rmidi->private_data;
627 if (substream->number >= MAX_PORTS)
630 VDBG(midi, "%s()\n", __func__);
631 midi->out_substream[substream->number] = substream;
635 static int f_midi_out_close(struct snd_rawmidi_substream *substream)
637 struct f_midi *midi = substream->rmidi->private_data;
639 VDBG(midi, "%s()\n", __func__);
643 static void f_midi_out_trigger(struct snd_rawmidi_substream *substream, int up)
645 struct f_midi *midi = substream->rmidi->private_data;
647 VDBG(midi, "%s()\n", __func__);
650 set_bit(substream->number, &midi->out_triggered);
652 clear_bit(substream->number, &midi->out_triggered);
655 static struct snd_rawmidi_ops gmidi_in_ops = {
656 .open = f_midi_in_open,
657 .close = f_midi_in_close,
658 .trigger = f_midi_in_trigger,
661 static struct snd_rawmidi_ops gmidi_out_ops = {
662 .open = f_midi_out_open,
663 .close = f_midi_out_close,
664 .trigger = f_midi_out_trigger
667 /* register as a sound "card" */
668 static int f_midi_register_card(struct f_midi *midi)
670 struct snd_card *card;
671 struct snd_rawmidi *rmidi;
673 static struct snd_device_ops ops = {
674 .dev_free = f_midi_snd_free,
677 err = snd_card_create(midi->index, midi->id, THIS_MODULE, 0, &card);
679 ERROR(midi, "snd_card_create() failed\n");
684 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, midi, &ops);
686 ERROR(midi, "snd_device_new() failed: error %d\n", err);
690 strcpy(card->driver, f_midi_longname);
691 strcpy(card->longname, f_midi_longname);
692 strcpy(card->shortname, f_midi_shortname);
695 snd_component_add(card, "MIDI");
696 err = snd_rawmidi_new(card, card->longname, 0,
697 midi->out_ports, midi->in_ports, &rmidi);
699 ERROR(midi, "snd_rawmidi_new() failed: error %d\n", err);
703 strcpy(rmidi->name, card->shortname);
704 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
705 SNDRV_RAWMIDI_INFO_INPUT |
706 SNDRV_RAWMIDI_INFO_DUPLEX;
707 rmidi->private_data = midi;
710 * Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT.
711 * It's an upside-down world being a gadget.
713 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &gmidi_in_ops);
714 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &gmidi_out_ops);
716 snd_card_set_dev(card, &midi->gadget->dev);
718 /* register it - we're ready to go */
719 err = snd_card_register(card);
721 ERROR(midi, "snd_card_register() failed\n");
725 VDBG(midi, "%s() finished ok\n", __func__);
730 snd_card_free(midi->card);
736 /* MIDI function driver setup/binding */
739 f_midi_bind(struct usb_configuration *c, struct usb_function *f)
741 struct usb_descriptor_header **midi_function;
742 struct usb_midi_in_jack_descriptor jack_in_ext_desc[MAX_PORTS];
743 struct usb_midi_in_jack_descriptor jack_in_emb_desc[MAX_PORTS];
744 struct usb_midi_out_jack_descriptor_1 jack_out_ext_desc[MAX_PORTS];
745 struct usb_midi_out_jack_descriptor_1 jack_out_emb_desc[MAX_PORTS];
746 struct usb_composite_dev *cdev = c->cdev;
747 struct f_midi *midi = func_to_midi(f);
748 int status, n, jack = 1, i = 0;
750 /* maybe allocate device-global string ID */
751 if (midi_string_defs[0].id == 0) {
752 status = usb_string_id(c->cdev);
755 midi_string_defs[0].id = status;
758 /* We have two interfaces, AudioControl and MIDIStreaming */
759 status = usb_interface_id(c, f);
762 ac_interface_desc.bInterfaceNumber = status;
764 status = usb_interface_id(c, f);
767 ms_interface_desc.bInterfaceNumber = status;
768 ac_header_desc.baInterfaceNr[0] = status;
772 /* allocate instance-specific endpoints */
773 midi->in_ep = usb_ep_autoconfig(cdev->gadget, &bulk_in_desc);
776 midi->in_ep->driver_data = cdev; /* claim */
778 midi->out_ep = usb_ep_autoconfig(cdev->gadget, &bulk_out_desc);
781 midi->out_ep->driver_data = cdev; /* claim */
783 /* allocate temporary function list */
784 midi_function = kcalloc((MAX_PORTS * 4) + 9, sizeof(midi_function),
786 if (!midi_function) {
792 * construct the function's descriptor set. As the number of
793 * input and output MIDI ports is configurable, we have to do
797 /* add the headers - these are always the same */
798 midi_function[i++] = (struct usb_descriptor_header *) &ac_interface_desc;
799 midi_function[i++] = (struct usb_descriptor_header *) &ac_header_desc;
800 midi_function[i++] = (struct usb_descriptor_header *) &ms_interface_desc;
802 /* calculate the header's wTotalLength */
803 n = USB_DT_MS_HEADER_SIZE
804 + (midi->in_ports + midi->out_ports) *
805 (USB_DT_MIDI_IN_SIZE + USB_DT_MIDI_OUT_SIZE(1));
806 ms_header_desc.wTotalLength = cpu_to_le16(n);
808 midi_function[i++] = (struct usb_descriptor_header *) &ms_header_desc;
810 /* configure the external IN jacks, each linked to an embedded OUT jack */
811 for (n = 0; n < midi->in_ports; n++) {
812 struct usb_midi_in_jack_descriptor *in_ext = &jack_in_ext_desc[n];
813 struct usb_midi_out_jack_descriptor_1 *out_emb = &jack_out_emb_desc[n];
815 in_ext->bLength = USB_DT_MIDI_IN_SIZE;
816 in_ext->bDescriptorType = USB_DT_CS_INTERFACE;
817 in_ext->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
818 in_ext->bJackType = USB_MS_EXTERNAL;
819 in_ext->bJackID = jack++;
821 midi_function[i++] = (struct usb_descriptor_header *) in_ext;
823 out_emb->bLength = USB_DT_MIDI_OUT_SIZE(1);
824 out_emb->bDescriptorType = USB_DT_CS_INTERFACE;
825 out_emb->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
826 out_emb->bJackType = USB_MS_EMBEDDED;
827 out_emb->bJackID = jack++;
828 out_emb->bNrInputPins = 1;
829 out_emb->pins[0].baSourcePin = 1;
830 out_emb->pins[0].baSourceID = in_ext->bJackID;
832 midi_function[i++] = (struct usb_descriptor_header *) out_emb;
834 /* link it to the endpoint */
835 ms_in_desc.baAssocJackID[n] = out_emb->bJackID;
838 /* configure the external OUT jacks, each linked to an embedded IN jack */
839 for (n = 0; n < midi->out_ports; n++) {
840 struct usb_midi_in_jack_descriptor *in_emb = &jack_in_emb_desc[n];
841 struct usb_midi_out_jack_descriptor_1 *out_ext = &jack_out_ext_desc[n];
843 in_emb->bLength = USB_DT_MIDI_IN_SIZE;
844 in_emb->bDescriptorType = USB_DT_CS_INTERFACE;
845 in_emb->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
846 in_emb->bJackType = USB_MS_EMBEDDED;
847 in_emb->bJackID = jack++;
849 midi_function[i++] = (struct usb_descriptor_header *) in_emb;
851 out_ext->bLength = USB_DT_MIDI_OUT_SIZE(1);
852 out_ext->bDescriptorType = USB_DT_CS_INTERFACE;
853 out_ext->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
854 out_ext->bJackType = USB_MS_EXTERNAL;
855 out_ext->bJackID = jack++;
856 out_ext->bNrInputPins = 1;
858 out_ext->pins[0].baSourceID = in_emb->bJackID;
859 out_ext->pins[0].baSourcePin = 1;
860 midi_function[i++] = (struct usb_descriptor_header *) out_ext;
862 /* link it to the endpoint */
863 ms_out_desc.baAssocJackID[n] = in_emb->bJackID;
866 /* configure the endpoint descriptors ... */
867 ms_out_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->in_ports);
868 ms_out_desc.bNumEmbMIDIJack = midi->in_ports;
870 ms_in_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->out_ports);
871 ms_in_desc.bNumEmbMIDIJack = midi->out_ports;
873 /* ... and add them to the list */
874 midi_function[i++] = (struct usb_descriptor_header *) &bulk_out_desc;
875 midi_function[i++] = (struct usb_descriptor_header *) &ms_out_desc;
876 midi_function[i++] = (struct usb_descriptor_header *) &bulk_in_desc;
877 midi_function[i++] = (struct usb_descriptor_header *) &ms_in_desc;
878 midi_function[i++] = NULL;
881 * support all relevant hardware speeds... we expect that when
882 * hardware is dual speed, all bulk-capable endpoints work at
885 /* copy descriptors, and track endpoint copies */
886 if (gadget_is_dualspeed(c->cdev->gadget)) {
888 bulk_in_desc.wMaxPacketSize = cpu_to_le16(512);
889 bulk_out_desc.wMaxPacketSize = cpu_to_le16(512);
890 f->hs_descriptors = usb_copy_descriptors(midi_function);
892 f->descriptors = usb_copy_descriptors(midi_function);
895 kfree(midi_function);
900 /* we might as well release our claims on endpoints */
902 midi->out_ep->driver_data = NULL;
904 midi->in_ep->driver_data = NULL;
906 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
912 * f_midi_bind_config - add USB MIDI function to a configuration
913 * @c: the configuration to supcard the USB audio function
914 * @index: the soundcard index to use for the ALSA device creation
915 * @id: the soundcard id to use for the ALSA device creation
916 * @buflen: the buffer length to use
917 * @qlen the number of read requests to pre-allocate
918 * Context: single threaded during gadget setup
920 * Returns zero on success, else negative errno.
922 int __init f_midi_bind_config(struct usb_configuration *c,
924 unsigned int in_ports,
925 unsigned int out_ports,
933 if (in_ports > MAX_PORTS || out_ports > MAX_PORTS)
936 /* allocate and initialize one new instance */
937 midi = kzalloc(sizeof *midi, GFP_KERNEL);
943 for (i = 0; i < in_ports; i++) {
944 struct gmidi_in_port *port = kzalloc(sizeof(*port), GFP_KERNEL);
953 midi->in_port[i] = port;
956 midi->gadget = c->cdev->gadget;
957 tasklet_init(&midi->tasklet, f_midi_in_tasklet, (unsigned long) midi);
959 /* set up ALSA midi devices */
960 midi->in_ports = in_ports;
961 midi->out_ports = out_ports;
962 status = f_midi_register_card(midi);
966 midi->func.name = "gmidi function";
967 midi->func.strings = midi_strings;
968 midi->func.bind = f_midi_bind;
969 midi->func.unbind = f_midi_unbind;
970 midi->func.set_alt = f_midi_set_alt;
971 midi->func.disable = f_midi_disable;
973 midi->id = kstrdup(id, GFP_KERNEL);
975 midi->buflen = buflen;
978 status = usb_add_function(c, &midi->func);
985 for (--i; i >= 0; i--)
986 kfree(midi->in_port[i]);