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/module.h>
24 #include <linux/slab.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>
39 MODULE_AUTHOR("Ben Williamson");
40 MODULE_LICENSE("GPL v2");
42 static const char f_midi_shortname[] = "f_midi";
43 static const char f_midi_longname[] = "MIDI Gadget";
46 * We can only handle 16 cables on one single endpoint, as cable numbers are
47 * stored in 4-bit fields. And as the interface currently only holds one
48 * single endpoint, this is the maximum number of ports we can allow.
53 * This is a gadget, and the IN/OUT naming is from the host's perspective.
54 * USB -> OUT endpoint -> rawmidi
55 * USB <- IN endpoint <- rawmidi
57 struct gmidi_in_port {
62 #define STATE_UNKNOWN 0
63 #define STATE_1PARAM 1
64 #define STATE_2PARAM_1 2
65 #define STATE_2PARAM_2 3
66 #define STATE_SYSEX_0 4
67 #define STATE_SYSEX_1 5
68 #define STATE_SYSEX_2 6
73 struct usb_function func;
74 struct usb_gadget *gadget;
75 struct usb_ep *in_ep, *out_ep;
76 struct snd_card *card;
77 struct snd_rawmidi *rmidi;
79 struct snd_rawmidi_substream *in_substream[MAX_PORTS];
80 struct snd_rawmidi_substream *out_substream[MAX_PORTS];
81 struct gmidi_in_port *in_port[MAX_PORTS];
83 unsigned long out_triggered;
84 struct tasklet_struct tasklet;
85 unsigned int in_ports;
86 unsigned int out_ports;
89 unsigned int buflen, qlen;
92 static inline struct f_midi *func_to_midi(struct usb_function *f)
94 return container_of(f, struct f_midi, func);
97 static void f_midi_transmit(struct f_midi *midi, struct usb_request *req);
99 DECLARE_UAC_AC_HEADER_DESCRIPTOR(1);
100 DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1);
101 DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(16);
103 /* B.3.1 Standard AC Interface Descriptor */
104 static struct usb_interface_descriptor ac_interface_desc = {
105 .bLength = USB_DT_INTERFACE_SIZE,
106 .bDescriptorType = USB_DT_INTERFACE,
107 /* .bInterfaceNumber = DYNAMIC */
108 /* .bNumEndpoints = DYNAMIC */
109 .bInterfaceClass = USB_CLASS_AUDIO,
110 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
111 /* .iInterface = DYNAMIC */
114 /* B.3.2 Class-Specific AC Interface Descriptor */
115 static struct uac1_ac_header_descriptor_1 ac_header_desc = {
116 .bLength = UAC_DT_AC_HEADER_SIZE(1),
117 .bDescriptorType = USB_DT_CS_INTERFACE,
118 .bDescriptorSubtype = USB_MS_HEADER,
119 .bcdADC = cpu_to_le16(0x0100),
120 .wTotalLength = cpu_to_le16(UAC_DT_AC_HEADER_SIZE(1)),
122 /* .baInterfaceNr = DYNAMIC */
125 /* B.4.1 Standard MS Interface Descriptor */
126 static struct usb_interface_descriptor ms_interface_desc = {
127 .bLength = USB_DT_INTERFACE_SIZE,
128 .bDescriptorType = USB_DT_INTERFACE,
129 /* .bInterfaceNumber = DYNAMIC */
131 .bInterfaceClass = USB_CLASS_AUDIO,
132 .bInterfaceSubClass = USB_SUBCLASS_MIDISTREAMING,
133 /* .iInterface = DYNAMIC */
136 /* B.4.2 Class-Specific MS Interface Descriptor */
137 static struct usb_ms_header_descriptor ms_header_desc = {
138 .bLength = USB_DT_MS_HEADER_SIZE,
139 .bDescriptorType = USB_DT_CS_INTERFACE,
140 .bDescriptorSubtype = USB_MS_HEADER,
141 .bcdMSC = cpu_to_le16(0x0100),
142 /* .wTotalLength = DYNAMIC */
145 /* B.5.1 Standard Bulk OUT Endpoint Descriptor */
146 static struct usb_endpoint_descriptor bulk_out_desc = {
147 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
148 .bDescriptorType = USB_DT_ENDPOINT,
149 .bEndpointAddress = USB_DIR_OUT,
150 .bmAttributes = USB_ENDPOINT_XFER_BULK,
153 /* B.5.2 Class-specific MS Bulk OUT Endpoint Descriptor */
154 static struct usb_ms_endpoint_descriptor_16 ms_out_desc = {
155 /* .bLength = DYNAMIC */
156 .bDescriptorType = USB_DT_CS_ENDPOINT,
157 .bDescriptorSubtype = USB_MS_GENERAL,
158 /* .bNumEmbMIDIJack = DYNAMIC */
159 /* .baAssocJackID = DYNAMIC */
162 /* B.6.1 Standard Bulk IN Endpoint Descriptor */
163 static struct usb_endpoint_descriptor bulk_in_desc = {
164 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
165 .bDescriptorType = USB_DT_ENDPOINT,
166 .bEndpointAddress = USB_DIR_IN,
167 .bmAttributes = USB_ENDPOINT_XFER_BULK,
170 /* B.6.2 Class-specific MS Bulk IN Endpoint Descriptor */
171 static struct usb_ms_endpoint_descriptor_16 ms_in_desc = {
172 /* .bLength = DYNAMIC */
173 .bDescriptorType = USB_DT_CS_ENDPOINT,
174 .bDescriptorSubtype = USB_MS_GENERAL,
175 /* .bNumEmbMIDIJack = DYNAMIC */
176 /* .baAssocJackID = DYNAMIC */
179 /* string IDs are assigned dynamically */
181 #define STRING_FUNC_IDX 0
183 static struct usb_string midi_string_defs[] = {
184 [STRING_FUNC_IDX].s = "MIDI function",
185 { } /* end of list */
188 static struct usb_gadget_strings midi_stringtab = {
189 .language = 0x0409, /* en-us */
190 .strings = midi_string_defs,
193 static struct usb_gadget_strings *midi_strings[] = {
198 static inline struct usb_request *midi_alloc_ep_req(struct usb_ep *ep,
201 return alloc_ep_req(ep, length, length);
204 static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
207 usb_ep_free_request(ep, req);
210 static const uint8_t f_midi_cin_length[] = {
211 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
215 * Receives a chunk of MIDI data.
217 static void f_midi_read_data(struct usb_ep *ep, int cable,
218 uint8_t *data, int length)
220 struct f_midi *midi = ep->driver_data;
221 struct snd_rawmidi_substream *substream = midi->out_substream[cable];
224 /* Nobody is listening - throw it on the floor. */
227 if (!test_bit(cable, &midi->out_triggered))
230 snd_rawmidi_receive(substream, data, length);
233 static void f_midi_handle_out_data(struct usb_ep *ep, struct usb_request *req)
238 for (i = 0; i + 3 < req->actual; i += 4)
240 int cable = buf[i] >> 4;
241 int length = f_midi_cin_length[buf[i] & 0x0f];
242 f_midi_read_data(ep, cable, &buf[i + 1], length);
247 f_midi_complete(struct usb_ep *ep, struct usb_request *req)
249 struct f_midi *midi = ep->driver_data;
250 struct usb_composite_dev *cdev = midi->func.config->cdev;
251 int status = req->status;
254 case 0: /* normal completion */
255 if (ep == midi->out_ep) {
256 /* We received stuff. req is queued again, below */
257 f_midi_handle_out_data(ep, req);
258 } else if (ep == midi->in_ep) {
259 /* Our transmit completed. See if there's more to go.
260 * f_midi_transmit eats req, don't queue it again. */
261 f_midi_transmit(midi, req);
266 /* this endpoint is normally active while we're configured */
267 case -ECONNABORTED: /* hardware forced ep reset */
268 case -ECONNRESET: /* request dequeued */
269 case -ESHUTDOWN: /* disconnect from host */
270 VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
271 req->actual, req->length);
272 if (ep == midi->out_ep)
273 f_midi_handle_out_data(ep, req);
275 free_ep_req(ep, req);
278 case -EOVERFLOW: /* buffer overrun on read means that
279 * we didn't provide a big enough buffer.
282 DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
283 status, req->actual, req->length);
285 case -EREMOTEIO: /* short read */
289 status = usb_ep_queue(ep, req, GFP_ATOMIC);
291 ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
292 ep->name, req->length, status);
294 /* FIXME recover later ... somehow */
298 static int f_midi_start_ep(struct f_midi *midi,
299 struct usb_function *f,
303 struct usb_composite_dev *cdev = f->config->cdev;
308 err = config_ep_by_speed(midi->gadget, f, ep);
310 ERROR(cdev, "can't configure %s: %d\n", ep->name, err);
314 err = usb_ep_enable(ep);
316 ERROR(cdev, "can't start %s: %d\n", ep->name, err);
320 ep->driver_data = midi;
325 static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
327 struct f_midi *midi = func_to_midi(f);
328 struct usb_composite_dev *cdev = f->config->cdev;
332 err = f_midi_start_ep(midi, f, midi->in_ep);
336 err = f_midi_start_ep(midi, f, midi->out_ep);
340 if (midi->out_ep->driver_data)
341 usb_ep_disable(midi->out_ep);
343 err = config_ep_by_speed(midi->gadget, f, midi->out_ep);
345 ERROR(cdev, "can't configure %s: %d\n",
346 midi->out_ep->name, err);
350 err = usb_ep_enable(midi->out_ep);
352 ERROR(cdev, "can't start %s: %d\n",
353 midi->out_ep->name, err);
357 midi->out_ep->driver_data = midi;
359 /* allocate a bunch of read buffers and queue them all at once. */
360 for (i = 0; i < midi->qlen && err == 0; i++) {
361 struct usb_request *req =
362 midi_alloc_ep_req(midi->out_ep, midi->buflen);
366 req->complete = f_midi_complete;
367 err = usb_ep_queue(midi->out_ep, req, GFP_ATOMIC);
369 ERROR(midi, "%s queue req: %d\n",
370 midi->out_ep->name, err);
377 static void f_midi_disable(struct usb_function *f)
379 struct f_midi *midi = func_to_midi(f);
380 struct usb_composite_dev *cdev = f->config->cdev;
382 DBG(cdev, "disable\n");
385 * just disable endpoints, forcing completion of pending i/o.
386 * all our completion handlers free their requests in this case.
388 usb_ep_disable(midi->in_ep);
389 usb_ep_disable(midi->out_ep);
392 static int f_midi_snd_free(struct snd_device *device)
397 static void f_midi_transmit_packet(struct usb_request *req, uint8_t p0,
398 uint8_t p1, uint8_t p2, uint8_t p3)
400 unsigned length = req->length;
401 u8 *buf = (u8 *)req->buf + length;
407 req->length = length + 4;
411 * Converts MIDI commands to USB MIDI packets.
413 static void f_midi_transmit_byte(struct usb_request *req,
414 struct gmidi_in_port *port, uint8_t b)
416 uint8_t p0 = port->cable << 4;
419 f_midi_transmit_packet(req, p0 | 0x0f, b, 0, 0);
420 } else if (b >= 0xf0) {
424 port->state = STATE_SYSEX_1;
429 port->state = STATE_1PARAM;
433 port->state = STATE_2PARAM_1;
437 port->state = STATE_UNKNOWN;
440 f_midi_transmit_packet(req, p0 | 0x05, 0xf6, 0, 0);
441 port->state = STATE_UNKNOWN;
444 switch (port->state) {
446 f_midi_transmit_packet(req,
447 p0 | 0x05, 0xf7, 0, 0);
450 f_midi_transmit_packet(req,
451 p0 | 0x06, port->data[0], 0xf7, 0);
454 f_midi_transmit_packet(req,
455 p0 | 0x07, port->data[0],
456 port->data[1], 0xf7);
459 port->state = STATE_UNKNOWN;
462 } else if (b >= 0x80) {
464 if (b >= 0xc0 && b <= 0xdf)
465 port->state = STATE_1PARAM;
467 port->state = STATE_2PARAM_1;
468 } else { /* b < 0x80 */
469 switch (port->state) {
471 if (port->data[0] < 0xf0) {
472 p0 |= port->data[0] >> 4;
475 port->state = STATE_UNKNOWN;
477 f_midi_transmit_packet(req, p0, port->data[0], b, 0);
481 port->state = STATE_2PARAM_2;
484 if (port->data[0] < 0xf0) {
485 p0 |= port->data[0] >> 4;
486 port->state = STATE_2PARAM_1;
489 port->state = STATE_UNKNOWN;
491 f_midi_transmit_packet(req,
492 p0, port->data[0], port->data[1], b);
496 port->state = STATE_SYSEX_1;
500 port->state = STATE_SYSEX_2;
503 f_midi_transmit_packet(req,
504 p0 | 0x04, port->data[0], port->data[1], b);
505 port->state = STATE_SYSEX_0;
511 static void f_midi_transmit(struct f_midi *midi, struct usb_request *req)
513 struct usb_ep *ep = midi->in_ep;
520 req = midi_alloc_ep_req(ep, midi->buflen);
523 ERROR(midi, "%s: alloc_ep_request failed\n", __func__);
527 req->complete = f_midi_complete;
529 for (i = 0; i < MAX_PORTS; i++) {
530 struct gmidi_in_port *port = midi->in_port[i];
531 struct snd_rawmidi_substream *substream = midi->in_substream[i];
533 if (!port || !port->active || !substream)
536 while (req->length + 3 < midi->buflen) {
538 if (snd_rawmidi_transmit(substream, &b, 1) != 1) {
542 f_midi_transmit_byte(req, port, b);
547 usb_ep_queue(ep, req, GFP_ATOMIC);
549 free_ep_req(ep, req);
552 static void f_midi_in_tasklet(unsigned long data)
554 struct f_midi *midi = (struct f_midi *) data;
555 f_midi_transmit(midi, NULL);
558 static int f_midi_in_open(struct snd_rawmidi_substream *substream)
560 struct f_midi *midi = substream->rmidi->private_data;
562 if (!midi->in_port[substream->number])
565 VDBG(midi, "%s()\n", __func__);
566 midi->in_substream[substream->number] = substream;
567 midi->in_port[substream->number]->state = STATE_UNKNOWN;
571 static int f_midi_in_close(struct snd_rawmidi_substream *substream)
573 struct f_midi *midi = substream->rmidi->private_data;
575 VDBG(midi, "%s()\n", __func__);
579 static void f_midi_in_trigger(struct snd_rawmidi_substream *substream, int up)
581 struct f_midi *midi = substream->rmidi->private_data;
583 if (!midi->in_port[substream->number])
586 VDBG(midi, "%s() %d\n", __func__, up);
587 midi->in_port[substream->number]->active = up;
589 tasklet_hi_schedule(&midi->tasklet);
592 static int f_midi_out_open(struct snd_rawmidi_substream *substream)
594 struct f_midi *midi = substream->rmidi->private_data;
596 if (substream->number >= MAX_PORTS)
599 VDBG(midi, "%s()\n", __func__);
600 midi->out_substream[substream->number] = substream;
604 static int f_midi_out_close(struct snd_rawmidi_substream *substream)
606 struct f_midi *midi = substream->rmidi->private_data;
608 VDBG(midi, "%s()\n", __func__);
612 static void f_midi_out_trigger(struct snd_rawmidi_substream *substream, int up)
614 struct f_midi *midi = substream->rmidi->private_data;
616 VDBG(midi, "%s()\n", __func__);
619 set_bit(substream->number, &midi->out_triggered);
621 clear_bit(substream->number, &midi->out_triggered);
624 static struct snd_rawmidi_ops gmidi_in_ops = {
625 .open = f_midi_in_open,
626 .close = f_midi_in_close,
627 .trigger = f_midi_in_trigger,
630 static struct snd_rawmidi_ops gmidi_out_ops = {
631 .open = f_midi_out_open,
632 .close = f_midi_out_close,
633 .trigger = f_midi_out_trigger
636 static inline void f_midi_unregister_card(struct f_midi *midi)
639 snd_card_free(midi->card);
644 /* register as a sound "card" */
645 static int f_midi_register_card(struct f_midi *midi)
647 struct snd_card *card;
648 struct snd_rawmidi *rmidi;
650 static struct snd_device_ops ops = {
651 .dev_free = f_midi_snd_free,
654 err = snd_card_new(&midi->gadget->dev, midi->index, midi->id,
655 THIS_MODULE, 0, &card);
657 ERROR(midi, "snd_card_new() failed\n");
662 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, midi, &ops);
664 ERROR(midi, "snd_device_new() failed: error %d\n", err);
668 strcpy(card->driver, f_midi_longname);
669 strcpy(card->longname, f_midi_longname);
670 strcpy(card->shortname, f_midi_shortname);
673 snd_component_add(card, "MIDI");
674 err = snd_rawmidi_new(card, card->longname, 0,
675 midi->out_ports, midi->in_ports, &rmidi);
677 ERROR(midi, "snd_rawmidi_new() failed: error %d\n", err);
681 strcpy(rmidi->name, card->shortname);
682 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
683 SNDRV_RAWMIDI_INFO_INPUT |
684 SNDRV_RAWMIDI_INFO_DUPLEX;
685 rmidi->private_data = midi;
688 * Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT.
689 * It's an upside-down world being a gadget.
691 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &gmidi_in_ops);
692 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &gmidi_out_ops);
694 /* register it - we're ready to go */
695 err = snd_card_register(card);
697 ERROR(midi, "snd_card_register() failed\n");
701 VDBG(midi, "%s() finished ok\n", __func__);
705 f_midi_unregister_card(midi);
709 /* MIDI function driver setup/binding */
711 static int f_midi_bind(struct usb_configuration *c, struct usb_function *f)
713 struct usb_descriptor_header **midi_function;
714 struct usb_midi_in_jack_descriptor jack_in_ext_desc[MAX_PORTS];
715 struct usb_midi_in_jack_descriptor jack_in_emb_desc[MAX_PORTS];
716 struct usb_midi_out_jack_descriptor_1 jack_out_ext_desc[MAX_PORTS];
717 struct usb_midi_out_jack_descriptor_1 jack_out_emb_desc[MAX_PORTS];
718 struct usb_composite_dev *cdev = c->cdev;
719 struct f_midi *midi = func_to_midi(f);
720 struct usb_string *us;
721 int status, n, jack = 1, i = 0;
723 midi->gadget = cdev->gadget;
724 tasklet_init(&midi->tasklet, f_midi_in_tasklet, (unsigned long) midi);
725 status = f_midi_register_card(midi);
729 /* maybe allocate device-global string ID */
730 us = usb_gstrings_attach(c->cdev, midi_strings,
731 ARRAY_SIZE(midi_string_defs));
733 status = PTR_ERR(us);
736 ac_interface_desc.iInterface = us[STRING_FUNC_IDX].id;
738 /* We have two interfaces, AudioControl and MIDIStreaming */
739 status = usb_interface_id(c, f);
742 ac_interface_desc.bInterfaceNumber = status;
744 status = usb_interface_id(c, f);
747 ms_interface_desc.bInterfaceNumber = status;
748 ac_header_desc.baInterfaceNr[0] = status;
752 /* allocate instance-specific endpoints */
753 midi->in_ep = usb_ep_autoconfig(cdev->gadget, &bulk_in_desc);
756 midi->in_ep->driver_data = cdev; /* claim */
758 midi->out_ep = usb_ep_autoconfig(cdev->gadget, &bulk_out_desc);
761 midi->out_ep->driver_data = cdev; /* claim */
763 /* allocate temporary function list */
764 midi_function = kcalloc((MAX_PORTS * 4) + 9, sizeof(*midi_function),
766 if (!midi_function) {
772 * construct the function's descriptor set. As the number of
773 * input and output MIDI ports is configurable, we have to do
777 /* add the headers - these are always the same */
778 midi_function[i++] = (struct usb_descriptor_header *) &ac_interface_desc;
779 midi_function[i++] = (struct usb_descriptor_header *) &ac_header_desc;
780 midi_function[i++] = (struct usb_descriptor_header *) &ms_interface_desc;
782 /* calculate the header's wTotalLength */
783 n = USB_DT_MS_HEADER_SIZE
784 + (midi->in_ports + midi->out_ports) *
785 (USB_DT_MIDI_IN_SIZE + USB_DT_MIDI_OUT_SIZE(1));
786 ms_header_desc.wTotalLength = cpu_to_le16(n);
788 midi_function[i++] = (struct usb_descriptor_header *) &ms_header_desc;
790 /* configure the external IN jacks, each linked to an embedded OUT jack */
791 for (n = 0; n < midi->in_ports; n++) {
792 struct usb_midi_in_jack_descriptor *in_ext = &jack_in_ext_desc[n];
793 struct usb_midi_out_jack_descriptor_1 *out_emb = &jack_out_emb_desc[n];
795 in_ext->bLength = USB_DT_MIDI_IN_SIZE;
796 in_ext->bDescriptorType = USB_DT_CS_INTERFACE;
797 in_ext->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
798 in_ext->bJackType = USB_MS_EXTERNAL;
799 in_ext->bJackID = jack++;
801 midi_function[i++] = (struct usb_descriptor_header *) in_ext;
803 out_emb->bLength = USB_DT_MIDI_OUT_SIZE(1);
804 out_emb->bDescriptorType = USB_DT_CS_INTERFACE;
805 out_emb->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
806 out_emb->bJackType = USB_MS_EMBEDDED;
807 out_emb->bJackID = jack++;
808 out_emb->bNrInputPins = 1;
809 out_emb->pins[0].baSourcePin = 1;
810 out_emb->pins[0].baSourceID = in_ext->bJackID;
812 midi_function[i++] = (struct usb_descriptor_header *) out_emb;
814 /* link it to the endpoint */
815 ms_in_desc.baAssocJackID[n] = out_emb->bJackID;
818 /* configure the external OUT jacks, each linked to an embedded IN jack */
819 for (n = 0; n < midi->out_ports; n++) {
820 struct usb_midi_in_jack_descriptor *in_emb = &jack_in_emb_desc[n];
821 struct usb_midi_out_jack_descriptor_1 *out_ext = &jack_out_ext_desc[n];
823 in_emb->bLength = USB_DT_MIDI_IN_SIZE;
824 in_emb->bDescriptorType = USB_DT_CS_INTERFACE;
825 in_emb->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
826 in_emb->bJackType = USB_MS_EMBEDDED;
827 in_emb->bJackID = jack++;
829 midi_function[i++] = (struct usb_descriptor_header *) in_emb;
831 out_ext->bLength = USB_DT_MIDI_OUT_SIZE(1);
832 out_ext->bDescriptorType = USB_DT_CS_INTERFACE;
833 out_ext->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
834 out_ext->bJackType = USB_MS_EXTERNAL;
835 out_ext->bJackID = jack++;
836 out_ext->bNrInputPins = 1;
838 out_ext->pins[0].baSourceID = in_emb->bJackID;
839 out_ext->pins[0].baSourcePin = 1;
840 midi_function[i++] = (struct usb_descriptor_header *) out_ext;
842 /* link it to the endpoint */
843 ms_out_desc.baAssocJackID[n] = in_emb->bJackID;
846 /* configure the endpoint descriptors ... */
847 ms_out_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->in_ports);
848 ms_out_desc.bNumEmbMIDIJack = midi->in_ports;
850 ms_in_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->out_ports);
851 ms_in_desc.bNumEmbMIDIJack = midi->out_ports;
853 /* ... and add them to the list */
854 midi_function[i++] = (struct usb_descriptor_header *) &bulk_out_desc;
855 midi_function[i++] = (struct usb_descriptor_header *) &ms_out_desc;
856 midi_function[i++] = (struct usb_descriptor_header *) &bulk_in_desc;
857 midi_function[i++] = (struct usb_descriptor_header *) &ms_in_desc;
858 midi_function[i++] = NULL;
861 * support all relevant hardware speeds... we expect that when
862 * hardware is dual speed, all bulk-capable endpoints work at
865 /* copy descriptors, and track endpoint copies */
866 f->fs_descriptors = usb_copy_descriptors(midi_function);
867 if (!f->fs_descriptors)
870 if (gadget_is_dualspeed(c->cdev->gadget)) {
871 bulk_in_desc.wMaxPacketSize = cpu_to_le16(512);
872 bulk_out_desc.wMaxPacketSize = cpu_to_le16(512);
873 f->hs_descriptors = usb_copy_descriptors(midi_function);
874 if (!f->hs_descriptors)
878 kfree(midi_function);
883 kfree(midi_function);
884 usb_free_descriptors(f->hs_descriptors);
886 f_midi_unregister_card(midi);
888 /* we might as well release our claims on endpoints */
890 midi->out_ep->driver_data = NULL;
892 midi->in_ep->driver_data = NULL;
894 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
899 static inline struct f_midi_opts *to_f_midi_opts(struct config_item *item)
901 return container_of(to_config_group(item), struct f_midi_opts,
905 CONFIGFS_ATTR_STRUCT(f_midi_opts);
906 CONFIGFS_ATTR_OPS(f_midi_opts);
908 static void midi_attr_release(struct config_item *item)
910 struct f_midi_opts *opts = to_f_midi_opts(item);
912 usb_put_function_instance(&opts->func_inst);
915 static struct configfs_item_operations midi_item_ops = {
916 .release = midi_attr_release,
917 .show_attribute = f_midi_opts_attr_show,
918 .store_attribute = f_midi_opts_attr_store,
921 #define F_MIDI_OPT(name, test_limit, limit) \
922 static ssize_t f_midi_opts_##name##_show(struct f_midi_opts *opts, char *page) \
926 mutex_lock(&opts->lock); \
927 result = sprintf(page, "%d\n", opts->name); \
928 mutex_unlock(&opts->lock); \
933 static ssize_t f_midi_opts_##name##_store(struct f_midi_opts *opts, \
934 const char *page, size_t len) \
939 mutex_lock(&opts->lock); \
940 if (opts->refcnt) { \
945 ret = kstrtou32(page, 0, &num); \
949 if (test_limit && num > limit) { \
957 mutex_unlock(&opts->lock); \
961 static struct f_midi_opts_attribute f_midi_opts_##name = \
962 __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, f_midi_opts_##name##_show, \
963 f_midi_opts_##name##_store)
965 F_MIDI_OPT(index, true, SNDRV_CARDS);
966 F_MIDI_OPT(buflen, false, 0);
967 F_MIDI_OPT(qlen, false, 0);
968 F_MIDI_OPT(in_ports, true, MAX_PORTS);
969 F_MIDI_OPT(out_ports, true, MAX_PORTS);
971 static ssize_t f_midi_opts_id_show(struct f_midi_opts *opts, char *page)
975 mutex_lock(&opts->lock);
977 result = strlcpy(page, opts->id, PAGE_SIZE);
983 mutex_unlock(&opts->lock);
988 static ssize_t f_midi_opts_id_store(struct f_midi_opts *opts,
989 const char *page, size_t len)
994 mutex_lock(&opts->lock);
1000 c = kstrndup(page, len, GFP_KERNEL);
1005 if (opts->id_allocated)
1008 opts->id_allocated = true;
1011 mutex_unlock(&opts->lock);
1015 static struct f_midi_opts_attribute f_midi_opts_id =
1016 __CONFIGFS_ATTR(id, S_IRUGO | S_IWUSR, f_midi_opts_id_show,
1017 f_midi_opts_id_store);
1019 static struct configfs_attribute *midi_attrs[] = {
1020 &f_midi_opts_index.attr,
1021 &f_midi_opts_buflen.attr,
1022 &f_midi_opts_qlen.attr,
1023 &f_midi_opts_in_ports.attr,
1024 &f_midi_opts_out_ports.attr,
1025 &f_midi_opts_id.attr,
1029 static struct config_item_type midi_func_type = {
1030 .ct_item_ops = &midi_item_ops,
1031 .ct_attrs = midi_attrs,
1032 .ct_owner = THIS_MODULE,
1035 static void f_midi_free_inst(struct usb_function_instance *f)
1037 struct f_midi_opts *opts;
1039 opts = container_of(f, struct f_midi_opts, func_inst);
1041 if (opts->id_allocated)
1047 static struct usb_function_instance *f_midi_alloc_inst(void)
1049 struct f_midi_opts *opts;
1051 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1053 return ERR_PTR(-ENOMEM);
1055 mutex_init(&opts->lock);
1056 opts->func_inst.free_func_inst = f_midi_free_inst;
1057 opts->index = SNDRV_DEFAULT_IDX1;
1058 opts->id = SNDRV_DEFAULT_STR1;
1062 opts->out_ports = 1;
1064 config_group_init_type_name(&opts->func_inst.group, "",
1067 return &opts->func_inst;
1070 static void f_midi_free(struct usb_function *f)
1072 struct f_midi *midi;
1073 struct f_midi_opts *opts;
1076 midi = func_to_midi(f);
1077 opts = container_of(f->fi, struct f_midi_opts, func_inst);
1079 mutex_lock(&opts->lock);
1080 for (i = opts->in_ports - 1; i >= 0; --i)
1081 kfree(midi->in_port[i]);
1084 mutex_unlock(&opts->lock);
1087 static void f_midi_unbind(struct usb_configuration *c, struct usb_function *f)
1089 struct usb_composite_dev *cdev = f->config->cdev;
1090 struct f_midi *midi = func_to_midi(f);
1091 struct snd_card *card;
1093 DBG(cdev, "unbind\n");
1095 /* just to be sure */
1101 snd_card_free(card);
1103 usb_free_all_descriptors(f);
1106 static struct usb_function *f_midi_alloc(struct usb_function_instance *fi)
1108 struct f_midi *midi;
1109 struct f_midi_opts *opts;
1112 opts = container_of(fi, struct f_midi_opts, func_inst);
1114 mutex_lock(&opts->lock);
1116 if (opts->in_ports > MAX_PORTS || opts->out_ports > MAX_PORTS) {
1117 mutex_unlock(&opts->lock);
1118 return ERR_PTR(-EINVAL);
1121 /* allocate and initialize one new instance */
1122 midi = kzalloc(sizeof(*midi), GFP_KERNEL);
1124 mutex_unlock(&opts->lock);
1125 return ERR_PTR(-ENOMEM);
1128 for (i = 0; i < opts->in_ports; i++) {
1129 struct gmidi_in_port *port = kzalloc(sizeof(*port), GFP_KERNEL);
1133 mutex_unlock(&opts->lock);
1140 midi->in_port[i] = port;
1143 /* set up ALSA midi devices */
1144 midi->id = kstrdup(opts->id, GFP_KERNEL);
1145 if (opts->id && !midi->id) {
1147 mutex_unlock(&opts->lock);
1150 midi->in_ports = opts->in_ports;
1151 midi->out_ports = opts->out_ports;
1152 midi->index = opts->index;
1153 midi->buflen = opts->buflen;
1154 midi->qlen = opts->qlen;
1156 mutex_unlock(&opts->lock);
1158 midi->func.name = "gmidi function";
1159 midi->func.bind = f_midi_bind;
1160 midi->func.unbind = f_midi_unbind;
1161 midi->func.set_alt = f_midi_set_alt;
1162 midi->func.disable = f_midi_disable;
1163 midi->func.free_func = f_midi_free;
1168 for (--i; i >= 0; i--)
1169 kfree(midi->in_port[i]);
1171 return ERR_PTR(status);
1174 DECLARE_USB_FUNCTION_INIT(midi, f_midi_alloc_inst, f_midi_alloc);